#!/bin/sh

# $Id: tests.sh,v 1.39 2006/10/29 04:13:25 gilles Exp gilles $	

#### Shell pragmas

exec 3>&2
#set -x # debug mode. See what is running
set -e # exit on first failure


#### functions definitions

echo3() {
	#echo '#####################################################' >&3
	echo "$*" >&3
}

run_test() {
	echo3 "#### $test_count $1"
	$1
	if test x"$?" = x"0"; then
 		echo "$1 passed"
	else
		echo "$1 failed" >&2
	fi
}

run_tests() {
	for t in $*; do
		test_count=`expr 1 + $test_count`
		run_test $t
		#sleep 1
	done
}


#### Variable definitions

ftpsync=./ftpsync
host=localhost
host_rmt=beaute
passfile=/var/tmp/secret
user=gilles
dirtest=/tmp/ftpsync/test

test_count=0

##### The tests functions

perl_syntax() {
	perl -c ./ftpsync
}


no_args() {
	./ftpsync
}

cleaning_test_directory() {
	test -d $dirtest && find  $dirtest -type d -print0| xargs -0 chmod 700
	rm -rf $dirtest
	mkdir -p $dirtest/d1/
}

first_sync() {
	./ftpsync --host $host --passfile $passfile --user $user \
		t/d1 $dirtest/d1
	diff -r t/d1 $dirtest/d1
}

a_simple_file() {
	./ftpsync --host $host --passfile $passfile --user $user  \
		./Makefile $dirtest
	diff  ./Makefile $dirtest/Makefile
}

a_simple_file_2() {
	./ftpsync --host $host --passfile $passfile --user $user  \
		./t/d2/f2 $dirtest
	diff  ./t/d2/f2 $dirtest/f2
	rm -f $dirtest/f2
}

a_simple_file_3() {
	echo blabla1 > ./t/d2/f3
	./ftpsync --host $host --passfile $passfile --user $user --debug \
		./t/d2/f3 $dirtest
	diff  ./t/d2/f3 $dirtest/f3
	sleep 2
	echo blabla2 > ./t/d2/f3
	./ftpsync --host $host --passfile $passfile --user $user --debug \
		./t/d2/f3 $dirtest
	diff  ./t/d2/f3 $dirtest/f3
	rm -f $dirtest/f3
}


sizeonly_1() {
	# remote no exists => transfer
	echo blabla1 > ./t/d2/s3
	./ftpsync --host $host --passfile $passfile --user $user --debug \
                --sizeonly \
		./t/d2/s3 $dirtest
	diff  ./t/d2/s3 $dirtest/s3
	sleep 2
	# remote exists, same size => no transfer
	echo blabla2 > ./t/d2/s3
	./ftpsync --host $host --passfile $passfile --user $user --debug \
                --sizeonly \
		./t/d2/s3 $dirtest
	# files must differ
	! diff  ./t/d2/s3 $dirtest/s3
	rm -f $dirtest/s3
}




second_sync() {
	./ftpsync --host $host --passfile $passfile --user $user \
		t/d1 $dirtest/d1
	diff -r t/d1 $dirtest/d1
}

third_sync() {
	./ftpsync --host $host --passfile $passfile --user $user \
		t/d1/./. $dirtest/d1
	diff -r t/d1 $dirtest/d1
}

uri_dest() {
	mkdir -p $dirtest/d2
	./ftpsync --passfile $passfile --user $user \
		t/d2 ftp://$host/$dirtest/d2
	diff -r t/d2 $dirtest/d2
}

change_a_dir_to_a_file() {
	rm -rf $dirtest/d2/d22
	touch  $dirtest/d2/d22
	./ftpsync --passfile $passfile --user $user \
		t/d2 ftp://$host/$dirtest/d2
	diff -r t/d2 $dirtest/d2
}

change_a_file_to_a_dir() {
	rm -rf $dirtest/d2
	mkdir -p $dirtest/d2/f2
	./ftpsync --passfile $passfile --user $user \
		t/d2 ftp://$host/$dirtest/d2
	diff -r t/d2 $dirtest/d2
}

file_permission_denied() {
	> $dirtest/Makefile # make empty
	chmod 000 $dirtest/Makefile
	! ./ftpsync --host $host --passfile $passfile --user $user  \
		./Makefile $dirtest
	test -f  $dirtest/Makefile
	! test -s  $dirtest/Makefile
	rm -f $dirtest/Makefile
}

