Renamed --output_dir option to --output-dir
[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   -s, --status=[STATUS]         retrieves only package of the given status\n\
130                                 (possible values are: Pending, Published, \n\
131                                 Superseded, Deleted or Obsolete).");
132
133         puts("");
134
135         printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
136         puts("");
137         printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
138 }
139
140
141 int main(int argc, char **argv)
142 {
143         char *owner, *ppa;
144         int optc;
145         int output_html = 0;
146         char *package_status = NULL;
147         int cmdok = 1;
148         char *output_dir = NULL;
149
150         program_name = argv[0];
151
152         while ((optc = getopt_long(argc, argv, "vho:ds:", long_options,
153                                    NULL)) != -1) {
154                 switch (optc) {
155                 case 'o':
156                         output_html = 1;
157                         output_dir = strdup(optarg);
158                         break;
159                 case 'd':
160                         debug = 1;
161                         break;
162                 case 'h':
163                         print_help();
164                         exit(EXIT_SUCCESS);
165                 case 'v':
166                         print_version();
167                         exit(EXIT_SUCCESS);
168                 case 's':
169                         if (optarg)
170                                 package_status = strdup(optarg);
171                         break;
172                 default:
173                         cmdok = 0;
174                         break;
175                 }
176         }
177
178         if (!cmdok || optind + 2 != argc) {
179                 fprintf(stderr, "Try `%s --help' for more information.\n",
180                         program_name);
181                 exit(EXIT_FAILURE);
182         }
183
184         owner = argv[optind];
185         ppa = argv[optind+1];
186
187         if (output_html)
188                 ppa_to_html(owner, ppa, package_status, output_dir);
189         else
190                 display_published_binaries(owner, ppa, package_status);
191
192         /* for valgrind.... */
193         free(package_status);
194         free(output_dir);
195         lp_ws_cleanup();
196         cache_cleanup();
197
198         exit(EXIT_SUCCESS);
199 }