#!/bin/busybox ash
# (c) Jason W 2013 

sudo chmod 1777 /tmp
. /etc/init.d/tc-functions
useBusybox
TCEDIR=/etc/sysconfig/tcedir
SCE="$1"
SCEDIR="$TCEDIR"/sce
BUILD=`getBuild`
read SSMIRROR < /opt/tcemirror                                                                             
SSMIRROR="${SSMIRROR%/}/dCore/import"   

pgrep updatesce > /dev/null 2>&1 || exit 1


[ -f /tmp/sce.md5new ] && sudo rm /tmp/sce.md5new
[ -f /tmp/file.lst ] && sudo rm /tmp/file.lst
[ -f /tmp/ssupdates ] && sudo rm /tmp/ssupdates
ls /tmp/*.deb2sce > /dev/null 2>&1 && sudo rm /tmp/.*deb2sce
[ -f /tmp/.pkgprebuilt ] && sudo rm /tmp/.pkgprebuilt
[ -f /tmp/.importupdates ] && sudo rm /tmp/.importupdates


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


if [ ! -f "$SCEDIR"/"$SCE".sce ] && [ ! -f "$SCEDIR"/update/"$SCE".sce ]; then
  echo ""$SCE".sce does not exist, exiting.."
  exit 1
fi


if sudo mount | grep "/tmp/sceupdatetest " > /dev/null 2>&1; then
 sudo umount /tmp/sceupdatetest
fi

if [ -f "$SCEDIR"/update/"$SCE".sce ]; then
   [ -d /tmp/sceupdatetest ] || mkdir /tmp/sceupdatetest
   sudo mount "$SCEDIR"/update/"$SCE".sce /tmp/sceupdatetest
   DATADIR=/tmp/sceupdatetest/usr/local/sce/"$SCE"
   FILE=/tmp/sceupdatetest/usr/local/sce/"$SCE"/"$SCE".md5sum
   FILEDATA=/tmp/sceupdatetest/usr/local/sce/"$SCE"/"$SCE"-data.md5sum
   SSDIR="/tmp/sceupdatetest/usr/local/tce.installed"
elif sudo mount | grep "/tmp/tcloop/$SCE " > /dev/null 2>&1; then
   DATADIR=/tmp/tcloop/"$SCE"/usr/local/sce/"$SCE"
   FILE=/tmp/tcloop/"$SCE"/usr/local/sce/"$SCE"/"$SCE".md5sum
   FILEDATA=/tmp/tcloop/"$SCE"/usr/local/sce/"$SCE"/"$SCE"-data.md5sum
   SSDIR="/tmp/tcloop/"$SCE"/usr/local/tce.installed"
elif [ -f "$SCEDIR"/"$SCE".sce ]; then
   DATADIR=/tmp/sceupdatetest/usr/local/sce/"$SCE"
   [ -d /tmp/sceupdatetest ] || mkdir /tmp/sceupdatetest
   sudo mount "$SCEDIR"/"$SCE".sce /tmp/sceupdatetest
   FILE=/tmp/sceupdatetest/usr/local/sce/"$SCE"/"$SCE".md5sum
   FILEDATA=/tmp/sceupdatetest/usr/local/sce/"$SCE"/"$SCE"-data.md5sum
   SSDIR="/tmp/sceupdatetest/usr/local/tce.installed"
fi

if [ ! -f "$FILE" ]; then
   echo ""$1" does not have the needed md5sum list, exiting.."
   if sudo mount | grep "/tmp/sceupdatetest" > /dev/null 2>&1; then
     sudo umount /tmp/sceupdatetest
   fi
   [ -d /tmp/sceupdatetest ] && sudo rmdir /tmp/sceupdatetest
   exit 1
fi

[ -d /tmp/work ] && sudo rm -r /tmp/work

if [ -f "$TCEDIR"/sce/"$SCE".sce.lst ]; then
	for I in `cat "$TCEDIR"/sce/"$SCE".sce.lst`; do 
		sudo debGetDeps "$I"
	done
else
	sudo debGetDeps "$SCE"
fi

for I in `ls /tmp/work`; do
	if ! grep "^$I$" "$DATADIR"/*.debs > /dev/null 2>&1; then
		sudo debGetUpdateList "$I"
	fi 

done 

echo " " 

echo "SCE:  "$SCE"" >> /tmp/updateavailable
echo "Available package updates:" >> /tmp/updateavailable
while read line; do 
	E=`echo $line | cut -f1 -d:`
        F=`echo $line | awk '{print $2}'`
    	G=`grep "^$E:" "$FILE" | awk '{print $2}'`


     	if [[ "$F" != "$G" ]] && ! grep "^$E$" /tmp/PKGEXCLUDELIST > /dev/null 2>&1; then
     		echo "$E" >> /tmp/.importupdates
     
        fi
	NAME="$E"
	if [ -f "$FILEDATA" ]; then
		if grep "^$E-deb2sce:" /tmp/PKGDATAFILEMD5SUMLIST > /dev/null 2>&1; then
			MD5SSNEW=`grep "^$E-deb2sce:" /tmp/PKGDATAFILEMD5SUMLIST | awk '{print $2}'`
			MD5SSOLD=`grep "^$E-deb2sce:" "$FILEDATA" | awk '{print $2}'`
			if ! grep "^$E-deb2sce:" "$FILEDATA" > /dev/null 2>&1; then
				MD5SSOLD="0"
			fi
			if [ "$MD5SSNEW" != "$MD5SSOLD" ]; then
				if ! grep "^$E$" /tmp/ssupdates; then
					echo "$E" >> /tmp/ssupdates
					echo "    "$E"   Updated startup script update available." >>  /tmp/updateavailable 
				fi
			fi
		fi

		if grep "^$E"-data: /tmp/PKGDATAFILEMD5SUMLIST > /dev/null 2>&1; then
	        	MD5DATANEW=`grep "^$E-data:" /tmp/PKGDATAFILEMD5SUMLIST | awk '{print $2}'`
			MD5DATAOLD=`grep "^$E-data:" "$FILEDATA" | awk '{print $2}'`
			if ! grep "^$E-data:" "$FILEDATA" > /dev/null 2>&1; then
				MD5DATAOLD="0"
			fi
	        	if [ "$MD5DATANEW" != "$MD5DATAOLD" ]; then
			 	if ! grep "^$E$" /tmp/ssupdates; then
					echo "$E" >> /tmp/ssupdates
			   		echo "    "$E"   Updated "$E"-data.tar.gz extra files update available." >>  /tmp/updateavailable 
	        		fi	
			fi
		fi
	fi 
		
done < /tmp/sce.md5new

if [ -s /tmp/.importupdates ]; then
	for I in `cat /tmp/.importupdates | sort | uniq`; do 
		A=`grep "^$I;" /tmp/.importpkgtype | cut -f2 -d";"`
		echo "   "$I"       "$A" " >> /tmp/updateavailable
 	done  
 	grep "^$1:" /tmp/.sceupdatelist > /dev/null 2>&1 || echo "$1:" >> /tmp/.sceupdatelist
fi


if [ -s /tmp/ssupdates ]; then
	grep "^$1:" /tmp/.sceupdatelist > /dev/null 2>&1 || echo "$1:" >> /tmp/.sceupdatelist
fi

if [ ! -s /tmp/.importupdates ] && [ ! -s /tmp/ssupdates ]; then
	echo "None" >> /tmp/updateavailable
fi

echo " " >> /tmp/updateavailable
echo " " >> /tmp/updateavailable

mount | grep /tmp/sceupdatetest > /dev/null 2>&1 && sudo umount /tmp/sceupdatetest
[ -f /tmp/sce.md5new ] &&   sudo rm /tmp/sce.md5new
[ -f /tmp/.importupdates ] && sudo rm /tmp/.importupdates
[ -f /tmp/ssupdates ] && sudo rm /tmp/ssupdates
[ -f "/tmp/.*deb2sce" ] && sudo rm /tmp/.*deb2scew
[ -f "/tmp/*.md5sum" ] && sudo rm /tmp/*.md5sum
[ -f "/tmp/*.pkglist" ] && sudo rm /tmp/*.pkglist
[ -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

sudo chmod 1777 /tmp
cd "$SCEDIR"
