#!/bin/sh
# (c) Jason Williams 2014
# Tool to update SCE extensions in bulk.

. /etc/init.d/tc-functions
checknotroot
SCEDIR="/etc/sysconfig/tcedir/sce"
BUILD=`getBuild`
unset DEPLIST
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo "Usage:"
	echo "'"sce-update"' with no options will bring up a menu of SCEs to choose to update."
	echo "'"sce-update -a"' will update all existing SCEs, both loaded and unloaded."
	echo "'"sce-update -n"' for non-interactive mode."
	echo "'"sce-update -c"' for check only 'dry run' mode, no option to update."
	echo "'"sce-update -r"' for sce-import to unpack files in RAM during update."
	echo "'"sce-update -s"' for sce-import to check for size and available space during update."
	echo "'"sce-update -z"' will ignore /etc/sysconfig/sceconfig and only use current command line options."
	echo "'"sce-update iceweasel"' will update iceweasel."
	exit 0
fi

while getopts canrsz OPTION
do
	case ${OPTION} in
		r) RAM=TRUE ;;
		n) NONINTERACTIVE=TRUE ;;
		c) CHECKONLY=TRUE ;;
		a) UPDATEALL=TRUE ;;
		s) SIZE=TRUE ;;
		z) NOCONFIG=TRUE ;;
	esac
done

# Determine if non-interactive mode is being used.
if grep -i "^NONINTERACTIVE=TRUE" /etc/sysconfig/sceconfig > /dev/null 2>&1 && [ "$NOCONFIG" != "TRUE" ]; then
	NONINTERACTIVE=TRUE
fi

shift `expr $OPTIND - 1`
UPDATETARGET="$1"
OPTIONS=""
if [ "$RAM" == "TRUE" ]; then
	OPTIONS=""$OPTIONS"r"
fi

if [ "$SIZE" == "TRUE" ]; then
	OPTIONS=""$OPTIONS"s"
fi

if [ -z "$1" ]; then
	SELECT=TRUE
fi

