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