#!/bin/bash
base=$(dirname $0)
P=dejsem
DEB=${DEB:-4}
CHAN=${CHAN:-2}
HOST=${HOST:-racek.hh.cz}
export DEB CHAN HOST
xclip="xclip -selection clipboard"
eta="tplzKrx6tž88ČJGlŘeMf."
tmp=/tmp/dejsem
tmpf=$tmp/file
tmpd=$tmp/dir

red="\e[1;31m"
green="\e[1;32m"
std="\e[0;39m"

help() {
	n=$(basename $0)
	echo -e "syntax:"
	echo -e "\t$n clip { short | long }"
	echo -e "\t$n hist"
	echo -e "\t$n srv  { short | long | dir }"
	echo -e "\t$n peer { short | long | dir }"
	echo -e "\t$n all"
	echo 
	echo ENVIRONMENT:
	echo DEB=0-5
	echo HOST="<server>"
	echo SSL="<path to ssl repository>"
	exit 1
}

clean() { rm -rf /tmp/dejsem; }

etalon() {
#----------------------------------------
#	create etalon file of specified size
#----------------------------------------
	mkdir -p $tmpf/chk
	rm -f $tmpf/etalon
	echo -n $eta >$tmpf/etalon
	cat $tmpf/etalon >$tmpf/f
	for i in `seq $1`
	do	cat $tmpf/etalon >> $tmpf/f
		cat $tmpf/f >> $tmpf/etalon
	done
	[[ $# -gt 1 ]] && echo -n "$2 " | cat - $tmp/f >$tmpf/etalon
	touch -r /etc $tmpf/etalon
	( cd $tmpf; ls -l etalon >chk/0.stat; )
}

etadir() {
#----------------------------------------
#	create etalon directory
#----------------------------------------
	mkdir -p $tmpd/chk
	rm -rf $tmpd/etalon
	mkdir -p $tmpd/etalon/d/alev
	mkdir -p $tmpd/etalon/epol
	
	etalon 3
	cp $tmpf/etalon $tmpd/etalon/f0
	touch -r /etc $tmpd/etalon/f0
	cp $tmpf/etalon $tmpd/etalon/d/f1
	
	etalon 5
	cp $tmpf/etalon $tmpd/etalon/epol/f2
	touch -r /lib $tmpd/etalon/epol/f2
	cp $tmpf/etalon $tmpd/etalon/d/alev/f3
	touch -r /usr/lib $tmpd/etalon/d/alev/f3
	
	touch -r /home $tmpd/etalon/d/alev
	touch -r /bin $tmpd/etalon/epol
	touch -r /usr $tmpd/etalon
	
	( 	cd $tmpd		
		zip -qr chk/0.zip etalon
		set - $(du -sbx etalon)
		echo $1 >chk/0.size
		ls -R etalon >chk/0.list
		ls -lR etalon >chk/0.stat
	)
}

say() {
	echo -e $*
	echo
}

chkdir() {
#----------------------------------------
#	compare sent and received etalon directory
#----------------------------------------
(	cd $tmpd/chk
	zip -qr 1.zip etalon || return 1
	set - $(du -sbx etalon)
	echo $1 >1.size
	ls -R etalon >1.list
	ls -lR etalon >1.stat
	
	if diff 0.list 1.list &>/dev/null
	then	say file/dir names ${green}OK${std}.
	else	say original and result file list ${red}differ${std}!!!
			return 1
	fi
	if diff 0.size 1.size &>/dev/null
	then	say total byte count ${green}OK${std}.
	else	say original and result byte count ${red}differ${std}!!!
			return 1
	fi
	if diff 0.stat 1.stat &>/dev/null
	then	say time stamps ${green}OK${std}.
	else	say original and result time stamps ${red}differ${std}!!!
			return 1
	fi
	if diff 0.zip 1.zip &>/dev/null
	then	return 0
	else	say data contents ${red}differ${std}!!!
			return 1
	fi		
)}

chkfile() {
#----------------------------------------
#	compare sent and received etalon files
#----------------------------------------
	if diff $tmpf/etalon $tmpf/chk/etalon
	then	say content of received file ${green}OK$std.
	else	say data content ${red}differs${std}!!!
			return 1
	fi		
	( cd $tmpf/chk; ls -l etalon >1.stat; )
	if diff $tmpf/chk/0.stat $tmpf/chk/1.stat
	then	say time stamps ${green}OK${std}.
	else	say original and result time stamps ${red}differ${std}!!!
			return 1
	fi
}

clip() {
#----------------------------------------
#	test clipboard exchange
#----------------------------------------
	[[ $1 ]] || help
	case $1 in
		short )	n=2 ;;
		long )	n=7 ;;
	esac	
	etalon $n
	clf=$tmpf/etalon
	$xclip -i <$clf
	$xclip -o >$tmpf/f1
	cl=$($xclip -o)
	echo
	say ">>> clipboard $1: ${#cl} chars >>> $($xclip -o | head -c 66)"
	$P --push
	echo
	read -p ">>> on Android perform pull clipboard "
	echo
	echo "" | $xclip -i
	ssh root@$HOST rm -f "/L/dejsem/$(printf %02d $CHAN)/clipboard"
	read -p ">>> on Android perform push clipboard "
	echo
	$P --pull
	$xclip -o >$tmpf/f2
	echo
    diff $tmpf/f1 $tmpf/f2 && return 0 || return 1
}