[ -d /tmp/work ] && sudo rm -r /tmp/work
[ -f /tmp/.sceupdatechoose ] && sudo rm /tmp/.sceupdatechoose
[ -f /tmp/.scelistchoose ] && sudo rm /tmp/.scelistchoose
[ -f /tmp/.sceupdateall ] && sudo rm /tmp/.sceupdateall
[ -f /tmp/.sceupdatelist ] && sudo rm /tmp/.sceupdatelist
[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans
[ -f /tmp/.importupdates ] && sudo rm /tmp/.importupdates
[ -f /tmp/updateavailable ] && sudo rm /tmp/updateavailable
[ -f /tmp/importupdated ] && sudo rm /tmp/importupdated

cd /etc/sysconfig/tcedir/sce

exit_tcnet() {
	echo "There is an issue connecting to `cat /opt/tcemirror`, exiting.."
	exit 1
}

updateenv() {
read IMPORTMIRROR < /opt/tcemirror
PREBUILTMIRROR="${IMPORTMIRROR%/}/dCore/"$BUILD"/import"
IMPORTMIRROR="${IMPORTMIRROR%/}/dCore/import"

sudo debGetEnv "$2"
if [ "$?" != "0" ]; then
	echo "Error in updating needed DEBINX files.  Exiting..."
fi
read DEBINX < /tmp/debinx
cd /tmp

[ -f /tmp/deb2sce.tar.gz ] && sudo rm /tmp/deb2sce.tar.gz
/bb/wget -O /tmp/deb2sce.tar.gz -cq "$PREBUILTMIRROR"/deb2sce.tar.gz ||  exit_tcnet
[ -f /tmp/PKGEXCLUDELIST ] || tar xvf /tmp/deb2sce.tar.gz PKGEXCLUDELIST >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGEXTRAREPODEP >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGADDDEP >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGEXTRAFILES >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGPREBUILTDEP >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PKGDATAFILEMD5SUMLIST >/dev/null 2>&1
tar xvf /tmp/deb2sce.tar.gz PREBUILTMD5SUMLIST >/dev/null 2>&1
cd "$SCEDIR"
}

## Get recursive list of dependency SCEs.
getDeps() {
DEPLIST=" $1 $DEPLIST "

if [ -f "$SCEDIR"/"$1".sce.dep ]; then
	for E in `cat "$SCEDIR"/"$1".sce.dep`; do 
		H=" $E "
		if echo "$DEPLIST" | grep "$H" > /dev/null 2>&1; then
			continue
		else 
			getDeps "$E"
		fi
	done
fi
}
##

if [ "$UPDATEALL" == "TRUE" ]; then
	updateenv
	echo " "
	echo "Searching for available updates for chosen SCEs."
	## Get list of SCEs to be updated, should be all.
	for I in `ls *.sce | sed 's:.sce::' | sort`; do 
		echo "$I"
		importupdatecheck "$I" > /dev/null 2>&1
	done
	##
	echo " "
	if [ "$CHECKONLY" == "TRUE" ]; then
		if [ -s /tmp/.sceupdatelist ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				echo "Refer to /tmp/updateavailable for details of package updates."
				exit 0
			else
				echo "Do you want to review the list of packages that have updates available?"
				echo -n "Enter y to view and q to exit reading the package list. (y/N): "
				read ans
				if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
					less /tmp/updateavailable
				fi			
				exit 0
			fi
		else
			clear
			echo "No updates available for chosen SCEs."
			exit 0
		fi
	fi
	#cat /tmp/.sceupdatelist | tr -d :
	echo " "
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$NONINTERACTIVE" == "TRUE" ]; then
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
			echo "Refer to /tmp/updateavailable for details of package updates."
		else
			clear
			cat /tmp/.sceupdatelist | tr -d :
			echo " "
			echo " "
			echo -n "The above SCEs are about to be updated.  Press Enter to proceed, y to view package updates, or Ctrl-C to abort.."
			read ans
			if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
				less /tmp/updateavailable
			fi
			## Update all SCEs
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
		fi
		##

	else 
		clear
		echo "No updates available for chosen SCEs at this time."
		exit 1
	fi
elif [ -f update/"$UPDATETARGET".sce ]; then
	cd update/
	updateenv
	echo " "
	echo "Searching for available updates for "$UPDATETARGET".sce."
	unset DEPLIST
	getDeps "$UPDATETARGET"
	for I in `echo "$DEPLIST"`; do
		importupdatecheck "$I" > /dev/null 2>&1
	done
	unset DEPLIST
	if [ "$CHECKONLY" == "TRUE" ]; then
		if [ -s /tmp/.sceupdatelist ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				echo "Refer to /tmp/updateavailable for details of package updates."
				exit 0
			else
				echo "Do you want to review the list of packages that have updates available?"
				echo -n "Enter y to view and q to exit reading the package list. (y/N): "
				read ans
				if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
					less /tmp/updateavailable
				fi			
				exit 0
			fi
		else
			clear
			echo "No updates available for chosen SCEs."
			exit 0
		fi
	fi
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$NONINTERACTIVE" == "TRUE" ]; then
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
			echo "Refer to /tmp/updateavailable for details of package updates."
		else
			clear
			cat /tmp/.sceupdatelist | tr -d :
			echo " "
			echo " "
			echo -n "The above SCEs are about to be updated.  Press Enter to proceed, y to view package updates, or Ctrl-C to abort.."
			read ans
			if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
				less /tmp/updateavailable
			fi
			## Update all SCEs
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
		fi
		##

	else 
		clear
		echo "No updates available for chosen SCEs at this time."
		exit 1
	fi
	cd ..
elif [ -f "$UPDATETARGET".sce ]; then
	updateenv
	echo " "
	echo "Searching for available updates for "$UPDATETARGET".sce."
	unset DEPLIST
	getDeps "$UPDATETARGET"
	for I in `echo "$DEPLIST"`; do
		importupdatecheck "$I" > /dev/null 2>&1
	done
	unset DEPLIST
	if [ "$CHECKONLY" == "TRUE" ]; then
		if [ -s /tmp/.sceupdatelist ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				echo "Refer to /tmp/updateavailable for details of package updates."
				exit 0
			else
				echo "Do you want to review the list of packages that have updates available?"
				echo -n "Enter y to view and q to exit reading the package list. (y/N): "
				read ans
				if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
					less /tmp/updateavailable
				fi			
				exit 0
			fi
		else
			clear
			echo "No updates available for chosen SCEs."
			exit 0
		fi
	fi
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$NONINTERACTIVE" == "TRUE" ]; then
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
			echo "Refer to /tmp/updateavailable for details of package updates."
		else
			clear
			cat /tmp/.sceupdatelist | tr -d :
			echo " "
			echo " "
			echo -n "The above SCEs are about to be updated.  Press Enter to proceed, y to view package updates, or Ctrl-C to abort.."
			read ans
			if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
				less /tmp/updateavailable
			fi
			## Update all SCEs
			for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				grep "^$I$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$I"
				echo "$I" >> /tmp/importupdated
			done
		fi
		##

	else 
		clear
		echo "No updates available for chosen SCEs at this time."
		exit 1
	fi
elif [ "$SELECT" == "TRUE" ]; then
	ls *.sce | sed 's:.sce::' | sort > /tmp/.scelistchoose
	while true; do 
		cat /tmp/.scelistchoose | select "Choose the SCE you want to check for updates . \
You can choose more than one, and enter "q" for quit when you have completed your selection." "-"
		read ANS < /tmp/select.ans
		if [ "$ANS" == "q" ]; then
			break
		fi
		grep "^$ANS$" /tmp/.sceupdatechoose > /dev/null 2>&1 || echo "$ANS" >> /tmp/.sceupdatechoose
		sed -i "/^$ANS$/d" /tmp/.scelistchoose
	done
	##
	clear
	if [ ! -s /tmp/.sceupdatechoose ]; then
		echo "No SCEs were chosen for update.  Exiting.."
		exit 0
	fi
	cat /tmp/.sceupdatechoose | tr -d :
	echo " "
	echo -n "You are about to check for updates to the above SCEs. y to continue, n to exit. (y/N): "
	read ANS
	if [ "$ANS" == "y" ] || [ "$ANS" == "Y" ]; then
		updateenv
		## Update selected SCEs
		for I in `cat /tmp/.sceupdatechoose | tr -d :`; do
			echo " "
			echo "Checking for updates for "$I".sce.."
			unset DEPLIST
			getDeps "$I"
			for D in `echo "$DEPLIST"`; do
				importupdatecheck "$D" > /dev/null 2>&1
			done
			#unset DEPLIST
		done
		if [ "$CHECKONLY" == "TRUE" ]; then
			if [ -s /tmp/.sceupdatelist ]; then
				if [ "$NONINTERACTIVE" == "TRUE" ]; then
					echo "Refer to /tmp/updateavailable for details of package updates."
					exit 0
				else
					echo "Do you want to review the list of packages that have updates available?"
					echo -n "Enter y to view and q to exit reading the package list. (y/N): "
					read ans
					if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
						less /tmp/updateavailable
					fi			
					exit 0
				fi
			else
				clear
				echo "No updates available for chosen SCEs."
				exit 0
			fi
		fi
	
		## Update SCEs
		if [ -s /tmp/.sceupdatelist ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				for I in `cat /tmp/.sceupdatelist | tr -d :`; do
					getDeps "$I"
				done
				for D in `echo "$DEPLIST"`; do
					grep "^$D$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$D"
					echo "$D" >> /tmp/importupdated
				done
				echo "Refer to /tmp/updateavailable for package update details."
			else
				clear
				cat /tmp/.sceupdatelist | tr -d :
				echo " "
				echo " "
				echo -n "The above SCEs are about to be updated.  Press Enter to proceed, y to view package updates, or Ctrl-C to abort.."
				read ans
				if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
					less /tmp/updateavailable
				fi
				## Update all SCEs
				#for I in `cat /tmp/.sceupdatelist | tr -d :`; do
				#	getDeps "$I"
				#done
				for D in `echo "$DEPLIST"`; do
					grep "^$D$" /tmp/importupdated > /dev/null 2>&1 || sce-import -"$OPTIONS"np "$D"
					echo "$D" >> /tmp/importupdated
				done
			fi
			##
	
		else 
			clear
			echo "No updates available for chosen SCEs at this time."
			exit 1
		fi
		##
	else
		echo "Exit was chosen, no action will be taken."
	fi
else
	echo ""$UPDATETARGET" is not an existing SCE file.  Exiting.."
fi

[ -f /tmp/.sceupdatechoose ] && sudo rm /tmp/.sceupdatechoose
[ -f /tmp/.scelistchoose ] && sudo rm /tmp/.scelistchoose
[ -f /tmp/.sceupdateall ] && sudo rm /tmp/.sceupdateall
[ -f /tmp/.sceupdatelist ] && sudo rm /tmp/.sceupdatelist
[ -f /tmp/select.ans ] && sudo rm /tmp/select.ans
[ -f /tmp/.importupdates ] && sudo rm /tmp/.importupdates
[ -f /tmp/PKGADDDEP ] && sudo rm /tmp/PKGADDDEP
[ -f /tmp/PKGDATAFILEMD5SUMLIST ] && sudo rm /tmp/PKGDATAFILEMD5SUMLIST
[ -f /tmp/PKGEXTRAFILES ] && sudo rm /tmp/PKGEXTRAFILES
[ -f /tmp/PKGEXTRAREPODEP ] && sudo rm /tmp/PKGEXTRAREPODEP
[ -f /tmp/PKGPREBUILTDEP ] && sudo rm /tmp/PKGPREBUILTDEP
[ -f /tmp/PREBUILTMD5SUMLIST ] && sudo rm /tmp/PREBUILTMD5SUMLIST
[ -f /tmp/importupdates ] && sudo rm /tmp/importupdates
[ -f /tmp/ssupdates ] && sudo rm /tmp/ssupdates
ls /tmp/*.md5sum > /dev/null 2>&1 && sudo rm /tmp/*.md5sum
[ -f /tmp/.importpkgtype ] && sudo rm /tmp/.importpkgtype
[ -f /tmp/.importpkgtype ] && sudo rm /tmp/.importpkgtype
[ -f /tmp/.prebuiltmd5sumlist ] && sudo rm /tmp/.prebuiltmd5sumlist
[ -f /tmp/.pkgextrafilemd5sumlist ] && sudo rm /tmp/.pkgextrafilemd5sumlist
[ -f /tmp/.pkgprebuilt ] && sudo rm /tmp/.pkgprebuilt
[ -d /tmp/work ] && sudo rm -r /tmp/work

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

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

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

