added inlt support
[ppastats.git] / src / main.c
1 /*a
2     Copyright (C) 2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19 #include <libintl.h>
20 #define _(String) gettext (String)
21
22 #include <getopt.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "cache.h"
28 #include "html.h"
29 #include "lp_ws.h"
30 #include "config.h"
31 #include "ppastats.h"
32
33 int debug;
34 static const char *program_name;
35
36 static void display_published_binaries(const char *owner,
37                                        const char *ppa,
38                                        const char *package_status)
39 {
40         struct ppa_stats *ppastats;
41         struct package_stats **packages;
42         struct version_stats **versions;
43         struct distro_stats **distros;
44         struct arch_stats **archs;
45
46         ppastats = create_ppa_stats(owner, ppa, package_status);
47         packages = ppastats->packages;
48         while (packages && *packages) {
49                 struct package_stats *p = *packages;
50
51                 printf("%s (%d)\n", p->name, p->download_count);
52
53                 versions = p->versions;
54
55                 while (*versions) {
56                         printf("\t%s (%d)\n", (*versions)->version,
57                                (*versions)->download_count);
58
59                         distros = (*versions)->distros;
60
61                         while (*distros) {
62                                 printf("\t\t%s (%d)\n",
63                                        (*distros)->name,
64                                        (*distros)->download_count);
65
66                                 archs = (*distros)->archs;
67
68                                 while (*archs) {
69                                         printf("\t\t\t%s (%d)\n",
70                                                (*archs)->name,
71                                                (*archs)->download_count);
72
73                                         archs++;
74                                 }
75
76                                 distros++;
77                         }
78
79                         versions++;
80                 }
81
82                 packages++;
83         }
84
85         ppa_stats_free(ppastats);
86 }
87
88 static struct option long_options[] = {
89         {"version", no_argument, 0, 'v'},
90         {"help", no_argument, 0, 'h'},
91         {"output-dir", required_argument, 0, 'o'},
92         {"debug", no_argument, 0, 'd'},
93         {"status", required_argument, 0, 's'},
94         {"skip-js-css", no_argument, 0, 'S'},
95         {0, 0, 0, 0}
96 };
97
98 static void print_version()
99 {
100         printf("ppastats %s\n", VERSION);
101         printf(_("Copyright (C) %s jeanfi@gmail.com\n\
102 License GPLv2: GNU GPL version 2 or later \
103 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
104 This is free software: you are free to change and redistribute it.\n\
105 There is NO WARRANTY, to the extent permitted by law.\n"),
106                  "2011-2012");
107 }
108
109 static void print_help()
110 {
111         printf(_("Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"), program_name);
112
113         puts("ppastats is a command application"
114              " for generating PPA statistics.\n");
115
116         puts("Prints number of downloads for each published packages of a PPA "
117              "or generates an HTML page containing a graph representation.");
118
119         puts("");
120         puts(_("Options:"));
121         puts(_("\
122   -h, --help          display this help and exit\n\
123   -v, --version       display version information and exit"));
124
125         puts("");
126
127         puts("\
128   -o, --output-dir=[PATH]       \
129 generates HTML pages into 'PATH'.");
130
131         puts("\
132   -s, --status=[STATUS]         retrieves only package of the given status\n\
133                                 (possible values are: Pending, Published, \n\
134                                 Superseded, Deleted or Obsolete).");
135
136         puts("\
137   -S, --skip-js-css             skip installation of js and css files");
138         puts("");
139
140         printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
141         puts("");
142         printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
143 }
144
145
146 int main(int argc, char **argv)
147 {
148         char *owner, *ppa;
149         int optc;
150         int output_html = 0;
151         char *package_status = NULL;
152         int cmdok = 1;
153         char *output_dir = NULL;
154         int install_static_files = 1;
155
156         program_name = argv[0];
157
158         setlocale(LC_ALL, "");
159         bindtextdomain(PACKAGE, LOCALEDIR);
160         textdomain(PACKAGE);
161
162         while ((optc = getopt_long(argc, argv, "vho:ds:S", long_options,
163                                    NULL)) != -1) {
164                 switch (optc) {
165                 case 'o':
166                         output_html = 1;
167                         output_dir = strdup(optarg);
168                         break;
169                 case 'd':
170                         debug = 1;
171                         break;
172                 case 'h':
173                         print_help();
174                         exit(EXIT_SUCCESS);
175                 case 'v':
176                         print_version();
177                         exit(EXIT_SUCCESS);
178                 case 's':
179                         if (optarg)
180                                 package_status = strdup(optarg);
181                         break;
182                 case 'S':
183                         install_static_files = 0;
184                         break;
185                 default:
186                         cmdok = 0;
187                         break;
188                 }
189         }
190
191         if (!cmdok || optind + 2 != argc) {
192                 fprintf(stderr, "Try `%s --help' for more information.\n",
193                         program_name);
194                 exit(EXIT_FAILURE);
195         }
196
197         owner = argv[optind];
198         ppa = argv[optind+1];
199
200         if (output_html)
201                 ppa_to_html(owner, ppa, package_status, output_dir,
202                             install_static_files);
203         else
204                 display_published_binaries(owner, ppa, package_status);
205
206         /* for valgrind.... */
207         free(package_status);
208         free(output_dir);
209         lp_ws_cleanup();
210         cache_cleanup();
211
212         exit(EXIT_SUCCESS);
213 }