1#!/bin/bash
2
3GET=mega-get
4PUT=mega-put
5RM=mega-rm
6CD=mega-cd
7LCD=mega-lcd
8MKDIR=mega-mkdir
9EXPORT=mega-export
10FIND=mega-find
11
12if [ "x$VERBOSE" == "x" ]; then
13VERBOSE=0
14fi
15
16if [ "x$MEGACMDSHELL" != "x" ]; then
17FIND="executeinMEGASHELL find"
18CMDSHELL=1
19fi
20
21
22function executeinMEGASHELL()
23{
24	command=$1
25	shift;
26	echo $command "$@" > /tmp/shellin
27
28	$MEGACMDSHELL < /tmp/shellin  | sed "s#^.*\[K##g" | grep $MEGA_EMAIL -A 1000 | grep -v $MEGA_EMAIL
29}
30
31
32
33function clean_all() {
34	if [[ $(mega-whoami) != *"$MEGA_EMAIL" ]]; then
35	mega-logout || :
36	mega-login $MEGA_EMAIL $MEGA_PWD || exit -1
37	fi
38
39	rm pipe > /dev/null 2>/dev/null || :
40	$RM -rf "*" > /dev/null
41	$RM -rf "//bin/*" > /dev/null
42
43	if [ -e localUPs ]; then rm -r localUPs; fi
44	if [ -e localtmp ]; then rm -r localtmp; fi
45
46	if [ -e megafind.txt ]; then rm megafind.txt; fi
47	if [ -e localfind.txt ]; then rm localfind.txt; fi
48}
49
50function clear_local_and_remote() {
51	rm -r localUPs/* 2>/dev/null
52	$RM -rf "/*" 2>/dev/null || :
53	initialize_contents()
54}
55
56ABSPWD=`pwd`
57
58currentTest=1
59
60function compare_and_clear() {
61	if [ "$VERBOSE" == "1" ]; then
62		echo "test $currentTest"
63	fi
64		$FIND | sort > megafind.txt
65		(cd localUPs; find | sed "s#\./##g" | sort) > localfind.txt
66
67	if diff --side-by-side megafind.txt localfind.txt 2>/dev/null >/dev/null; then
68		if [ "$VERBOSE" == "1" ]; then
69			echo "diff megafind vs localfind:"
70			diff --side-by-side megafind.txt localfind.txt
71		fi
72		echo "test $currentTest succesful!"
73	else
74		echo "test $currentTest failed!"
75		echo "diff megafind vs localfind:"
76		diff --side-by-side megafind.txt localfind.txt
77		cd $ABSPWD
78		exit 1
79	fi
80
81	clear_local_and_remote
82	currentTest=$((currentTest+1))
83	$CD /
84}
85
86function check_failed_and_clear() {
87if [ "$?" != "0" ]; then
88	echo "test $currentTest succesful!"
89else
90	echo "test $currentTest failed!"
91	cd $ABSPWD
92	exit 1
93fi
94
95clear_local_and_remote
96currentTest=$((currentTest+1))
97$CD /
98}
99
100function initialize () {
101
102	if [[ $(mega-whoami) != *"$MEGA_EMAIL" ]]; then
103	mega-logout || :
104	mega-login $MEGA_EMAIL $MEGA_PWD || exit -1
105	fi
106
107	if [ "$(ls -A .)" ]; then
108		echo "initialization folder not empty!"
109		cd $ABSPWD
110		exit 1
111	fi
112
113	if [ $($FIND / | grep -v "^$" |wc  -l) != 1 ]; then
114		echo "REMOTE Not empty, please clear it before starting!"
115		#~ $FIND /
116		cd $ABSPWD
117		exit 1
118	fi
119
120	#initialize localtmp estructure:
121	mkdir -p localtmp
122	touch localtmp/file01.txt
123	echo file01contents > localtmp/file01nonempty.txt
124	#local empty folders structure
125	mkdir -p localtmp/le01/{les01/less01,les02/less0{1,2}}
126	#local filled folders structure
127	mkdir -p localtmp/lf01/{lfs01/lfss01,lfs02/lfss0{1,2}}
128	touch localtmp/lf01/{lfs01/lfss01,lfs02/lfss0{1,2}}/commonfile.txt
129	#spaced structure
130	mkdir -p localtmp/ls\ 01/{ls\ s01/ls\ ss01,ls\ s02/ls\ ss0{1,2}}
131	touch localtmp/ls\ 01/{ls\ s01/ls\ ss01,ls\ s02/ls\ ss0{1,2}}/common\ file.txt
132
133	# localtmp/
134	# ├── file01nonempty.txt
135	# ├── file01.txt
136	# ├── le01
137	# │   ├── les01
138	# │   │   └── less01
139	# │   └── les02
140	# │       ├── less01
141	# │       └── less02
142	# ├── lf01
143	# │   ├── lfs01
144	# │   │   └── lfss01
145	# │   │       └── commonfile.txt
146	# │   └── lfs02
147	# │       ├── lfss01
148	# │       │   └── commonfile.txt
149	# │       └── lfss02
150	# │           └── commonfile.txt
151	# └── ls 01
152		# ├── ls s01
153		# │   └── ls ss01
154		# │       └── common file.txt
155		# └── ls s02
156			# ├── ls ss01
157			# │   └── common file.txt
158			# └── ls ss02
159				# └── common file.txt
160
161	#initialize dynamic contents:
162	clear_local_and_remote
163}
164
165function initialize_contents() {
166	$PUT localtmp/* /
167	mkdir -p localUPs
168	cp -r localtmp/* localUPs/
169}
170
171function compare_find(){
172	if [ "$VERBOSE" == "1" ]; then
173		echo "test $currentTest"
174	fi
175
176	$FIND "$@"  | sort > $ABSPWD/megafind.txt
177	(cd localUPs 2>/dev/null; find "$@" | sed "s#\./##g" | sort) > $ABSPWD/localfind.txt
178
179
180
181	if diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt 2>/dev/null >/dev/null; then
182		if [ "$VERBOSE" == "1" ]; then
183			echo "diff megafind vs localfind:"
184			diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt
185		fi
186		echo "test $currentTest succesful!"
187	else
188		echo "test $currentTest failed!"
189		echo "diff megafind vs localfind:"
190		diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt
191		cd $ABSPWD
192		exit 1
193	fi
194
195	currentTest=$((currentTest+1))
196}
197
198
199function find_remote(){
200	if [ "$VERBOSE" == "1" ]; then
201		echo "test $currentTest"
202	fi
203
204	$FIND "$@"  | sort > $ABSPWD/megafind.txt
205}
206
207function find_local(){
208  if [ "Darwin" == `uname` ]; then
209    (cd localUPs 2>/dev/null; find "$@" | sed "s#\.///#/#g"  | sed "s#\.//#/#g" | sort) > $ABSPWD/localfind.txt
210  else
211    (cd localUPs 2>/dev/null; find "$@" | sed "s#\./##g" | sort) > $ABSPWD/localfind.txt
212  fi
213}
214
215function find_local_append(){
216	(cd localUPs 2>/dev/null; find "$@" | sed "s#\./##g" | sort) >> $ABSPWD/localfind.txt
217}
218
219function compare_remote_local(){
220
221		if diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt 2>/dev/null >/dev/null; then
222		if [ "$VERBOSE" == "1" ]; then
223			echo "diff megafind vs localfind:"
224			diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt
225		fi
226		echo "test $currentTest succesful!"
227	else
228		echo "test $currentTest failed!"
229		echo "diff megafind vs localfind:"
230		diff -B --side-by-side $ABSPWD/megafind.txt $ABSPWD/localfind.txt
231		cd $ABSPWD
232		exit 1
233	fi
234
235	currentTest=$((currentTest+1))
236}
237
238
239if [ "$MEGA_EMAIL" == "" ] || [ "$MEGA_PWD" == "" ]; then
240	echo "You must define variables MEGA_EMAIL MEGA_PWD. WARNING: Use an empty account for $MEGA_EMAIL"
241	cd $ABSPWD
242	exit 1
243fi
244
245#INITIALIZATION
246clean_all
247initialize
248
249ABSMEGADLFOLDER=$PWD/megaDls
250
251#Test 01 #destiny empty file
252compare_find file01.txt
253
254#Test 02  #entire empty folders
255compare_find le01/les01/less01
256
257#Test 03 #1 file folder
258compare_find lf01/lfs01/lfss01
259
260#Test 04 #entire non empty folders structure
261compare_find lf01
262
263#Test 05 #multiple
264compare_find lf01 le01/les01
265
266#Test 06 #.
267$CD le01
268pushd localUPs/le01 > /dev/null
269compare_find .
270$CD /
271popd > /dev/null
272
273#Test 07 #. global
274compare_find .
275
276#Test 08 # spaced
277find_remote "ls\ 01"
278find_local ls\ 01
279compare_remote_local
280
281#Test 09 #XX/..
282find_remote "ls\ 01/.."
283find_local .//
284compare_remote_local
285
286#Test 10 #..
287$CD le01
288find_remote ..
289$CD /
290find_local .//
291compare_remote_local
292
293#Test 11 #complex stuff
294#$RM -rf ls\ 01/../le01/les01 lf01/../ls*/ls\ s02 #This one fails since it is PCRE expresion TODO: if ever supported PCRE enabling/disabling, consider that
295find_remote "ls\ 01/../le01/les01" "lf01/../ls\ *01/ls\ s02"
296find_local .//le01/les01
297find_local_append .//ls\ 01/ls\ s02
298compare_remote_local
299
300
301#Test 12 #folder/
302find_remote le01/
303find_local le01
304compare_remote_local
305
306if [ "x$CMDSHELL" != "x1" ]; then #TODO: currently there is no way to know last CMSHELL status code
307
308	#Test 13 #file01.txt/non-existent
309	$FIND file01.txt/non-existent >/dev/null 2>/dev/null
310	if [ $? == 0 ]; then echo "test $currentTest failed!"; cd $ABSPWD; exit 1;
311	else echo "test $currentTest succesful!"; fi
312
313fi
314
315currentTest=14
316
317#Test 14 #/
318find_remote /
319find_local .//
320compare_remote_local
321###TODO: do stuff in shared folders...
322
323########
324
325# Clean all
326if [ "$VERBOSE" != "1" ]; then
327clean_all
328fi
329