#!/bin/bash
e=.expected/
if [ -z "${1}" ]; then
	is_del=1
else
	is_del=0
	is_diff=${1}   # ./c 1 is diff, else(example: 0) to populate .expected
	diffs=0
	if [ ${is_diff} = 1 ]; then
		if [ ! -e ${e} ]; then
			echo ${e} comparisons only at https://launchpad.net/~colin-i/+archive/ubuntu/ppa
			exit 0
		fi
	fi
fi
to_expected () {
	if [ -z "${is_log}" ]; then
		base64 ${1} > ${exp}${1}.txt
	else
		mv ${1} ${exp}${1}
		#chmod -x ${exp}${1}    #else, will be warning at dpkg-source
	fi
}
switch () {
	test -e ${1}
	if [ $? = 0 ]; then
		if [ ${is_del} = 1 ]; then
			rm ${1}
		else
			is_log=`echo ${1} | grep ".log$"`
			if [ ${is_diff} = 1 ]; then
				if [ -z "${is_log}" ]; then
					btxt=${exp}${1}.txt
					base64 -d ${btxt} > ${exp}${1}
				fi
				echo diff ${1} ${exp}${1}
				diff ${1} ${exp}${1}
				if [ $? != 0 ]; then
					if [ -z "${continue_on_diff}" ]; then
						if [ -z "${is_log}" ]; then
							base64 ${1}
							cat ${btxt}
						fi
						if [ -n "${mod_and_cont}" ]; then
							echo y \?; read -n1 -t $((60*60*24)) ans
							if [ "${ans}" != "y" ]; then { echo not ok; exit 1; } fi
							to_expected ${1}
							diffs=$((diffs+1))
						else
							exit 1
						fi
					fi
				else
					diffs=$((diffs+1))
				fi
				#if [ -z "${is_log}" ]; then  #remove them later
				#	rm ${exp}${1}
				#fi
			else
				to_expected ${1}
			fi
		fi
	fi
}
sw () {
	n="$(find -maxdepth 1 -name "*${1}")"
	if [ -n "${n}" ]; then
		for v in ${n}; do
			switch $(expr substr ${v} 3 $(echo $(echo -n ${v} | wc -m)-2 | bc))  #switch ${v:2}
		done
	fi
}
del () {
	sw .o
	sw .log
	sw .x
	sw .exe       #by ocomp
	switch a.out  #when ld also a.out.oc
}
a="`ls`"
_e_=$(expr substr ${e} 1 $(echo $(echo -n ${e} | wc -m)-1 | bc))   #${e::-1}
for var in ${a}; do
	if [ -d "${var}" ]; then
		if [ "${var}" != "${_e_}" ]; then
			cd "${var}"
				exp=../${e}${var}/
				if [ ${is_del} = 0 ]; then
					mkdir -p ${exp}
				fi
				del
			cd ..
		fi
	fi
done

exp=${e}
del
switch acall/e

if [ ${is_del} = 0 ]; then
	if [ ${is_diff} = 1 ]; then
		echo diff .expected/a.out.c a.out.c
		diff .expected/a.out.c a.out.c || {
			if [ -z "${continue_on_diff}" ]; then
				if [ -n "${mod_and_cont}" ]; then
					echo y \?; read -n1 -t $((60*60*24)) ans
					if [ "${ans}" != "y" ]; then { echo not ok; exit 1; } fi
					is_log=x to_expected a.out.c
				else
					exit 1
				fi
			fi
		}
		tests="$(find -name "*.oc")"
		n=`echo ${tests} | wc -w`
		n=$((n*3))   #is .oc .log and .x
		echo ${n} ${diffs}
		if [ ${n} = ${diffs} ]; then
			echo diffs is ok
		else
			exit 1
		fi
	else # populate
		mv a.out.c .expected
	fi
else
	#remove otoc files
	a="$(find -maxdepth 1 -name "*.c")"
	if [ -n "${a}" ]; then
		for var in ${a}; do
			rm ${var}
		done
	fi

	#remoce cc output, will be different, based on distribution/architecture
	if [ -e ./cc ]; then
		rm ./cc
	fi

	#remove base64 troubles #or remove them after diff
	cd ${e} || exit 0
	a="$(find . -type f ! -name "*.txt" ! -name "*.log" ! -name "*.c")"
	if [ -n "${a}" ]; then
		for var in ${a}; do
			rm ${var}
		done
	fi
fi
