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