#!/bin/busybox ash
. /etc/init.d/tc-functions

BUILD=`getBuild`
PATH="/bb:/bin:/sbin:/usr/bin:/usr/sbin"
export PATH
PORT="$(cat /usr/share/doc/tc/release.txt | cut -f1 -d:)"
VERSION="$(cat /usr/share/doc/tc/release.txt | cut -f2 -d:)"
RESULTS="$PORT:$VERSION"
BOOTPATH="$(cat /etc/sysconfig/boot_path | grep -v '^#' | grep BOOTPATH | cut -f2 -d=)" > /dev/null 2>&1
MOUNTPOINT="$(cat /etc/sysconfig/boot_path | grep -v '^#' | grep BOOTPATH | cut -f3 -d/)" > /dev/null 2>&1
MAXBACKUPS="$(cat /etc/sysconfig/boot_path | grep -v '^#' | grep MAXBACKUPS | cut -f2 -d=)" > /dev/null 2>&1

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

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo " "
	echo "${YELLOW}version - Confirm dCore version, check for release or release candidate (RC)"
	echo "          updates, prompted to download an available update to the directory"
	echo "          specified in /etc/sysconfig/boot_path.${NORMAL} Review this file for details."


	echo "          The partition will auto-mount if /mnt/* is included in the pathway"
	echo "          and the entry exists in /etc/fstab. The existing dCore*.gz and"
	echo "          *.md5.txt files are automatically backed up before the update"
	echo "          is downloaded. Number of backups kept can also be specified."
	echo "          Reboot system to complete the update."
	echo " "
	echo "Usage:"
	echo " "
	echo "${YELLOW}"version"${NORMAL}     Display dCore name and version being utilized."
	echo "${YELLOW}"version -c"${NORMAL}  Check if using the latest stable release, if different version"
	echo "            available prompted to download, wget latest stable release and"
	echo "            md5.txt file to directory specified in /etc/sysconfig/boot_path."
	echo "${YELLOW}"version -r"${NORMAL}  Check if using the latest release candidate, if an update is"
	echo "            available prompted to download, wget latest release candidate and"
	echo "            md5.txt file to directory specified in /etc/sysconfig/boot_path."
	echo "${YELLOW}"version -l"${NORMAL}  Display dCore name and version being utilized."
	echo "${YELLOW}"version -s"${NORMAL}  Display dCore version being utilized."
	echo " "
exit 1
fi

