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
12
13if [ "x$VERBOSE" == "x" ]; then
14VERBOSE=0
15fi
16
17if [ "x$MEGACMDSHELL" != "x" ]; then
18PUT="executeinMEGASHELL put"
19CMDSHELL=1
20fi
21
22
23function executeinMEGASHELL()
24{
25	command=$1
26	shift;
27	echo lcd "$PWD" > /tmp/shellin
28	echo $command "$@" >> /tmp/shellin
29	#~ echo $command "$@" > /tmp/shellin
30
31	$MEGACMDSHELL < /tmp/shellin  | sed "s#^.*\[K##g" | grep $MEGA_EMAIL -A 1000 | grep -v $MEGA_EMAIL
32}
33
34ABSPWD=`pwd`
35
36function clean_all() {
37	if [[ $(mega-whoami) != *"$MEGA_EMAIL" ]]; then
38	mega-logout || :
39	mega-login $MEGA_EMAIL $MEGA_PWD || exit -1
40	fi
41	$RM -rf "*" > /dev/null
42	$RM -rf "//bin/*" > /dev/null
43
44	if [ -e localUPs ]; then rm -r localUPs; fi
45	if [ -e localtmp ]; then rm -r localtmp; fi
46
47	if [ -e megafind.txt ]; then rm megafind.txt; fi
48	if [ -e localfind.txt ]; then rm localfind.txt; fi
49}
50
51function clear_local_and_remote() {
52	rm -r localUPs/* 2>/dev/null
53	$RM -rf /01/../* >/dev/null 2>/dev/null || :
54	initialize_contents
55
56}
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
82	clear_local_and_remote
83	currentTest=$((currentTest+1))
84	$CD /
85}
86
87function check_failed_and_clear() {
88				if [ "$?" != "0" ]; then
89					echo "test $currentTest succesful!"
90				else
91					echo "test $currentTest failed!"
92					exit 1
93				fi
94
95				clear_local_and_remote
96				currentTest=$((currentTest+1))
97				$CD /
98}
99
100function initialize () {
101	if [[ $(mega-whoami) != *"$MEGA_EMAIL" ]]; then
102		mega-logout || :
103		mega-login $MEGA_EMAIL $MEGA_PWD || exit -1
104	fi
105
106	if [ "$(ls -A .)" ]; then
107		echo "initialization folder not empty!"
108		exit 1
109	fi
110
111	if [ $($FIND / | wc  -l) != 1 ]; then
112		echo "REMOTE Not empty, please clear it before starting!"
113		exit 1
114	fi
115
116	#initialize localtmp estructure:
117	mkdir -p localtmp
118	touch localtmp/file01.txt
119	echo file01contents > localtmp/file01nonempty.txt
120	#local empty folders structure
121	mkdir -p localtmp/le01/{les01/less01,les02/less0{1,2}}
122	#local filled folders structure
123	mkdir -p localtmp/lf01/{lfs01/lfss01,lfs02/lfss0{1,2}}
124	touch localtmp/lf01/{lfs01/lfss01,lfs02/lfss0{1,2}}/commonfile.txt
125	#spaced structure
126	mkdir -p localtmp/ls\ 01/{ls\ s01/ls\ ss01,ls\ s02/ls\ ss0{1,2}}
127	touch localtmp/ls\ 01/{ls\ s01/ls\ ss01,ls\ s02/ls\ ss0{1,2}}/common\ file.txt
128
129	# localtmp/
130	# ├── file01nonempty.txt
131	# ├── file01.txt
132	# ├── le01
133	# │   ├── les01
134	# │   │   └── less01
135	# │   └── les02
136	# │       ├── less01
137	# │       └── less02
138	# ├── lf01
139	# │   ├── lfs01
140	# │   │   └── lfss01
141	# │   │       └── commonfile.txt
142	# │   └── lfs02
143	# │       ├── lfss01
144	# │       │   └── commonfile.txt
145	# │       └── lfss02
146	# │           └── commonfile.txt
147	# └── ls 01
148		# ├── ls s01
149		# │   └── ls ss01
150		# │       └── common file.txt
151		# └── ls s02
152			# ├── ls ss01
153			# │   └── common file.txt
154			# └── ls ss02
155				# └── common file.txt
156
157
158	#initialize dynamic contents:
159	clear_local_and_remote
160}
161
162function initialize_contents() {
163	$MKDIR -p 01/{s01/ss01,s02/ss0{1,2}}
164	mkdir -p localUPs/01/{s01/ss01,s02/ss0{1,2}}
165}
166
167if [ "$MEGA_EMAIL" == "" ] || [ "$MEGA_PWD" == "" ]; then
168	echo "You must define variables MEGA_EMAIL MEGA_PWD. WARNING: Use an empty account for $MEGA_EMAIL"
169	cd $ABSPWD
170	exit 1
171fi
172
173#INITIALIZATION
174clean_all
175initialize
176
177ABSMEGADLFOLDER=$PWD/megaDls
178
179clear_local_and_remote
180
181#Test 01 #clean comparison
182compare_and_clear
183
184#Test 02 #no destiny empty file upload
185$PUT localtmp/file01.txt
186cp localtmp/file01.txt localUPs/
187compare_and_clear
188
189#Test 03 #/ destiny empty file upload
190$PUT localtmp/file01.txt /
191cp localtmp/file01.txt localUPs/
192compare_and_clear
193
194#Test 04 #no destiny nont empty file upload
195$PUT localtmp/file01nonempty.txt
196cp localtmp/file01nonempty.txt localUPs/
197compare_and_clear
198
199#Test 05 #empty folder
200$PUT localtmp/le01/les01/less01
201cp -r localtmp/le01/les01/less01 localUPs/
202compare_and_clear
203
204#Test 06 #1 file folder
205$PUT localtmp/lf01/lfs01/lfss01
206cp -r localtmp/lf01/lfs01/lfss01 localUPs/
207compare_and_clear
208
209#Test 07 #entire empty folders structure
210$PUT localtmp/le01
211cp -r localtmp/le01 localUPs/
212compare_and_clear
213
214#Test 08 #entire non empty folders structure
215$PUT localtmp/lf01
216cp -r localtmp/lf01 localUPs/
217compare_and_clear
218
219#Test 09 #copy structure into subfolder
220$PUT localtmp/le01 /01/s01
221cp -r localtmp/le01 localUPs/01/s01
222compare_and_clear
223
224#~ #Test 10 #copy exact structure
225mkdir aux
226cp -pr localUPs/01 aux
227$PUT aux/01/s01 /01/s01
228cp -r aux/01/s01 localUPs/01/s01
229rm -r aux
230compare_and_clear
231
232#Test 11 #merge increased structure
233mkdir aux
234cp -pr localUPs/01 aux
235touch aux/01/s01/another.txt
236$PUT aux/01/s01 /01/
237rsync -aLp aux/01/s01/ localUPs/01/s01/
238rm -r aux
239compare_and_clear
240
241#Test 12 #multiple upload
242$PUT localtmp/le01 localtmp/lf01 /01/s01
243cp -r localtmp/le01 localtmp/lf01 localUPs/01/s01
244compare_and_clear
245
246#Test 13 #local regexp
247$PUT localtmp/*txt /01/s01
248cp -r localtmp/*txt localUPs/01/s01
249compare_and_clear
250
251#Test 14 #../
252$CD 01
253$PUT localtmp/le01 ../01/s01
254$CD /
255cp -r localtmp/le01 localUPs/01/s01
256compare_and_clear
257
258currentTest=15
259
260#Test 15 #spaced stuff
261if [ "x$CMDSHELL" != "x1" ]; then #TODO: think about this again
262$PUT localtmp/ls\ 01
263else
264$PUT "localtmp/ls\ 01"
265fi
266
267cp -r localtmp/ls\ 01 localUPs/ls\ 01
268compare_and_clear
269
270
271###TODO: do stuff in shared folders...
272
273########
274
275##Test XX #merge structure with file updated
276##This test fails, because it creates a remote copy of the updated file.
277##That's expected. If ever provided a way to create a real merge (e.g.: put -m ...)  to do BACKUPs, reuse this test.
278#mkdir aux
279#cp -pr localUPs/01 aux
280#touch aux/01/s01/another.txt
281#$PUT aux/01/s01 /01/
282#rsync -aLp aux/01/s01/ localUPs/01/s01/
283#echo "newcontents" > aux/01/s01/another.txt
284#$PUT aux/01/s01 /01/
285#rsync -aLp aux/01/s01/ localUPs/01/s01/
286#rm -r aux
287#compare_and_clear
288
289
290# Clean all
291if [ "$VERBOSE" != "1" ]; then
292clean_all
293fi
294