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