94cf6964a96a366f50e8a16ca97c978344460e82
[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         {"skip-js-css", no_argument, 0, 'S'},
93         {0, 0, 0, 0}
94 };
95
96 static void print_version()
97 {
98         printf("ppastats %s\n", VERSION);
99         printf("Copyright (C) %s jeanfi@gmail.com\n\
100 License GPLv2: GNU GPL version 2 or later \
101 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
102 This is free software: you are free to change and redistribute it.\n\
103 There is NO WARRANTY, to the extent permitted by law.\n",
104                "2011");
105 }
106
107 static void print_help()
108 {
109         printf("Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n", program_name);
110
111         puts("ppastats is a command application"
112              " for generating PPA statistics.\n");
113
114         puts("Prints number of downloads for each published packages of a PPA "
115              "or generates an HTML page containing a graph representation.");
116
117         puts("");
118         puts("Options:");
119         puts("\
120   -h, --help          display this help and exit\n\
121   -v, --version       display version information and exit");
122
123         puts("");
124
125         puts("\
126   -o, --output-dir=[PATH]       \
127 generates HTML pages into 'PATH'.");
128
129         puts("\
130   -s, --status=[STATUS]         retrieves only package of the given status\n\
131                                 (possible values are: Pending, Published, \n\
132                                 Superseded, Deleted or Obsolete).");
133
134         puts("\
135   -S, --skip-js-css             skip installation of js and css files");
136         puts("");
137
138         printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
139         puts("");
140         printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
141 }
142
143
144 int main(int argc, char **argv)
145 {
146         char *owner, *ppa;
147         int optc;
148         int output_html = 0;
149         char *package_status = NULL;
150         int cmdok = 1;
151         char *output_dir = NULL;
152         int install_static_files = 1;
153
154         program_name = argv[0];
155
156         while ((optc = getopt_long(argc, argv, "vho:ds:S", long_options,
157                                    NULL)) != -1) {
158                 switch (optc) {
159                 case 'o':
160                         output_html = 1;
161                         output_dir = strdup(optarg);
162                         break;
163                 case 'd':
164                         debug = 1;
165                         break;
166                 case 'h':
167                         print_help();
168                         exit(EXIT_SUCCESS);
169                 case 'v':
170                         print_version();
171                         exit(EXIT_SUCCESS);
172                 case 's':
173                         if (optarg)
174                                 package_status = strdup(optarg);
175                         break;
176                 case 'S':
177                         install_static_files = 0;
178                         break;
179                 default:
180                         cmdok = 0;
181                         break;
182                 }
183         }
184
185         if (!cmdok || optind + 2 != argc) {
186                 fprintf(stderr, "Try `%s --help' for more information.\n",
187                         program_name);
188                 exit(EXIT_FAILURE);
189         }
190
191         owner = argv[optind];
192         ppa = argv[optind+1];
193
194         if (output_html)
195                 ppa_to_html(owner, ppa, package_status, output_dir,
196                             install_static_files);
197         else
198                 display_published_binaries(owner, ppa, package_status);
199
200         /* for valgrind.... */
201         free(package_status);
202         free(output_dir);
203         lp_ws_cleanup();
204         cache_cleanup();
205
206         exit(EXIT_SUCCESS);
207 }