hist() {
#----------------------------------------
#	test clipboard history
#----------------------------------------
	ssh root@$HOST rm -rf "/L/dejsem/$(printf %02d $CHAN)/history"
	etalon 1
	echo 1111 entry 1, $(<$tmpf/etalon) | $xclip -i
	$P --push
	etalon 3
	echo 2222 long entry 2, $(<$tmpf/etalon) >$tmpf/chk/0.entry
	$xclip -i $tmpf/chk/0.entry
	$P --push
	etalon 1
	echo 3333 entry 3, $(<$tmpf/etalon) | $xclip -i
	$P --push
	rm -rf ~/.local/share/dejsem/history
	echo
	read -p ">>> pulling clipboard history to local host - it should contain 3 entries, hit ENTER "
	echo
	$P --pullhist
	echo
	read -p ">>> on Android go to clipboard  history and choose \"2222 long entry 2\" "
	echo
	read -p ">>> on Android push clipboard "
	$P --pull
	$xclip -o >$tmpf/chk/1.entry
	echo
    diff $tmpf/chk/0.entry $tmpf/chk/1.entry && return 0 || return 1
}

srv() {
#----------------------------------------
#	test file exchange with server
#----------------------------------------
	[[ $1 ]] || help
	if [[ $1 == dir ]]
	then
		etadir
		fp=$tmpd/etalon
		ssh root@$HOST rm -rf "/L/dejsem/$(printf %02d $CHAN)/files/etalon"
		say ">>> uploading directory etalon..."
		$P --pushfile $fp
		echo
		read -p ">>> on Android refresh / on server and download directory etalon "
		echo
		ssh root@$HOST rm -rf "/L/dejsem/$(printf %02d $CHAN)/files/etalon"
		echo
		read -p ">>> on Android upload directory etalon to / on server "
		echo
		rm -rf $tmpd/chk/etalon
		$P --pullfile etalon $tmpd/chk
		echo
		chkdir	
	else
		case $1 in
			short )		n=3 ;;
			long )		n=8 ;;
		esac	
		etalon $n
		fp=$tmpf/etalon
		ssh root@$HOST rm -rf "/L/dejsem/$(printf %02d $CHAN)/files/etalon"
		say ">>> uploading file etalon: $(set -- $(ls -lh $fp); echo $5) bytes"
		$P --pushfile $fp
		echo
		read -p ">>> refresh Android server-list and dowload file etalon "
		ssh root@$HOST rm -rf "/L/dejsem/$(printf %02d $CHAN)/files/etalon"
		echo
		read -p ">>> on Android upload file etalon "
		echo
		say ">>> downloading file etalon: $(set -- $(ls -lh $fp); echo $5) bytes"
		rm -rf $tmpf/chk/etalon
		$P --pullfile etalon $tmpf/chk
		echo
		chkfile
	fi
}

peer() {
#----------------------------------------
#	test file exchange with peer
#----------------------------------------
	[[ $1 ]] || help
	if [[ $1 == dir ]]
	then
		etadir
		fp=$tmpd/etalon
		echo
		read -p ">>> on Android start pull from peer "
		echo
		$P --pushpeer $fp
		say ">>> on Android copy directory etalon to peer (${red}1 minute TIMEOUT!${std})"
		rm -rf $tmpd/chk/etalon
		$P --pullpeer $tmpd/chk
		echo
		chkdir
	else
		case $1 in
			short )		n=3 ;;
			long )		n=13 ;;
		esac	
		etalon $n
		read -p ">>> on Android start pull from peer "
		echo
		$P --pushpeer $tmpd/etalon
		echo
		say ">>> on Android copy file etalon to peer (${red}1 minute TIMEOUT${std})"
		rm -rf $tmpf/chk/etalon
		$P --pullpeer $tmpf/chk
		echo
		chkfile
	fi
}

tst() {
#----------------------------------------
#	run specified test
#----------------------------------------
	x=y 
	while [[ $x == y ]]; do
		echo
		echo "*----------------------------------------"
		echo "* $*"
		echo "*----------------------------------------"
		echo
		$* &&  say "${green}>>> OK${std}" || say "${red}>>> orig & result don't compare !!!${std}"
		read -p  ">>> repaeat the test? y/N " x
	done	
}

all() {
#----------------------------------------
#	run all tests
#----------------------------------------
	tst clip short
	tst srv dir
	tst clip long
	tst srv short
	tst srv long
	tst peer short
	tst peer long
	tst peer dir
}

trap clean exit
clean
mkdir -p $tmp

[[ $1 == all ]] && {
	all
	exit
	}
	
(($#)) && {
	[[ $# -lt 1 ]] && help
	tst $*
	exit
0	}

help
10