improved output
[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
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "cache.h"
26 #include "html.h"
27 #include "lp_ws.h"
28 #include "config.h"
29 #include "ppastats.h"
30
31 int debug;
32 static const char *program_name;
33
34 static void display_published_binaries(const char *owner, const char *ppa)
35 {
36         struct ppa_stats *ppastats;
37         struct package_stats **packages;
38         struct version_stats **versions;
39         struct distro_stats **distros;
40         struct arch_stats **archs;
41
42         ppastats = create_ppa_stats(owner, ppa);
43         packages = ppastats->packages;
44         while (packages && *packages) {
45                 struct package_stats *p = *packages;
46
47                 printf("%s (%d)\n", p->name, p->download_count);
48
49                 versions = p->versions;
50
51                 while (*versions) {
52                         printf("\t%s (%d)\n", (*versions)->version,
53                                (*versions)->download_count);
54
55                         distros = (*versions)->distros;
56
57                         while (*distros) {
58                                 printf("\t\t%s (%d)\n",
59                                        (*distros)->name,
60                                        (*distros)->download_count);
61
62                                 archs = (*distros)->archs;
63
64                                 while (*archs) {
65                                         printf("\t\t\t%s (%d)\n",
66                                                (*archs)->name,
67                                                (*archs)->download_count);
68
69                                         archs++;
70                                 }
71
72                                 distros++;
73                         }
74
75                         versions++;
76                 }
77
78                 packages++;
79         }
80
81         ppa_stats_free(ppastats);
82 }
83
84 static struct option long_options[] = {
85         {"version", no_argument, 0, 'v'},
86         {"help", no_argument, 0, 'h'},
87         {"html", no_argument, 0, 't'},
88         {"debug", no_argument, 0, 'd'},
89         {0, 0, 0, 0}
90 };
91
92 static void print_version()
93 {
94         printf("ppastats %s\n", VERSION);
95         printf("Copyright (C) %s jeanfi@gmail.com\n\
96 License GPLv2: GNU GPL version 2 or later \
97 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
98 This is free software: you are free to change and redistribute it.\n\
99 There is NO WARRANTY, to the extent permitted by law.\n",
100                "2010-2011");
101 }
102
103 static void print_help()
104 {
105         printf("Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n", program_name);
106
107         puts("ppastats is a command application"
108              " for generating PPA statistics.\n");
109
110         puts("Prints number of downloads for each published packages of a PPA "
111              "or generates an HTML page containing a graph representation.");
112
113         puts("");
114         puts("Options:");
115         puts("\
116   -h, --help          display this help and exit\n\
117   -v, --version       display version information and exit");
118
119         puts("");
120
121         puts("\
122   -t, --html       \
123 generates an HTML output.");
124
125         puts("");
126
127         printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
128         puts("");
129         printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
130 }
131
132
133 int main(int argc, char **argv)
134 {
135         char *owner, *ppa;
136         int optc;
137         int output_html = 0;
138         int cmdok = 1;
139
140         program_name = argv[0];
141
142         while ((optc = getopt_long(argc, argv, "vhtd", long_options,
143                                    NULL)) != -1) {
144                 switch (optc) {
145                 case 't':
146                         output_html = 1;
147                         break;
148                 case 'd':
149                         debug = 1;
150                         break;
151                 case 'h':
152                         print_help();
153                         exit(EXIT_SUCCESS);
154                 case 'v':
155                         print_version();
156                         exit(EXIT_SUCCESS);
157                 default:
158                         cmdok = 0;
159                         break;
160                 }
161         }
162
163         if (!cmdok || optind + 2 != argc) {
164                 fprintf(stderr, "Try `%s --help' for more information.\n",
165                         program_name);
166                 exit(EXIT_FAILURE);
167         }
168
169         owner = argv[optind];
170         ppa = argv[optind+1];
171
172         if (output_html)
173                 ppa_to_html(owner, ppa);
174         else
175                 display_published_binaries(owner, ppa);
176
177         /* for valgrind.... */
178         lp_ws_cleanup();
179         cache_cleanup();
180
181         exit(EXIT_SUCCESS);
182 }