Remove trailing whitespace. Sorry if this breaks your patches.
[oweals/opkg-lede.git] / tests / libopkg_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <libgen.h>
5
6 #include <opkg.h>
7
8 pkg_t *find_pkg = NULL;
9
10
11 #define TEST_PACKAGE "aspell"
12
13 void
14 progress_callback (const opkg_progress_data_t *progress, void *data)
15 {
16   printf ("\r%s %3d%%\n", (char*) data, progress->percentage);
17   fflush (stdout);
18 }
19
20 static void list_pkg(pkg_t *pkg)
21 {
22   char *v = pkg_version_str_alloc(pkg);
23   printf ("%s - %s\n", pkg->name, v);
24   free(v);
25 }
26
27 void
28 package_list_installed_callback (pkg_t *pkg, void *data)
29 {
30   if (pkg->state_status == SS_INSTALLED)
31     list_pkg(pkg);
32 }
33
34 void
35 package_list_callback (pkg_t *pkg, void *data)
36 {
37   static int install_count = 0;
38   static int total_count = 0;
39
40   if (pkg->state_status == SS_INSTALLED)
41     install_count++;
42
43   total_count++;
44
45   printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
46   fflush (stdout);
47
48   if (!find_pkg)
49   {
50     /* store the first package to print out later */
51     find_pkg = pkg;
52   }
53 }
54
55 void
56 package_list_upgradable_callback (pkg_t *pkg, void *data)
57 {
58   list_pkg(pkg);
59 }
60
61 void
62 print_package (pkg_t *pkg)
63 {
64   char *v = pkg_version_str_alloc(pkg);
65   printf (
66       "Name:         %s\n"
67       "Version:      %s\n"
68       "Repository:   %s\n"
69       "Architecture: %s\n"
70       "Description:  %s\n"
71       "Tags:         %s\n"
72       "Size:         %ld\n"
73       "Status:       %d\n",
74       pkg->name,
75       v,
76       pkg->src->name,
77       pkg->architecture,
78       pkg->description,
79       pkg->tags? pkg->tags : "",
80       pkg->size,
81       pkg->state_status);
82   free(v);
83 }
84
85
86 void
87 opkg_test (void)
88 {
89   int err;
90   pkg_t *pkg;
91
92   err = opkg_update_package_lists (progress_callback, "Updating...");
93   printf ("\nopkg_update_package_lists returned %d\n", err);
94
95   opkg_list_packages (package_list_callback, NULL);
96   printf ("\n");
97
98   if (find_pkg)
99   {
100     printf ("Finding package \"%s\"\n", find_pkg->name);
101     pkg = opkg_find_package (find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->src->name);
102     if (pkg)
103     {
104       print_package (pkg);
105     }
106     else
107       printf ("Package \"%s\" not found!\n", find_pkg->name);
108   }
109   else
110     printf ("No package available to test find_package.\n");
111
112   err = opkg_install_package (TEST_PACKAGE, progress_callback, "Installing...");
113   printf ("\nopkg_install_package returned %d\n", err);
114
115   err = opkg_upgrade_package (TEST_PACKAGE, progress_callback, "Upgrading...");
116   printf ("\nopkg_upgrade_package returned %d\n", err);
117
118   err = opkg_remove_package (TEST_PACKAGE, progress_callback, "Removing...");
119   printf ("\nopkg_remove_package returned %d\n", err);
120
121   printf ("Listing upgradable packages...\n");
122   opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
123
124   err = opkg_upgrade_all (progress_callback, "Upgrading all...");
125   printf ("\nopkg_upgrade_all returned %d\n", err);
126
127 }
128
129 int
130 main (int argc, char **argv)
131 {
132   pkg_t *pkg;
133   int err;
134
135   if (argc < 2)
136   {
137     printf ("Usage: %s command\n"
138             "\nCommands:\n"
139             "\tupdate - Update package lists\n"
140             "\tfind [package] - Print details of the specified package\n"
141             "\tinstall [package] - Install the specified package\n"
142             "\tupgrade [package] - Upgrade the specified package\n"
143             "\tlist upgrades - List the available upgrades\n"
144             "\tlist all - List all available packages\n"
145             "\tlist installed - List all the installed packages\n"
146             "\tremove [package] - Remove the specified package\n"
147             "\trping - Reposiroties ping, check the accessibility of repositories\n"
148             "\ttest - Run test script\n"
149     , basename (argv[0]));
150     exit (0);
151   }
152
153   setenv("OFFLINE_ROOT", "/tmp", 0);
154
155   if (opkg_new ()) {
156           printf("opkg_new() failed. This sucks.\n");
157           print_error_list();
158           return 1;
159   }
160
161   switch (argv[1][0])
162   {
163     case 'f':
164       pkg = opkg_find_package (argv[2], NULL, NULL, NULL);
165       if (pkg)
166       {
167         print_package (pkg);
168       }
169       else
170         printf ("Package \"%s\" not found!\n", find_pkg->name);
171       break;
172     case 'i':
173       err = opkg_install_package (argv[2], progress_callback, "Installing...");
174       printf ("\nopkg_install_package returned %d\n", err);
175       break;
176
177     case 'u':
178       if (argv[1][2] == 'd')
179       {
180         err = opkg_update_package_lists (progress_callback, "Updating...");
181         printf ("\nopkg_update_package_lists returned %d\n", err);
182         break;
183       }
184       else
185       {
186         if (argc < 3)
187         {
188           err = opkg_upgrade_all (progress_callback, "Upgrading all...");
189           printf ("\nopkg_upgrade_all returned %d\n", err);
190         }
191         else
192         {
193           err = opkg_upgrade_package (argv[2], progress_callback, "Upgrading...");
194           printf ("\nopkg_upgrade_package returned %d\n", err);
195         }
196       }
197       break;
198
199     case 'l':
200       if (argc < 3)
201       {
202         printf ("Please specify one either all, installed or upgrades\n");
203       }
204       else
205       {
206         switch (argv[2][0])
207         {
208           case 'u':
209             printf ("Listing upgradable packages...\n");
210             opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
211             break;
212           case 'a':
213             printf ("Listing all packages...\n");
214             opkg_list_packages (package_list_callback, NULL);
215             printf ("\n");
216             break;
217           case 'i':
218             printf ("Listing installed packages...\n");
219             opkg_list_packages (package_list_installed_callback, NULL);
220             break;
221           default:
222             printf ("Unknown list option \"%s\"\n", argv[2]);
223         }
224       }
225       break;
226
227     case 'r':
228       if (argv[1][1] == 'e')
229       {
230         err = opkg_remove_package (argv[2], progress_callback, "Removing...");
231         printf ("\nopkg_remove_package returned %d\n", err);
232         break;
233       }else if (argv[1][1] == 'p')
234       {
235         err = opkg_repository_accessibility_check();
236         printf("\nopkg_repository_accessibility_check returned (%d)\n", err);
237         break;
238       }
239
240     default:
241       printf ("Unknown command \"%s\"\n", argv[1]);
242   }
243
244
245   opkg_free ();
246
247   return 0;
248 }