added --debug option
[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 binary_package_publishing_history **e_cur, **list;
37         char *archive_url = get_archive_url(owner, ppa);
38
39         list = get_binary_package_publishing_history_list(archive_url);
40         free(archive_url);
41
42         if (!list) {
43                 fprintf(stderr, "Failed to retrieve PPA information\n");
44                 exit(EXIT_FAILURE);
45         }
46
47         e_cur = list;
48         while (*e_cur) {
49                 struct binary_package_publishing_history *binary;
50                 const struct distro_arch_series *arch;
51                 const struct distro_series *distro;
52
53                 binary = *e_cur;
54                 arch = get_distro_arch_series(binary->distro_arch_series_link);
55                 distro = get_distro_series(arch->distroseries_link);
56
57                 printf("%s %s %s(%s): %d\n",
58                        binary->binary_package_name,
59                        binary->binary_package_version,
60                        distro->name,
61                        arch->architecture_tag,
62                        get_download_count(binary->self_link));
63                 e_cur++;
64         }
65
66         binary_package_publishing_history_list_free(list);
67 }
68
69 static struct option long_options[] = {
70         {"version", no_argument, 0, 'v'},
71         {"help", no_argument, 0, 'h'},
72         {"html", no_argument, 0, 't'},
73         {"debug", no_argument, 0, 'd'},
74         {0, 0, 0, 0}
75 };
76
77 static void print_version()
78 {
79         printf("ppastats %s\n", VERSION);
80         printf("Copyright (C) %s jeanfi@gmail.com\n\
81 License GPLv2: GNU GPL version 2 or later \
82 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
83 This is free software: you are free to change and redistribute it.\n\
84 There is NO WARRANTY, to the extent permitted by law.\n",
85                "2010-2011");
86 }
87
88 static void print_help()
89 {
90         printf("Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n", program_name);
91
92         puts("ppastats is a command application"
93              " for generating PPA statistics.\n");
94
95         puts("Prints number of downloads for each published packages of a PPA "
96              "or generates an HTML page containing a graph representation.");
97
98         puts("");
99         puts("Options:");
100         puts("\
101   -h, --help          display this help and exit\n\
102   -v, --version       display version information and exit");
103
104         puts("");
105
106         puts("\
107   -t, --html       \
108 generates an HTML output.");
109
110         puts("");
111
112         printf("Report bugs to: %s\n", PACKAGE_BUGREPORT);
113         puts("");
114         printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
115 }
116
117
118 int main(int argc, char **argv)
119 {
120         char *owner, *ppa;
121         int optc;
122         int output_html = 0;
123         int cmdok = 1;
124
125         program_name = argv[0];
126
127         while ((optc = getopt_long(argc, argv, "vhtd", long_options,
128                                    NULL)) != -1) {
129                 switch (optc) {
130                 case 't':
131                         output_html = 1;
132                         break;
133                 case 'd':
134                         debug = 1;
135                         break;
136                 case 'h':
137                         print_help();
138                         exit(EXIT_SUCCESS);
139                 case 'v':
140                         print_version();
141                         exit(EXIT_SUCCESS);
142                 default:
143                         cmdok = 0;
144                         break;
145                 }
146         }
147
148         if (!cmdok || optind + 2 != argc) {
149                 fprintf(stderr, "Try `%s --help' for more information.\n",
150                         program_name);
151                 exit(EXIT_FAILURE);
152         }
153
154         owner = argv[optind];
155         ppa = argv[optind+1];
156
157         if (output_html)
158                 ppa_to_html(owner, ppa);
159         else
160                 display_published_binaries(owner, ppa);
161
162         /* for valgrind.... */
163         lp_ws_cleanup();
164         cache_cleanup();
165
166         exit(EXIT_SUCCESS);
167 }