directory_permission_denied() {
	chmod 000 $dirtest/d1/d11
	! ./ftpsync --host $host --passfile $passfile --user $user  \
		./t/d1 $dirtest/d1
	chmod 700 $dirtest/d1/d11
}


la_the_totale() {
	rm -rf $dirtest/d2
	rm -rf $dirtest/d1
	./ftpsync --host $host --passfile $passfile --user $user  \
		. $dirtest

	diff -r . $dirtest
}

passive_mode() {
	rm -rf  $dirtest/t/d1
	FTP_PASSIVE=1 ./ftpsync --host $host --passfile $passfile --user $user  \
		. $dirtest

	diff -r . $dirtest
}


option_norecursive() {
	rm -rf $dirtest/d1/*
	mkdir -p $dirtest/d1
	./ftpsync --host localhost --passfile /var/tmp/secret --user gilles \
		--norecursive \
		t/d1 /tmp/ftpsync/test/d1

	diff t/d1 /tmp/ftpsync/test/d1
	! diff -r t/d1 /tmp/ftpsync/test/d1 # prune d1/d11
}

recursive_delete_1() {
	./ftpsync --host $host --passfile $passfile --user $user  \
	--del  $dirtest/d1/d11

	! ls  $dirtest/d1/d11 2>/dev/null || false
}

recursive_delete_2() {
	./ftpsync --host $host --passfile $passfile --user $user  \
	--del  $dirtest/d1/d11 --del  $dirtest/d1/d12 

	! ls  $dirtest/d1/d11 2>/dev/null || false
	! ls  $dirtest/d1/d12 2>/dev/null || false
}

recursive_delete_3() {
	./ftpsync --host $host --passfile $passfile --user $user  \
	--del  $dirtest

	! ls  $dirtest 2>/dev/null || false
}

empty_dir_bug() {
	mkdir -p $dirtest
	./ftpsync --passfile $passfile --user $user \
	t/d1/ ftp://$host/$dirtest
}


no_dest_dir() {
	rm -rf $dirtest/noexist
	./ftpsync --passfile $passfile --user $user \
	t/d1 ftp://$host/$dirtest/noexist
}

no_dest_dir2() {
	rm -rf $dirtest/noexist
	! ./ftpsync --passfile $passfile --user $user \
	t/d1 ftp://$host/$dirtest/noexist/blurgzch
}

sync_delete_1() {
	touch ./t/d2/f3
	mkdir ./t/d2/d3333
	./ftpsync --passfile $passfile --user $user \
	t/d2 ftp://$host/$dirtest/d2
	test -f $dirtest/d2/f3
	rm -f t/d2/f3
	rmdir ./t/d2/d3333
	./ftpsync --passfile $passfile --user $user --delete \
	t/d2 ftp://$host/$dirtest/d2
	test -f $dirtest/d2/f2
	! test -f $dirtest/d2/f3
	! test -d $dirtest/d2/d3333
}



sync_delete_2() {
	rm -rf $dirtest/d1
	cp -a t/d1/d11 t/d1/d13
	./ftpsync --passfile $passfile --user $user \
	t/d1 ftp://$host/$dirtest/d1
	diff -r  $dirtest/d1 t/d1
	rm -rf t/d1/d13
	mkdir  -p t/d1/d14
	echo yopopoy > t/d1/d14/f14
	! diff -r  $dirtest/d1 t/d1
	./ftpsync --passfile $passfile --user $user --delete \
	t/d1 ftp://$host/$dirtest/d1
	test -d $dirtest/d1/d11
	test -f $dirtest/d1/d14/f14
	! test -d $dirtest/d1/d13
}




date_diff_func() {
    
    host_dfc=${1:? need host};
    date_remo_str=`LANG=C rdate -p $host_dfc`
    date_loca_epo=`LANG=C date +%s`
    date_remo_epo=`date -d "$date_remo_str" +%s`
    echo date remote $date_remo_epo
    echo date local  $date_loca_epo
    date_diff=`expr $date_remo_epo - $date_loca_epo` || : # need success...
    if test $date_diff -gt 0
    then
	echo clock remote is in advance by $date_diff sec
	echo some files may not be updated
    else
	echo clock remote is late by $date_diff sec
	echo some files be updated even if not needed
    fi
    file_ddf=./ftpsync.date
    touch $file_ddf
    echo first sync
    ./ftpsync --host $host_dfc --passfile $passfile --user $user  \
	--debug \
	$file_ddf /tmp/
    echo second sync no transfert needed
#    mtime_r=`ssh root@loul 'stat -c %Z /tmp/ftpsync.date'`
#    mtime_l=`stat -c %Z ././ftpsync.date`
#    echo mtime_r=$mtime_r
#    echo mtime_l=$mtime_l
#    echo mtime_f=`expr $mtime_r - $date_diff`
    ./ftpsync --host $host_dfc --passfile $passfile --user $user  \
	--debug \
	$file_ddf /tmp/ \
	| grep 'up to date /tmp//ftpsync.date'
#    return
    sleep 1
    touch $file_ddf
#    ssh root@loul 'stat -c %Z /tmp/ftpsync.date'; 
#    stat -c %Z ././ftpsync.date
    echo third sync transfert needed
    ./ftpsync --host $host_dfc --passfile $passfile --user $user  \
	--debug \
	$file_ddf /tmp/ \
	| grep 'successful put to /tmp//ftpsync.date'
    

}

date_diff_1() {
    date_diff_func $host_rmt
}

date_diff_2() {
    date_diff_func $host
}

ftpsyncblock_1() {
	./ftpsync  --host $host --passfile $passfile --user $user \
	--excludelist t/excludelist.txt \
		t/d3 $dirtest/d3
	diff -r t/d3/d31 $dirtest/d3/d31
	! test -d $dirtest/d3/d32
	! test -f $dirtest/d3/F3
	diff -r t/d3/d33 $dirtest/d3/d33
	diff t/d3/F3b $dirtest/d3/F3b
	
}

ftpsyncblock_nolist() {
	! ./ftpsync --host $host --passfile $passfile --user $user \
	--excludelist t/excludelistnoexist.txt \
		t/d1 $dirtest/d1 2>&1
}



space_bug_1_file() {
    
	./ftpsync --host $host --passfile $passfile --user $user  \
		./t/d2/"f  3" $dirtest
	diff  ./t/d2/"f  3" $dirtest/"f  3"
	rm -f $dirtest/"f  3"
}


space_bug_2_dir() {
	
	./ftpsync --host $host --passfile $passfile --user $user  \
		./t/d2/d3 $dirtest
	test -d $dirtest/"d  3"
	rmdir $dirtest/"d  3"
}

space_bug_3_file_delete() {
        touch $dirtest"/f  3del"
	./ftpsync --host $host --passfile $passfile --user $user  \
		--delete ./t/d2/"f  3" $dirtest
	diff  ./t/d2/"f  3" $dirtest/"f  3"
	#rm -f $dirtest/"f  3"
}

space_bug_4_dir_delete() {
        mkdir $dirtest/"d  3del"
	./ftpsync --host $host --passfile $passfile --user $user  \
		--delete ./t/d2/"f  3" $dirtest
	! test -d  $dirtest/"d  3del"
	
}


pattern_1() {
	rm -rf $dirtest/d4
	./ftpsync --host $host --passfile $passfile --user $user  \
		 --pattern '^.*/[^/.][^/]+$' t/d4 $dirtest/d4

	test -f $dirtest/d4/toto.doc 
	test -f $dirtest/d4/toto
	! test -f $dirtest/d4/.toto
}



# mandatory tests

run_tests perl_syntax cleaning_test_directory

# All tests

test $# -eq 0 && run_tests \
	no_args  \
	first_sync \
	a_simple_file \
	a_simple_file_2 \
	a_simple_file_3\
	second_sync \
	third_sync \
	uri_dest \
	change_a_dir_to_a_file \
	change_a_file_to_a_dir \
	file_permission_denied \
	directory_permission_denied \
	la_the_totale \
	passive_mode \
	option_norecursive \
	recursive_delete_1 \
	recursive_delete_2 \
	recursive_delete_3 \
	empty_dir_bug \
	no_dest_dir \
	no_dest_dir2 \
	sync_delete_1 \
	sync_delete_2 \
	date_diff_2 \
	ftpsyncblock_nolist \
	ftpsyncblock_1 \
	space_bug_1_file \
	space_bug_2_dir \
	space_bug_3_file_delete \
	space_bug_4_dir_delete \
	pattern_1 \
	sizeonly_1 \
#	date_diff_1 


# selective tests

test $# -gt 0 && run_tests $*

# If there, all is good

echo3 ALL $test_count TESTS SUCCESSFUL

