Debian packaging: 0.0.4-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 AdacMitfahrclub adac;
20 MainLoop loop;
21
22 async void get_lifts (string city_from, string city_to, Date date) {
23         print ("Lifts from %s to %s on %d.%d.%d\n", city_from, city_to,
24                date.get_day (), date.get_month (), date.get_year ());
25
26         var lift_list = yield adac.get_lift_list (city_from, 0, city_to, 0, date);
27         foreach (Lift lift in lift_list) {
28                 string datetime = "%02d.%02d.%02d".printf (lift.time.day, lift.time.month, lift.time.year);
29                 if (lift.time.hour >= 0)
30                         datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
31                 print ("%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, datetime, lift.places, lift.price);
32                 if (LiftFlags.SMOKER in lift.flags)
33                         print ("smoker ");
34                 else if (LiftFlags.NON_SMOKER in lift.flags)
35                         print ("non_smoker ");
36                 if (LiftFlags.ADAC_MEMBER in lift.flags)
37                         print ("adac ");
38                 if  (LiftFlags.WOMEN_ONLY in lift.flags)
39                         print ("women ");
40                 print ("\n");
41                 print ("%s\n", lift.href);
42         }
43
44         loop.quit ();
45 }
46
47 async void get_details (string href) {
48         var lift = new Lift ();
49         lift.href = href;
50         yield adac.update_lift_details (lift);
51
52         string datetime = "%02d.%02d.%02d".printf (lift.time.day, lift.time.month, lift.time.year);
53         if (lift.time.hour >= 0)
54                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
55         print ("%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, datetime, lift.places, lift.price);
56         if (LiftFlags.SMOKER in lift.flags)
57                 print ("smoker ");
58         else if (LiftFlags.NON_SMOKER in lift.flags)
59                 print ("non_smoker ");
60         if (LiftFlags.ADAC_MEMBER in lift.flags)
61                 print ("adac ");
62         if  (LiftFlags.WOMEN_ONLY in lift.flags)
63                 print ("women ");
64                print ("\n");
65         foreach (string via in lift.city_via) {
66                 print ("\tvia %s\n", via);
67         }
68
69         string phone;
70         if (lift.cell != null)
71                 phone = lift.cell;
72         else if (lift.phone != null)
73                 phone = lift.phone;
74         else if (lift.phone2 == null)
75                 phone = lift.phone2;
76         else
77                 phone = "";
78         print ("Driver: %s (%s)\n", lift.name, phone);
79         print ("Description:\n%s\n", lift.description);
80
81         loop.quit ();
82 }
83
84 void usage () {
85         print ("usage: beifahrer-cli <city_from> <city_to> [date]\n");
86         print ("or:    beifahrer-cli details <lift_url>\n");
87         print ("or:    beifahrer-cli login <username> <password>\n");
88         print ("or:    beifahrer-cli login <cookie>\n");
89 }
90
91 static int main (string[] args) {
92         loop = new MainLoop (null);
93
94         if (args.length < 3) {
95                 usage ();
96                 return 1;
97         }
98
99         Curl.global_init (Curl.GLOBAL_DEFAULT);
100
101         adac = new AdacMitfahrclub ();
102
103         if (args[1] == "login") {
104                 if (args.length > 3) {
105                         adac.login (args[2], args[3]);
106                 } else {
107                         adac.set_cookie (args[2]);
108                         adac.login (null, null);
109                 }
110                 loop.run ();
111                 return 0;
112         }
113
114         if (args[1] == "details") {
115                 get_details.begin (args[2]);
116
117                 loop.run ();
118                 return 0;
119         }
120
121         string city_from = args[1];
122         string city_to = args[2];
123
124         var date = Date ();
125         if (args.length > 3) {
126                 date.set_parse (args[3]);
127         } else {
128                 var now = TimeVal ();
129                 date.set_time_val (now);
130         }
131
132         get_lifts.begin (city_from, city_to, date);
133
134         loop.run ();
135
136         Curl.global_cleanup ();
137
138         return 0;
139 }