#!/bin/busybox sh
#
# Chromecast Auto Rooter Installer Script
# By Chris Blake <chrisrblake93 (at) gmail.com> 2015
# Licensed under the GPL v3 License

# Dir used to hold files
tempdir="/data/temp"

# Path to latest release image
ImgDL="http://pdl.team-eureka.com/recovery/latest.txt"
ImgSave="$tempdir/root-recovery.img"

# Path to Google OTA we will use for this
OTADL="http://cache.pack.google.com/edgedl/googletv-eureka/stable-channel/ota.46278.stable-channel.eureka-b3.ef7fdf1f7153dacecc77b7d1a3bb22a97e27ec7e.zip"
OTASave="$tempdir/ota.46278.stable-channel.eureka-b3.ef7fdf1f7153dacecc77b7d1a3bb22a97e27ec7e.zip"

# Prefixed log messages are easier to distinguish
pLog() {
		echo "AutoRoot-Recovery: $@"
		echo "$@" >> /cache/autoroot-installer.log
}

# Remove all the things, in case of error or completion
CleanHouse() {
	# Delete the failed update if it exists
	if [ -f "$ImgSave" ]
	then
		rm $ImgSave
	fi
	# Delete the failed update md5 if it exists
	if [ -f "$ImgSave.md5" ]
	then
		rm $ImgSave.md5
	fi
	# Delete the failed google OTA if it exists
	if [ -f "$OTASave" ]
	then
		rm $OTASave
	fi
	# Delete tempdir
	if [ ! -d "$tempdir" ]; then
		busybox rm -fr $tempdir
	fi

}

# Check that SHA1
contains() {
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        return 0    # $substring is in $string
    else
        return 1    # $substring is not in $string
    fi
}

pLog "Running AutoRoot-Recovery Installer!"

if busybox expr $(EurekaSettings get DNS useDHCP) = "0"; then
	pLog "Dumping DNS settings to new format..."
	EurekaSettings get DNS primary > /data/dns.conf
	echo "" >> /data/dns.conf
	EurekaSettings get DNS secondary >> /data/dns.conf
fi

if [ ! -d "$tempdir" ]; then
	mkdir $tempdir
fi

# Parse latest file to DL link only
ImgDL=`busybox wget -q ${ImgDL} -O - | busybox awk '{ print $2 }'`

pLog "Downloading now..."
busybox wget -q "$ImgDL" -O "$ImgSave"
if [ $? -ne 0 ];
then
	pLog "Error Downloading! Exiting..."
	CleanHouse
	exit 1
else
	#Download was good, now download MD5 and check
	pLog "Recovery Downloaded Successfully!"
	pLog "Verifiying Recovery..."

	busybox wget -q "$ImgDL.md5" -O "$ImgSave.md5"
	# Did MD5 Download Successfully?
	if [ $? -ne 0 ];
	then
		pLog "Error Downloading MD5, Exiting..."
		CleanHouse
		exit 1
	else
		# Check of MD5 is OK
		MD1=`busybox md5sum -c "$ImgSave.md5" | busybox awk '{ print $2 }'`

		# Compare MD5's
		if [ "$MD1" != "OK" ]
		then
			# Bad MD5 Match
			pLog "Failed to verify, Exiting..."
			CleanHouse
			exit 1
		else
			# All went good
			pLog "File Verified Successfully!"

			# Download Google OTA
			pLog "Downloading Google OTA..."
			busybox wget -q "$OTADL" -O "$OTASave"
			if [ $? -ne 0 ];
			then
				pLog "Error Downloading Google OTA, Exiting..."
				CleanHouse
				exit 1
			else
				pLog "Verifying Google OTA..."
				SHA=`busybox sha1sum "$OTASave" | busybox awk '{ print $1 }'`
				if contains "$OTASave" "$SHA"; then
					pLog "Google OTA Verified! Moving file..."
					busybox cp "$OTASave" /cache/ota.zip

					pLog "Flashing Recovery..."
					flash_image --scan-all recovery /data/temp/root-recovery.img

					pLog "Process Complete! Cleaning and rebooting..."
					CleanHouse
					sleep 5
					reboot recovery
					exit 0
				else
					pLog "Failed to verify OTA! Exiting..."
					CleanHouse
					exit 1
				fi
			fi
		fi
	fi
fi

