1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <ctype.h>
21#include <unistd.h>
22#include <sys/vfs.h>
23#include <limits.h>
24#include <dirent.h>
25
26#include "usb_info.h"
27#include "disk_initial.h"
28#include "disk_share.h"
29
30#define VERSION 1
31
32#include <bcmnvram.h>
33#include <shutils.h>
34#include <shared.h>
35#include <stdarg.h>
36
37int main(int argc, char *argv[]){
38	char *command;
39	int i, num;
40	char **list;
41	int layer, right;
42	char *mount_path, *share;
43	int result;
44
45	if((command = rindex(argv[0], '/')) != NULL)
46		++command;
47	else
48		command = argv[0];
49
50	if(!strcmp(command, "set_file_integrity")){
51		if(argc != 2)
52			usb_dbg("Usage: set_file_integrity filename\n");
53		else{
54			set_file_integrity(argv[1]);
55		}
56	}
57	else if(!strcmp(command, "check_file_integrity")){
58		if(argc != 2)
59			usb_dbg("Usage: check_file_integrity filename\n");
60		else if(check_file_integrity(argv[1])){
61			usb_dbg("Ok.\n");
62		}
63		else{
64			usb_dbg("Broken.\n");
65		}
66	}
67	else if(!strcmp(command, "get_account_list")){
68		if(get_account_list(&num, &list) <= 0)
69			usb_dbg("Can't get account list.\n");
70		else{
71			for(i = 0; i < num; ++i)
72				usb_dbg("%dth account: %s.\n", i+1, list[i]);
73
74			free_2_dimension_list(&num, &list);
75		}
76	}
77	else if(!strcmp(command, "get_folder_list")){
78		if(argc != 2)
79			usb_dbg("Usage: get_folder_list mount_path\n");
80		else if(get_folder_list(argv[1], &num, &list) < 0)
81			usb_dbg("Can't get folder list.\n");
82		else{
83			for(i = 0; i < num; ++i)
84				usb_dbg("%dth folder: %s.\n", i+1, list[i]);
85
86			free_2_dimension_list(&num, &list);
87		}
88	}
89	else if(!strcmp(command, "get_all_folder")){
90		if(argc != 2)
91			usb_dbg("Usage: get_all_folder mount_path\n");
92		else if(get_all_folder(argv[1], &num, &list) < 0)
93			usb_dbg("Can't get all folder.\n");
94		else{
95			for(i = 0; i < num; ++i)
96				usb_dbg("%dth folder: %s.\n", i+1, list[i]);
97
98			free_2_dimension_list(&num, &list);
99		}
100	}
101	else if(!strcmp(command, "get_var_file_name")){
102		char *file_name;
103
104		if(argc != 3)
105			usb_dbg("Usage: get_var_file_name account mount_path\n");
106		else if(get_var_file_name(argv[1], argv[2], &file_name))
107			usb_dbg("Can't get the var file name with %s in %s.\n", argv[1], argv[2]);
108		else{
109			usb_dbg("done: %s.\n", file_name);
110			free(file_name);
111		}
112	}
113	else if(!strcmp(command, "initial_folder_list")){
114		if(argc != 2)
115			usb_dbg("Usage: initial_folder_list mount_path\n");
116		else if(initial_folder_list(argv[1]) < 0)
117			usb_dbg("Can't initial folder list in %s.\n", argv[1]);
118		else
119			usb_dbg("done.\n");
120	}
121	else if(!strcmp(command, "initial_var_file")){
122		char *target_acc = NULL;
123
124		if(argc != 3){
125			usb_dbg("Usage: initial_var_file account mount_path\n");
126			return -1;
127		}
128
129		if(strcmp(argv[1], "NULL"))
130			target_acc = argv[1];
131
132		if(initial_var_file(target_acc, argv[2]) < 0){
133			if(target_acc == NULL)
134				usb_dbg("Can't initial share's var file in %s.\n", argv[2]);
135			else
136				usb_dbg("Can't initial %s's var file in %s.\n", target_acc, argv[2]);
137		}
138		else
139			usb_dbg("done.\n");
140	}
141	else if(!strcmp(command, "initial_all_var_file")){
142		if(argc != 2)
143			usb_dbg("Usage: initial_all_var_file mount_path\n");
144		else if(initial_all_var_file(argv[1]) < 0)
145			usb_dbg("Can't initial all var file in %s.\n", argv[1]);
146		else
147			usb_dbg("done.\n");
148	}
149	else if(!strcmp(command, "test_of_var_files")){
150		if(argc != 2)
151			usb_dbg("Usage: test_of_var_files mount_path\n");
152		else if(test_of_var_files(argv[1]) < 0)
153			usb_dbg("Can't test_of_var_files in %s.\n", argv[1]);
154		else
155			usb_dbg("done.\n");
156	}
157	else if(!strcmp(command, "create_if_no_var_files")){
158		if(argc != 2)
159			usb_dbg("Usage: create_if_no_var_files mount_path\n");
160		else if(create_if_no_var_files(argv[1]) < 0)
161			usb_dbg("Can't create var files in %s.\n", argv[1]);
162		else
163			usb_dbg("done.\n");
164	}
165	else if(!strcmp(command, "modify_if_exist_new_folder")){
166		if(argc != 3)
167			usb_dbg("Usage: modify_if_exist_new_folder account mount_path\n");
168		else if(modify_if_exist_new_folder(argv[1], argv[2]) < 0)
169			usb_dbg("Can't fix %s's var files in %s.\n", argv[1], argv[2]);
170		else
171			usb_dbg("done.\n");
172	}
173	else if(!strcmp(command, "get_permission")){
174		if(argc != 5)
175			usb_dbg("Usage: get_permission account mount_path folder [cifs|ftp|dms]\n");
176		else if((right = get_permission(argv[1], argv[2], argv[3], argv[4])) < 0)
177			usb_dbg("%s can't get %s's %s permission in %s.\n", argv[1], argv[3], argv[4], argv[2]);
178		else
179			usb_dbg("done: %d.\n", right);
180	}
181	else if(!strcmp(command, "set_permission")){
182		if(argc != 6)
183			usb_dbg("Usage: set_permission account mount_path folder [cifs|ftp|dms] [0~3]\n");
184		else if(set_permission(argv[1], argv[2], argv[3], argv[4], atoi(argv[5])) < 0)
185			usb_dbg("%s can't set %s's %s permission to be %s in %s.\n", argv[1], argv[3], argv[4], argv[5], argv[2]);
186		else
187			usb_dbg("done.\n");
188	}
189	else if(!strcmp(command, "add_account")){
190		if(argc != 3)
191			usb_dbg("Usage: add_account account password\n");
192		else if(add_account(argv[1], argv[2]) < 0)
193			usb_dbg("Can't add account(%s:%s).\n", argv[1], argv[2]);
194		else
195			usb_dbg("done.\n");
196	}
197	else if(!strcmp(command, "del_account")){
198		if(argc != 2)
199			usb_dbg("Usage: del_account account\n");
200		else if(del_account(argv[1]) < 0)
201			usb_dbg("Can't del account(%s).\n", argv[1]);
202		else
203			usb_dbg("done.\n");
204	}
205	else if(!strcmp(command, "mod_account")){
206		if(argc != 4)
207			usb_dbg("Usage: mod_account account new_account new_password\n");
208		else if(mod_account(argv[1], argv[2], argv[3]) < 0)
209			usb_dbg("Can't mod account(%s) to (%s:%s).\n", argv[1], argv[2], argv[3]);
210		else
211			usb_dbg("done.\n");
212	}
213	else if(!strcmp(command, "test_if_exist_account")){
214		if(argc != 2)
215			usb_dbg("Usage: test_if_exist_account account\n");
216		else if(test_if_exist_account(argv[1]) < 0)
217			usb_dbg("Can't test if %s is existed.\n", argv[1]);
218		else
219			usb_dbg("done.\n");
220	}
221	else if(!strcmp(command, "add_folder")){
222		if(argc != 4){
223			usb_dbg("Usage: add_folder account mount_path folder\n");
224			return 0;
225		}
226
227		if(!strcmp(argv[1], "NULL"))
228			result = add_folder(NULL, argv[2], argv[3]);
229		else
230			result = add_folder(argv[1], argv[2], argv[3]);
231		if(result < 0)
232			usb_dbg("Can't add folder(%s) in %s.\n", argv[3], argv[2]);
233		else
234			usb_dbg("done.\n");
235	}
236	else if(!strcmp(command, "del_folder")){
237		if(argc != 3)
238			usb_dbg("Usage: del_folder mount_path folder\n");
239		else if(del_folder(argv[1], argv[2]) < 0)
240			usb_dbg("Can't del folder(%s) in %s.\n", argv[2], argv[1]);
241		else
242			usb_dbg("done.\n");
243	}
244	else if(!strcmp(command, "mod_folder")){
245		if(argc != 4)
246			usb_dbg("Usage: mod_folder mount_path folder new_folder\n");
247		else if(mod_folder(argv[1], argv[2], argv[3]) < 0)
248			usb_dbg("Can't mod folder(%s) to (%s) in %s.\n", argv[2], argv[3], argv[1]);
249		else
250			usb_dbg("done.\n");
251	}
252	else if(!strcmp(command, "test_if_exist_share")){
253		if(argc != 3)
254			usb_dbg("Usage: test_if_exist_share mount_path folder\n");
255		else if(test_if_exist_share(argv[1], argv[2]))
256			usb_dbg("%s is existed in %s.\n", argv[2], argv[1]);
257		else
258			usb_dbg("%s is NOT existed in %s.\n", argv[2], argv[1]);
259	}
260	else if(!strcmp(command, "how_many_layer")){
261		if(argc != 2)
262			usb_dbg("Usage: how_many_layer path\n");
263		else if((layer = how_many_layer(argv[1], &mount_path, &share)) < 0)
264			usb_dbg("Can't count the layer with %s.\n", argv[1]);
265		else
266			usb_dbg("done: %d layers, share=%s, mount_path=%s.\n", layer, share, mount_path);
267	}
268	else if(!strcmp(command, "test_size")){
269		unsigned long file_size = f_size(argv[1]);
270		_dprintf("size: %lu.\n", file_size);
271	}
272	else
273		usb_dbg("test_share(ver. %d): Need to link the command name from test_share first.\n", VERSION);
274
275	return 0;
276}
277