#!/bin/busybox ash
# (c) Jason Williams 2013
HERE=`pwd`
. /etc/init.d/tc-functions
[ -f /tmp/.sceconflict ] && sudo rm /tmp/.sceconflict
[ -f /tmp/.scedeps ] && sudo rm /tmp/.scedeps
[ -f /tmp/.scedebs ] && sudo rm /tmp/.scedebs
[ -f /tmp/.scelist ] && sudo rm /tmp/.scelist
[ -f /tmp/.pkgexists ] && sudo rm /tmp/.pkgexists
[ -f /tmp/"$1".debdeps ] && sudo rm /tmp/"$1".debdeps

SCE="$1"

if ls /tmp/*.debs > /dev/null 2>&1; then 
	sudo rm /tmp/*.debs
fi

cd /etc/sysconfig/tcedir/sce
if [ -f /etc/sysconfig/tcedir/sce/"$1".sce.dep ]; then
	cat /etc/sysconfig/tcedir/sce/"$1".sce.dep > /tmp/.scedeps
else

	## Select menu for choosing dependency sce's
	ls *.sce | sed 's:.sce$::' | sort > /tmp/.scelist
	while true; do 
		[ -s /tmp/.scelist ] || break
		#cat /tmp/.scelist | select2 "You can choose more than one, press Enter to proceed." "-"
		cat /tmp/.scelist | select2 "Choose existing SCE(s) that will provide dependencies for "$1"." "-"
		read ANS < /tmp/select.ans
		if [ "$ANS" == "" ]; then
			break
		fi
		grep "^$ANS$" /tmp/.scedeps > /dev/null 2>&1 || echo "$ANS" >> /tmp/.scedeps
		sed -i "/^$ANS$/d" /tmp/.scelist
	done
	##

	## Echo the selected deps to the dep file
	if [ -f /tmp/.scedeps ]; then
		cat /tmp/.scedeps >> /etc/sysconfig/tcedir/sce/"$1".sce.dep
	else
		echo " "
		echo -n "* No dependency SCE(s) selected, proceeding to import '"$1"'."
	fi
	##
fi

## Recursive function to determine packages that exist in dependency sce's
getDeps() {

grep "^$1$" /tmp/.scedeps > /dev/null 2>&1 || echo "$1" >> /tmp/.scedeps
if [ -f "$1".sce.dep ]; then
	if grep "^$SCE$" "$1".sce.dep > /dev/null 2>&1; then
		echo "$1" >> /tmp/.sceconflict
		#break
		exit 0
	fi
	for E in `cat "$1".sce.dep`; do 
		getDeps "$E"
	done
fi
}
if [ -f /tmp/.scedeps ]; then
	for I in `cat /tmp/.scedeps`; do 
		getDeps "$I"
	done
fi
##

echo " "


## Create deb package lists from depdendency sce's
if [ -f /tmp/.scedeps ]; then
unset PKGEXISTS
	for I in `cat /tmp/.scedeps`; do 
		if [ -f /etc/sysconfig/tcedir/sce/update/"$I".sce ]; then
			DIR1=/tmp/"$I".tmp
		  	[ -d "$DIR1" ] || mkdir -p "$DIR1"
			sudo mount /etc/sysconfig/tcedir/sce/update/"$I".sce "$DIR1" > /dev/null 2>&1
			if [ "$?" != "0" ]; then
				echo " "
				echo "Unable to mount "$I".sce to get needed information, exiting.."
				exit 1
			fi
			if [ ! -f "$DIR1"/usr/local/sce/"$I"/"$I".md5sum ]; then
				#echo ""$I" has no md5sum file, please re-import it."
				sudo umount "$DIR1" > /dev/null 2>&1
		  		rmdir "$DIR1"
				continue
			fi
			if grep "^$1:" "$DIR1"/usr/local/sce/"$I"/"$I".md5sum > /dev/null 2>&1; then
				echo "$I" >> /tmp/.pkgexists
			fi
		  	cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: >> /tmp/.scedebs
		  	cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: > /tmp/"$I".debs
		  	
		  	sudo umount "$DIR1"
		  	rmdir "$DIR1"
		elif [ -d /tmp/tcloop/"$I" ]; then
			DIR1=/tmp/tcloop/"$I"
			# yes n | cp -ai "$DIR1"/* "$OUTDIR"/ 2>/dev/null
			if [ ! -f "$DIR1"/usr/local/sce/"$I"/"$I".md5sum ]; then
				#echo ""$I" has no md5sum file, please re-import it."
				continue
			fi
			if grep "^$1:" "$DIR1"/usr/local/sce/"$I"/"$I".md5sum > /dev/null 2>&1; then
				echo "$I" >> /tmp/.pkgexists
			fi
			cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: >> /tmp/.scedebs
			cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: > /tmp/"$I".debs
		elif [ -f /etc/sysconfig/tcedir/sce/"$I".sce ]; then
		  	DIR1=/tmp/"$I".tmp
		  	[ -d "$DIR1" ] || mkdir -p "$DIR1"
			sudo mount /etc/sysconfig/tcedir/sce/"$I".sce "$DIR1" > /dev/null 2>&1
			if [ "$?" != "0" ]; then
				echo " "
				echo "Unable to mount "$I".sce to get needed information, exiting.."
				exit 1
			fi
			if [ ! -f "$DIR1"/usr/local/sce/"$I"/"$I".md5sum ]; then
				#echo ""$I" has no md5sum file, please re-import it."
				sudo umount "$DIR1" > /dev/null 2>&1
		  		rmdir "$DIR1"
				continue
			fi
		  	# yes n | cp -ai "$DIR1"/* "$OUTDIR"/ > /dev/null 2>&1
			if grep "^$1:" "$DIR1"/usr/local/sce/"$I"/"$I".md5sum > /dev/null 2>&1; then
				echo "$I" >> /tmp/.pkgexists
			fi
		  	cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: >> /tmp/.scedebs
		  	cat "$DIR1"/usr/local/sce/"$I"/"$I".md5sum | cut -f1 -d: > /tmp/"$I".debs
		  	
		  	sudo umount "$DIR1"
		  	rmdir "$DIR1"
		else 
			:
			#echo "ERROR:  "$I".sce not found!"
			#exit 1
		fi
	done
fi
##

ls -t /tmp/work > /tmp/"$1".debdeps
cd "$HERE"
