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

. /etc/init.d/tc-functions
checknotroot
TCEDIR=/etc/sysconfig/tcedir
SCEDIR="/etc/sysconfig/tcedir/sce"
DEBINXDIR="/etc/sysconfig/tcedir/import/debinx"
BUILD=`getBuild`
unset DEPLIST SCEBOOTLIST VIEWED

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        echo " "
	echo "${YELLOW}sce-update - Check and update all or individual SCE(s) including any dependency"
	echo "             SCE(s).${NORMAL} View DEBINX (Debian index) diff and available updates if"
	echo "             desired. May use some option combinations."
	echo "Usage:"
	echo "${YELLOW}"sce-update"${NORMAL}         Menu prompt, choose SCE(s) to check, update if required."
       	echo "${YELLOW}"sce-update SCE"${NORMAL}     Check a specific SCE for update, update if required."
	echo "${YELLOW}"sce-update -n SCE"${NORMAL}  Non-interactive mode, check a specific SCE for updates,"
	echo "                   update if required."
	echo "${YELLOW}"sce-update -a"${NORMAL}      Check all system SCEs for updates, view DEBINX diff and"
	echo "                   available updates if desired, update SCEs if required."
	echo "${YELLOW}"sce-update -na"${NORMAL}     Non-interactive mode, check all SCEs for updates,"
	echo "                   update if required."
	echo "${YELLOW}"sce-update -b"${NORMAL}      Check sceboot.lst SCEs for updates, update if required."
	echo "${YELLOW}"sce-update -c"${NORMAL}      Menu prompt, check selected SCE(s) for updates only,"
        echo "                   no actual updates performed."
	echo "${YELLOW}"sce-update -r"${NORMAL}      Unpack files in RAM during an SCE update re-import."
	echo "${YELLOW}"sce-update -s"${NORMAL}      Estimate package, HD and RAM space during an SCE update"
	echo "                   re-import, warn as needed."
	echo "${YELLOW}"sce-update -z"${NORMAL}      Ignore /etc/sysconfig/sceconfig preferences, use only"
        echo "                   current command line options."
	echo "${YELLOW}"sce-update -f"${NORMAL}      Full check mode, all packages and data contained in the"
        echo "                   SCE will be checked for updates.  Default is the SCE is"
	echo "                   flagged for need of update on the first found item in"
	echo "                   need of update and checking mode for that SCE then exits."
	exit 0
fi

[ -f /tmp/sceupdatefull ] && sudo rm /tmp/sceupdatefull

if [ ! "$NOLOCK" == "TRUE" ]; then
	if [ -f /tmp/.scelock ]; then
		LOCK=`cat /tmp/.scelock`
		if /bb/ps -o pid | sed -e 's/^[ \t]*//'| grep "^$LOCK$" | /bb/grep -v grep > /dev/null 2>&1; then
			echo "${YELLOW}WARNING:${NORMAL} Another SCE utility is presently in use, exiting.."
			exit 1
		fi
	fi
	
	echo "$$" > /tmp/.scelock
fi

while getopts canrszbf OPTION
do
	case ${OPTION} in
		r) RAM=TRUE;;
		n) NONINTERACTIVE=TRUE ;;
		c) CHECKONLY=TRUE ;;
		a) UPDATEALL=TRUE ;;
		s) SIZE=TRUE ;;
		z) NOCONFIG=TRUE ;;
		b) SCEBOOTLIST=TRUE ;;
		f) SCEUPDATEFULL=TRUE ;;
                *) echo "Run  sce-update --help  for usage information."
                   exit 1 ;;
	esac
done


NICE=`grep -m 1 "^NICE=" /etc/sysconfig/sceconfig | cut -f2 -d=`
if [ -n "$NICE" ]; then
	if [ "$NICE" -gt 19 ]; then
		NICE=19
		echo "* Using nice level 19, 19 is the highest possible nice value."
	elif [ 0 -gt "$NICE" ]; then
		NICE=0
		echo "* Using nice level "$NICE", only root can use negative nice values."
	else 
		echo "* Using nice level "$NICE"."
	fi
	/bb/renice -n "$NICE" -p $$
