Initial commit - version 0.0.1
[beifahrer] / src / beifahrer-cli.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 static int main (string[] args) {
20         var result = Curl.global_init (Curl.GLOBAL_DEFAULT);
21         if (result != Curl.Code.OK)
22                 return 1;
23
24         if (args.length < 3) {
25                 print ("usage: beifahrer-cli <city_from> <city_to> [date]\n");
26                 print ("or:    beifahrer-cli details <lift_url>\n");
27                 return 1;
28         }
29
30         var adac = new AdacMitfahrclub ();
31
32         if (args[1] == "details") {
33                 var lift = adac.get_lift_details (args[2]);
34
35                 print ("%s\t%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, lift.date, lift.time, lift.places, lift.price);
36                 if (LiftFlags.SMOKER in lift.flags)
37                         print ("smoker ");
38                 else if (LiftFlags.NON_SMOKER in lift.flags)
39                         print ("non_smoker ");
40                 if (LiftFlags.ADAC_MEMBER in lift.flags)
41                         print ("adac ");
42                 if  (LiftFlags.WOMEN_ONLY in lift.flags)
43                         print ("women ");
44                 print ("\n");
45                 foreach (string via in lift.city_via) {
46                         print ("\tvia %s\n", via);
47                 }
48
49                 print ("Driver: %s (%s)\n", lift.name, lift.phone);
50                 print ("Description:\n%s\n", lift.description);
51
52                 return 0;
53         }
54
55         string city_from = args[1];
56         string city_to = args[2];
57
58         var date = Date ();
59         if (args.length > 3) {
60                 date.set_parse (args[3]);
61         } else {
62                 var now = TimeVal ();
63                 date.set_time_val (now);
64         }
65
66         print ("Lifts from %s to %s on %d.%d.%d\n", city_from, city_to,
67                date.get_day (), date.get_month (), date.get_year ());
68
69         var lift_list = adac.get_lift_list (city_from, city_to, date);
70         foreach (Lift lift in lift_list) {
71                 print ("%s\t%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, lift.date, lift.time, lift.places, lift.price);
72                 if (LiftFlags.SMOKER in lift.flags)
73                         print ("smoker ");
74                 else if (LiftFlags.NON_SMOKER in lift.flags)
75                         print ("non_smoker ");
76                 if (LiftFlags.ADAC_MEMBER in lift.flags)
77                         print ("adac ");
78                 if  (LiftFlags.WOMEN_ONLY in lift.flags)
79                         print ("women ");
80                 print ("\n");
81                 print ("%s\n", lift.href);
82         }
83
84         Curl.global_cleanup ();
85
86         return 0;
87 }