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