fi


# 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=`basename "$1" .sce`
OPTIONS=""

if [ "$UPDATEALL" == "TRUE" ]; then
	if [ -n "$1" ]; then
		echo " "
		echo "${YELLOW}WARNING:${NORMAL} The -a option is not to be used with a specified SCE, exiting.."
		exit 1
	else
		echo "* Using the -a option."
	fi
fi

if [ "$SCEBOOTLIST" == "TRUE" ]; then
	if [ -n "$1" ]; then
		echo " "
		echo "${YELLOW}WARNING:${NORMAL} The -b option is not to be used with a specified SCE, exiting.."
		exit 1
	else
		echo "* Using the -b option."
	fi
fi

if [ "$CHECKONLY" == "TRUE" ]; then
	echo "* Using the -c option."
fi

if [ "$SCEUPDATEFULL" == "TRUE" ]; then
	echo "* Using the -f option."
fi

if [ "$NONINTERACTIVE" == "TRUE" ]; then
	echo "* Using the -n option."
fi

if [ "$RAM" == "TRUE" ]; then
	echo "* Using the -r option."
	OPTIONS=""$OPTIONS"r"
fi

if [ "$SIZE" == "TRUE" ]; then
	echo "* Using the -s option."
	OPTIONS=""$OPTIONS"s"
fi

if [ "$NOCONFIG" == "TRUE" ]; then
	echo "* Using the -z option."
fi

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

if [ "$SCEUPDATEFULL" == "TRUE" ]; then
	touch /tmp/sceupdatefull
fi

if [ "$SCEBOOTLIST" == "TRUE" ] && [ "$UPDATEALL" == "TRUE" ]; then
	echo " "
	echo "${YELLOW}WARNING:${NORMAL} The -a and -b options cannot be used together, exiting.."
	exit 1
fi

if [ -n "$2" ]; then
	echo " "
	echo "${YELLOW}WARNING:${NORMAL} Only one SCE per sce-update session can be entered in command line."
	echo "         Run 'sce-update' to check more than one SCE per session, exiting.."
	exit 1
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
[ -f /tmp/.importpkgtype ] && sudo rm /tmp/.importpkgtype
[ -f /tmp/.updatechecked ] && sudo rm /tmp/.updatechecked   
[ -f /tmp/sceupdateerror ] && sudo rm /tmp/sceupdateerror