while getopts rcls OPTION
do
	case ${OPTION} in
		r) LATEST=`wget -q -O - $(cat /opt/tcemirror)/dCore/"$BUILD"/release_candidates/"$PORT"/"$PORT".latest 2>/dev/null`
			if [ "$?" == 0 ]; then
				RUNNING="${RESULTS##*:}"
				if [ "$LATEST" != "$RESULTS" ]; then
					echo "${YELLOW}Warning:${NORMAL}"
					echo "System using:              "$RESULTS""
					echo "Latest release candidate:  "$LATEST""
					echo " "
					if [ -f /tmp/dCoreUpdateDownloadedReboot! ]; then
						echo "${YELLOW}Update already downloaded${NORMAL} to "$BOOTPATH"."
						echo " "
						echo "Confirm correct boot path, reboot system to complete update."
						exit 1
					fi
					echo "dCore*.gz download path from /etc/sysconfig/boot_path (MAXBACKUPS = "$MAXBACKUPS"):"
					if [ ! "$BOOTPATH" ]; then
						echo "Not specified, create /etc/sysconfig/boot_path entry, exiting.."
						exit 1
					else
						echo ""$BOOTPATH""
					fi
					echo " "
					echo "Press Enter to download the most recent release candidate, existing dCore*.gz"
					echo -n "and *.md5.txt files will automatically be backed up, (q)uit to exit: "
					read TARGET
					if [ "$TARGET" == "Q" ] || [ "$TARGET" == "q" ]; then
						exit
					fi				
					mount /dev/"$MOUNTPOINT" > /dev/null 2>&1
					sudo mkdir -p "$BOOTPATH"
					cd "$BOOTPATH"
					sudo mv ""$PORT".gz.md5.txt" ""$PORT".gz.md5.txt."$VERSION"" > /dev/null 2>&1
					sudo mv ""$PORT".gz" ""$PORT".gz."$VERSION"" > /dev/null 2>&1
					echo " "
					sudo -E wget -c $(cat /opt/tcemirror)/dCore/"$BUILD"/release_candidates/"$PORT"/"$PORT".gz.md5.txt
					echo " "
					sudo -E wget -c $(cat /opt/tcemirror)/dCore/"$BUILD"/release_candidates/"$PORT"/"$PORT".gz
					echo " "
					if [ -f ""$BOOTPATH"/"$PORT".gz.md5.txt" ]; then
						echo "Download success: "$BOOTPATH"/"$PORT".gz.md5.txt"
						NUMBACK="$(ls dCore*.gz.md5.txt.2*.*.*.*.* | wc -l)" > /dev/null 2>&1
						while [ "$NUMBACK" -gt "$MAXBACKUPS" ]
						do
							ls -1t dCore*.gz.md5.txt.2*.*.*.*.* | tail -1 | xargs sudo /bin/rm -f
							NUMBACK="$(ls dCore*.gz.md5.txt.2*.*.*.*.* | wc -l)"
						done
					else
						echo "Download error:   "$BOOTPATH"/"$PORT".gz.md5.txt"
					fi
					if [ -f ""$BOOTPATH"/"$PORT".gz" ]; then
						echo "Download success: "$BOOTPATH"/"$PORT".gz"
						sudo touch /tmp/dCoreUpdateDownloadedReboot!
						NUMBACK="$(ls dCore*.gz.2*.*.*.*.* | wc -l)" > /dev/null 2>&1
						while [ "$NUMBACK" -gt "$MAXBACKUPS" ]
						do
							ls -1t dCore*.gz.2*.*.*.*.* | tail -1 | xargs sudo /bin/rm -f
							NUMBACK="$(ls dCore*.gz.2*.*.*.*.* | wc -l)"
						done
					else
						echo "Download error:   "$BOOTPATH"/"$PORT".gz"
					fi
					MD5REPORTED=`cat "$BOOTPATH"/"$PORT".gz.md5.txt | awk '{print $1}'`
					MD5SUM=`md5sum "$BOOTPATH"/"$PORT".gz | awk '{print $1}'`
					echo "Reported md5sum:  "$MD5REPORTED""
					echo "Actual md5sum:    "$MD5SUM""				
					echo " "
					echo "Correct any errors, confirm boot path, reboot to complete update."
				else
					echo "System using "$RESULTS", the latest release candidate."
				fi
			
			else
				exit_tcnet
			fi
			exit
			;;
		c) LATEST=`wget -q -O - $(cat /opt/tcemirror)/dCore/"$BUILD"/release/"$PORT"/"$PORT".latest 2>/dev/null`
			if [ "$?" == 0 ]; then
				RUNNING="${RESULTS##*:}"
				if [ "$LATEST" != "$RESULTS" ]; then
					echo "${YELLOW}Warning:${NORMAL}"
					echo "System using:    "$RESULTS""
					echo "Latest release:  "$LATEST""
					echo " "
					if [ -f /tmp/dCoreUpdateDownloadedReboot! ]; then
						echo "${YELLOW}Update already downloaded${NORMAL} to "$BOOTPATH"."
						echo " "
						echo "Confirm correct boot path, reboot system to complete update."
						exit 1
					fi
					echo "dCore*.gz download path from /etc/sysconfig/boot_path (MAXBACKUPS = "$MAXBACKUPS"):"
					if [ ! "$BOOTPATH" ]; then
						echo "Not specified, create /etc/sysconfig/boot_path entry, exiting.."
						exit 1
					else
						echo ""$BOOTPATH""
					fi
					echo " "
					echo "Press Enter to download the most recent stable release, existing dCore*.gz"
					echo -n "and *.md5.txt files will automatically be backed up, (q)uit to exit: "
					read TARGET
					if [ "$TARGET" == "Q" ] || [ "$TARGET" == "q" ]; then
						exit
					fi
					mount /dev/"$MOUNTPOINT" > /dev/null 2>&1
					sudo mkdir -p "$BOOTPATH"
					cd "$BOOTPATH"
					sudo mv ""$PORT".gz.md5.txt" ""$PORT".gz.md5.txt."$VERSION"" > /dev/null 2>&1
					sudo mv ""$PORT".gz" ""$PORT".gz."$VERSION"" > /dev/null 2>&1
					echo " "
					sudo -E wget -c $(cat /opt/tcemirror)/dCore/"$BUILD"/release/"$PORT"/"$PORT".gz.md5.txt
					echo " "
					sudo -E wget -c $(cat /opt/tcemirror)/dCore/"$BUILD"/release/"$PORT"/"$PORT".gz
					echo " "
					if [ -f ""$BOOTPATH"/"$PORT".gz.md5.txt" ]; then
						echo "Download success: "$BOOTPATH"/"$PORT".gz.md5.txt"
						NUMBACK="$(ls dCore*.gz.md5.txt.2*.*.*.*.* | wc -l)" > /dev/null 2>&1
						while [ "$NUMBACK" -gt "$MAXBACKUPS" ]
						do
							ls -1t dCore*.gz.md5.txt.2*.*.*.*.* | tail -1 | xargs sudo /bin/rm -f
							NUMBACK="$(ls dCore*.gz.md5.txt.2*.*.*.*.* | wc -l)"
						done
					else
						echo "Download error:   "$BOOTPATH"/"$PORT".gz.md5.txt"
					fi
					if [ -f ""$BOOTPATH"/"$PORT".gz" ]; then
						echo "Download success: "$BOOTPATH"/"$PORT".gz"
						sudo touch /tmp/dCoreUpdateDownloadedReboot!
						NUMBACK="$(ls dCore*.gz.2*.*.*.*.* | wc -l)" > /dev/null 2>&1
						while [ "$NUMBACK" -gt "$MAXBACKUPS" ]
						do
							ls -1t dCore*.gz.2*.*.*.*.* | tail -1 | xargs sudo /bin/rm -f
							NUMBACK="$(ls dCore*.gz.2*.*.*.*.* | wc -l)"
						done
					else
						echo "Download error:   "$BOOTPATH"/"$PORT".gz"
					fi
					MD5REPORTED=`cat "$BOOTPATH"/"$PORT".gz.md5.txt | awk '{print $1}'`
					MD5SUM=`md5sum "$BOOTPATH"/"$PORT".gz | awk '{print $1}'`
					echo "Reported md5sum:  "$MD5REPORTED""
					echo "Actual md5sum:    "$MD5SUM""				
					echo " "
					echo "Correct any errors, confirm boot path, reboot to complete update."
				else
					echo "System using "$RESULTS", the latest release."
				fi
			else
				exit_tcnet
			fi
			exit
			;;
		l) echo "$RESULTS"
			exit
			;;
		s) echo "${RESULTS##*:}"
			exit
			;;
		*) echo "Run  version --help  for usage information."
			;;
	esac
done
echo "$RESULTS"
exit

