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