cleanup() {
	sudo chown -R "$TCUSER":staff "$SCEDIR" > /dev/null 2>&1
	[ -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/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/.prebuiltmd5sumlist ] && sudo rm /tmp/.prebuiltmd5sumlist
	[ -f /tmp/.pkgextrafilemd5sumlist ] && sudo rm /tmp/.pkgextrafilemd5sumlist
	[ -f /tmp/.pkgprebuilt ] && sudo rm /tmp/.pkgprebuilt
	[ -f /tmp/.updatechecked ] && sudo rm /tmp/.updatechecked
	[ -d /tmp/work ] && sudo rm -r /tmp/work
	sudo rm /tmp/*suggested > /dev/null 2>&1
	sudo rm /tmp*recommended > /dev/null 2>&1
}

cd /etc/sysconfig/tcedir/sce

exit_tcnet() {
	echo " "
	echo "${YELLOW}WARNING:${NORMAL} Issue connecting to `cat /opt/tcemirror`, exiting.."
	exit 1
}


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

sudo -E debGetEnv "$2"
if [ "$?" != "0" ]; then
	echo " "
	echo "${YELLOW}WARNING:${NORMAL} Error updating DEBINX files, exiting.."
	exit 1
fi
read DEBINX < /etc/sysconfig/tcedir/import/debinx/debinx

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
	
##### OLDDEBINX, NEWDEBINX
#####
	cd /etc/sysconfig/tcedir/import/debinx
	if [ -f OLDDEBINX ]; then
		MDOLD=`md5sum OLDDEBINX | cut -f1 -d" "`
	else
		MDOLD="0"
	fi
	MDNEW=`md5sum NEWDEBINX | cut -f1 -d" "`
	if [ "$MDOLD" == "$MDNEW" ]; then
		echo " "
		echo "The entire SCE directory is up to date, exiting.."
		sudo rm NEWDEBINX
		exit 0
	elif [ ! "$NONINTERACTIVE" == "TRUE" ]; then
		echo " "
		if [ ! -f "$DEBINXDIR"/OLDDEBINX ]; then
			echo "${YELLOW}WARNING:${NORMAL} No "$DEBINXDIR"/OLDDEBINX available to compare."
			echo " "
			echo -n "Recommend press Enter to proceed with update check, Ctrl-C aborts: "
			read ans
			echo " "
			:
		else
			echo "Press Enter to proceed with update check, (v)iew DEBINX diff, Ctrl-C aborts"
			echo -n "(to view DEBINX diff use spacebar and (q)uit): "
			read ans
			echo " "
			if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
				echo -n "Obtaining diff of new and old package data."
				diff "$DEBINXDIR"/OLDDEBINX "$DEBINXDIR"/NEWDEBINX > /dev/null 2>&1 > /tmp/debinx.diff & /usr/bin/rotdash $!
				echo " " && echo " "
				more -s /tmp/debinx.diff
				rm /tmp/debinx.diff
				echo " "
				echo -n "Press Enter to proceed with update check, Ctrl-C aborts: "
				read ans
				echo " "
			fi
		fi
	fi

	

##### /OLDDEBINX, NEWDEBINX
#####	

	cd "$SCEDIR"
	echo "Checking all system SCEs for updates:"
	echo " "
	## Get list of SCEs to be updated, should be all.
	for I in `ls *.sce | sed 's:.sce$::' | sort`; do
		if [ -f update/"$I".sce.debinx ] && [ -f update/"$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat update/"$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		elif [ -f "$I".sce.debinx ] && [ -f "$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat "$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		fi
		echo -n "         ${YELLOW}"$I".sce${NORMAL} update check."
		sudo importupdatecheck "$I" > /dev/null 2>&1 & /usr/bin/rotdash $!
		echo " "
	done
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$CHECKONLY" != "TRUE" ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			else
				echo " "
				echo "Update check completed."
				echo " "
				cat  /tmp/.sceupdatelist | sort | uniq
				echo " "
                                echo -n "Press Enter to update above SCE(s), (v)iew package updates, Ctrl-C aborts: "
				read ans
				echo " "
				if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
					more -s /tmp/updateavailable
					echo " "
					echo -n "Press Enter to proceed with updates, Ctrl-C aborts: "
					read ans
					echo " "
				fi
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			fi
		else
			echo " "
			echo "Update check completed."
			echo " "
			cat  /tmp/.sceupdatelist | sort | uniq
			echo " "
			echo -n "Press Enter to view package updates, Ctrl-C aborts: "
			read ans
			if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
				more -s /tmp/updateavailable
			fi
			exit 0
		fi
		cd /etc/sysconfig/tcedir/import/debinx
		sudo cp NEWDEBINX OLDDEBINX > /dev/null 2>&1
	else
		echo " "
		echo "No updates available for main or any dependency SCEs."
		cd /etc/sysconfig/tcedir/import/debinx
		sudo cp NEWDEBINX OLDDEBINX > /dev/null 2>&1
	fi
	##
elif [ "$SCEBOOTLIST" == "TRUE" ]; then
	echo " "
	## Added top of script:
	#if [ -n "$1" ]; then
	#	echo " "
	#	echo "${YELLOW}WARNING:${NORMAL} The -b option is not to be used with a specified SCE, exiting.."
	#	exit 1
	#fi	
	for i in `cat /proc/cmdline`; do
		case $i in
			lst=*) TARGETLIST=${i#*=} ;;
		esac
	done
	[ -n "$TARGETLIST" ] || TARGETLIST="sceboot.lst"
	for I in `cat "$TCEDIR"/"$TARGETLIST"`; do
		getDeps "$I"
	done
	for I in `echo "$DEPLIST"`; do
		if grep "^$I$" /tmp/.updatechecked > /dev/null 2>&1; then
			continue
		fi
		if [ -f update/"$I".sce.debinx ] && [ -f update/"$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat update/"$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		elif [ -f "$I".sce.debinx ] && [ -f "$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat "$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		fi
		echo -n "         ${YELLOW}"$I".sce${NORMAL} update check."
		sudo importupdatecheck "$I" > /dev/null 2>&1 & /usr/bin/rotdash $!
		echo " "
	done
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$CHECKONLY" != "TRUE" ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			else
				echo " "
				echo "Update check completed."
				echo " "
				cat  /tmp/.sceupdatelist | sort | uniq
				echo " "
                                echo -n "Press Enter to update above SCE(s), (v)iew package updates, Ctrl-C aborts: "
				read ans
				echo " "
				if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
					more -s /tmp/updateavailable
					echo " "
					echo -n "Press Enter to proceed with updates, Ctrl-C aborts: "
					read ans
					echo " "
				fi
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			fi
		else
			echo " "
			echo "Update check completed."
			echo " "
			cat  /tmp/.sceupdatelist | sort | uniq
			echo " "
			echo -n "Press Enter to view package update details, Ctrl-C aborts: "
			read ans
			if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
				more -s /tmp/updateavailable
			fi
			exit 0
		fi
	else
		echo " "
		echo "No updates available for main or any dependency SCEs."
	fi
elif [ "$SELECT" == "TRUE" ]; then
	ls *.sce | sed 's:.sce$::' | sort > /tmp/.scelistchoose
	while true; do
		[ ! -s /tmp/.scelistchoose ] && break
		cat /tmp/.scelistchoose | select2 "Choose SCE(s) to update check, press Enter with no selection to proceed." "-"
		read ANS < /tmp/select.ans
		if [ "$ANS" == "" ]; then
			break
		fi
		grep "^$ANS$" /tmp/.sceupdatechoose > /dev/null 2>&1 || echo "$ANS" >> /tmp/.sceupdatechoose
		sed -i "/^$ANS$/d" /tmp/.scelistchoose
	done
	##
	if [ ! -s /tmp/.sceupdatechoose ]; then
		echo "No SCE(s) chosen for update check, exiting.."
		exit 0
	fi
	echo " "
	cat /tmp/.sceupdatechoose | tr -d :
	echo " "
	echo -n "Press Enter to update check above SCE(s), (q)uit to exit: "
	read ANS
	if [ "$ANS" == "q" ] || [ "$ANS" == "quit" ]; then
		cleanup
		exit 0
	else
		## Update selected SCEs
		echo " "
		for D in `cat /tmp/.sceupdatechoose | tr -d :`; do
			echo "Checking ${YELLOW}"$D".sce${NORMAL}."
			unset DEPLIST
			getDeps "$D"
			for I in `echo "$DEPLIST"`; do
				UPDATETARGET="$I"
				if [ -f update/"$I".sce.debinx ] && [ -f update/"$I".sce.md5.txt ]; then
					MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
					MDOLD=`cat update/"$I".sce.debinx`
					if [ "$MDOLD" == "$MDNEW" ]; then
						echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
						continue
					fi
				elif [ -f "$I".sce.debinx ] && [ -f "$I".sce.md5.txt ]; then
					MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
					MDOLD=`cat "$I".sce.debinx`
					if [ "$MDOLD" == "$MDNEW" ]; then
						echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
						continue
					fi
				fi
				echo -n "         ${YELLOW}"$I".sce${NORMAL} update check."
				sudo importupdatecheck "$I" > /dev/null 2>&1 & /usr/bin/rotdash $!
				echo " "
			done
		done
		if [ -s /tmp/.sceupdatelist ]; then
			if [ "$CHECKONLY" != "TRUE" ]; then
				if [ "$NONINTERACTIVE" == "TRUE" ]; then
					for I in `cat /tmp/.sceupdatelist`; do
						if [ -f /tmp/"$I".recommended ]; then
							OPTIONS="$OPTIONS"R
						fi
						if [ -f /tmp/"$I".suggested ]; then
							OPTIONS="$OPTIONS"S
						fi
						if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
							sce-import -"$OPTIONS"Nnp "$I"
							if [ -s /tmp/sceimporterror ]; then
								echo "Error in updating "$I"" >> /tmp/sceupdateerror
								cat /tmp/sceimporterror >> /tmp/sceupdateerror
								echo " " >> /tmp/sceupdateerror
								continue
							fi
						fi
						echo "$I" >> /tmp/importupdated
					done
				else
					echo " "
					echo "Update check completed."
					echo " "
					cat  /tmp/.sceupdatelist | sort | uniq
					echo " "
					echo -n "Press Enter to update above SCE(s), (v)iew package updates, Ctrl-C aborts: "
					read ans
					echo " "
					if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
						VIEWED=TRUE
						more -s /tmp/updateavailable
						echo " "
						echo -n "Press Enter to proceed with updates, Ctrl-C aborts: "
						read ans
						echo " "
					fi
					for I in `cat /tmp/.sceupdatelist`; do
						if [ -f /tmp/"$I".recommended ]; then
							OPTIONS="$OPTIONS"R
						fi
						if [ -f /tmp/"$I".suggested ]; then
							OPTIONS="$OPTIONS"S
						fi
						if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
							sce-import -"$OPTIONS"Nnp "$I"
							if [ -s /tmp/sceimporterror ]; then
								echo "Error in updating "$I"" >> /tmp/sceupdateerror
								cat /tmp/sceimporterror >> /tmp/sceupdateerror
								echo " " >> /tmp/sceupdateerror
								continue
							fi
						fi
						echo "$I" >> /tmp/importupdated
					done
				fi
			else
				echo " "
				echo "Update check completed."
				echo " "
				cat  /tmp/.sceupdatelist | sort | uniq
				echo " "
				echo -n "Press Enter to view package update details, Ctrl-C aborts: "
				read ans
				if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
					more -s /tmp/updateavailable
				fi
				exit 0
			fi
		else
			:
		fi
			unset DEPLIST
		if [ ! -s /tmp/.sceupdatelist ]; then
			echo " "
			echo "No updates available for chosen SCE(s) at this time."
		else
			if [ "$VIEWED" != "TRUE" ]; then
				echo " "
				echo -n "Press Enter to review available updates, (n)o to exit: "
				read ans
				if [ "$ans" == "" ]; then
					more -s /tmp/updateavailable
				fi
			fi			
		fi
	fi

elif [ -f update/"$UPDATETARGET".sce ]; then
	cd update/
	echo "Checking ${YELLOW}"$UPDATETARGET".sce${NORMAL} for updates."
	unset DEPLIST
	getDeps "$UPDATETARGET"
	for I in `echo "$DEPLIST"`; do
		if [ -f update/"$I".sce.debinx ] && [ -f update/"$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat update/"$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		elif [ -f "$I".sce.debinx ] && [ -f "$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat "$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		fi
		echo -n "         ${YELLOW}"$I".sce${NORMAL} update check."
		sudo importupdatecheck "$I" > /dev/null 2>&1 & /usr/bin/rotdash $!
		echo " "
	done
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$CHECKONLY" != "TRUE" ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			else
				echo " "
				echo "Update check completed."
				echo " "
				cat  /tmp/.sceupdatelist | sort | uniq
				echo " "
                                echo -n "Press Enter to update above SCE(s), (v)iew package updates, Ctrl-C aborts: "
				read ans
				echo " "
				if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
					more -s /tmp/updateavailable
					echo " "
					echo -n "Press Enter to proceed with updates, Ctrl-C aborts: "
					read ans
					echo " "
				fi
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			fi
		else
			echo " "
			echo "Update check completed."
			echo " "
			cat  /tmp/.sceupdatelist | sort | uniq
			echo " "
			echo -n "Press Enter to view package update details, Ctrl-C aborts: "
			read ans
			if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
				more -s /tmp/updateavailable
			fi
			exit 0
		fi
	else
		echo " "
		echo "No updates available for "$UPDATETARGET".sce or any dependency SCE(s)."
	fi
	cd ..
elif [ -f "$UPDATETARGET".sce ]; then
	echo " "
	echo "Checking ${YELLOW}"$UPDATETARGET".sce${NORMAL} for updates."
	unset DEPLIST
	getDeps "$UPDATETARGET"
	for I in `echo "$DEPLIST"`; do
		if [ -f update/"$I".sce.debinx ] && [ -f update/"$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat update/"$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		elif [ -f "$I".sce.debinx ] && [ -f "$I".sce.md5.txt ]; then
			MDNEW=`md5sum /etc/sysconfig/tcedir/import/debinx/NEWDEBINX | cut -f1 -d" "`
			MDOLD=`cat "$I".sce.debinx`
			if [ "$MDOLD" == "$MDNEW" ]; then
				echo "         ${YELLOW}"$I".sce${NORMAL} is up to date."
				continue
			fi
		fi
		echo -n "         ${YELLOW}"$I".sce${NORMAL} update check."
		sudo importupdatecheck "$I" > /dev/null 2>&1 & /usr/bin/rotdash $!
		echo " "
	done
	if [ -s /tmp/.sceupdatelist ]; then
		if [ "$CHECKONLY" != "TRUE" ]; then
			if [ "$NONINTERACTIVE" == "TRUE" ]; then
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			else
				echo " "
				echo "Update check completed."
				echo " "
				cat  /tmp/.sceupdatelist | sort | uniq
				echo " "
				echo -n "Press Enter to update above SCE(s), (v)iew package updates, Ctrl-C aborts: "
				read ans
				echo " "
				if [ "$ans" == "v" ] || [ "$ans" == "view" ]; then
					more -s /tmp/updateavailable
					echo " "
					echo -n "Press Enter to proceed with updates, Ctrl-C aborts: "
					read ans
					echo " "
				fi
				for I in `cat /tmp/.sceupdatelist`; do
					if [ -f /tmp/"$I".recommended ]; then
						OPTIONS="$OPTIONS"R
					fi
					if [ -f /tmp/"$I".suggested ]; then
						OPTIONS="$OPTIONS"S
					fi
					if ! grep "^$I$" /tmp/importupdated > /dev/null 2>&1; then
						sce-import -"$OPTIONS"Nnp "$I"
						if [ -s /tmp/sceimporterror ]; then
							echo "Error in updating "$I"" >> /tmp/sceupdateerror
							cat /tmp/sceimporterror >> /tmp/sceupdateerror
							echo " " >> /tmp/sceupdateerror
							continue
						fi
					fi
					echo "$I" >> /tmp/importupdated
				done
			fi
		else
			echo " "
			echo "Update check completed."
			echo " "
			cat  /tmp/.sceupdatelist | sort | uniq
			echo " "
			echo -n "Press Enter to view package update details, Ctrl-C aborts: "
			read ans
			if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
				more -s /tmp/updateavailable
			fi
			exit 0
		fi
	else
		echo " "
		echo "No updates available for "$UPDATETARGET".sce or any dependency SCE(s)."
	fi
else
	echo " "
	echo "${YELLOW}WARNING:${NORMAL} '"$UPDATETARGET"' is not an existing SCE file, exiting.."
fi

if [ -s /tmp/sceupdateerror ]; then
	echo -n "Errors occured during sce-update, press Enter to view them, Ctrl-C aborts:"
	read ans
	if [ "$ans" == "" ] || [ "$ans" == "v" ] || [ "$ans" == "V" ]; then
		more -s /tmp/sceupdateerror
	fi
fi

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

cleanup
