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