# !/bin/bash
# Switch between original and patched installds
# Changelog at http://secretss.com/switchrd
# v3.4.6 - 2nd July 2010 - by Secretss
#
# Thanks go to rcg, Helpmy360isEMO, iBlaze
# ChingPow, applefour, ShapeShifter499, Lswpb, diytto, artypig78, Jeroenz0r, ttwj

cd /usr/libexec

#########################################################################################01#
function download {
	echo -e "What would you like to download? (1 or 2):
\t1 = Original installd
\t2 = Patched  installd"
	read ans2
	if [ $ans2 != 1 ] && [ $ans2 != 2 ]; then
		echo -e "Please input either '1' or '2'.\n"
		exit
	elif [ $ans2 == 1 ]; then
		echo -e "Please enter the integer corresponding to\nyour device model (1 - 7):
\t1 = iPodTouch 1G
\t2 = iPodTouch 2G
\t3 = iPodTouch 3G
\t4 = iPhone
\t5 = iPhone 3G
\t6 = iPhone 3GS
\t7 = iPad (OS 3.2)"
		read num
		case $num in
		1)
			devicemodel="iPod Touch 1G"
		;;
		2)
			devicemodel="iPod Touch 2G"
		;;
		3)
			devicemodel="iPod Touch 3G"
		;;
		4)
			devicemodel="iPhone"
		;;
		5)
			devicemodel="iPhone 3G"
		;;
		6)
			devicemodel="iPhone 3GS"
		;;
		7)
			devicemodel="iPad"
		;;
		*)
			echo -e "Please input an integer from 1 to 7.\n"
			exit
		;;
		esac
		file=$(echo $devicemodel | tr A-Z a-z | tr -d ' ')
		if [ -f installd.backup ]; then
			echo -e "Installd.backup exists.\nPlease choose an option:
\t1 = Overwrite existing file"
		else
			echo -e "How would you like to download?
\t1 = Download as is"
		fi
		echo -e "\t2 = Download as installd.$file
\t3 = Download to /var/mobile"
		read ans4
		case $ans4 in
		1)
			downloadfileo installd.backup
			echo -e "\nIf you selected the wrong device model, please\ndelete installd.backup and run this process again."
		;;
		2)
			downloadfileo installd.$file
		;;
		3)
			cd /var/mobile
			downloadfileo installd.$file
			cd /usr/libexec
		;;
		*)
			echo -e "Please input an integer from 1 to 3.\n"
			exit
		;;
		esac
		echo  
		checkd
	elif [ $ans2 == 2 ]; then
		downloadp		
	fi
}

#########################################################################################01#
function downloadfileo(){
	echo -e "\nDownloading original installd file for\n\t$devicemodel.\nPlease do not interrupt the process.\n"
	wget -N -q http://dl.dropbox.com/u/1838452/original/$num/$1
	chmod 755 $1
	echo -e "Original installd file for $devicemodel\ndownloaded to \n$(pwd)/$1."
}

function downloadfilep(){
	echo -e "\nDownloading patched installd file.\nPlease do not interrupt the process.\n"
	wget -N -q http://dl.dropbox.com/u/1838452/patched/$ipad/$1
	chmod 755 $1
	echo -e "Patched installd file downloaded to\n$(pwd)/$1."
}

#########################################################################################00#
function downloadp {
	echo -e "Are you on an iPad (OS 3.2)? (Y/N):"
	read ipad
	if [ $ipad != n ] && [ $ipad != N ] && [ $ipad != y ] && [ $ipad != Y ]; then
		echo -e "Please input either Y for yes or N for no.\n"
	fi
	if [ -f installd.patched ]; then
		echo -e "Installd.patched exists.\nPlease choose an option:
\t1 = Overwrite existing file
\t2 = Download as installd.patched1
\t3 = Download to /var/mobile"
		read ans5
		if [ $ans5 != 1 ] && [ $ans5 != 2 ] && [ $ans5 != 3 ]; then
			echo -e "Please input an integer from 1 to 3.\n"
			exit
		elif [ $ans5 == 1 ]; then
			downloadfilep installd.patched
		elif [ $ans5 == 2 ]; then
			downloadfilep installd.patched1
		elif [ $ans5 == 3 ]; then
			cd /var/mobile
			downloadfilep installd.patched
			cd /usr/libexec
		fi
	else
		downloadfilep installd.patched
	fi
	echo  
	checkd
}

#########################################################################################01#
function checkd {
	if [ -f installd.backup ] && [ -f installd.patched ]; then
		echo -e "Found coexisting:\ninstalld.backup and installd.patched.\nPlease select an option (1 - 5):
\t1 = Put original installd in place
\t2 = Put patched  installd in place
\t3 = Rename installd to patched and
\t    put original in place
\t4 = Rename installd to backup  and
\t    put patched  in place
\t5 = Ignore anomaly"
		read ans3
		if [ $ans3 != 1 ] && [ $ans3 != 2 ] && [ $ans3 != 3 ] && [ $ans3 != 4 ] && [ $ans3 != 5 ]; then
			echo -e "Please input an integer from 1 to 5.\n"
			exit
		elif [ $ans3 == 1 ]; then
			mv -f installd.backup installd
		elif [ $ans3 == 2 ]; then
			mv -f installd.patched installd
		elif [ $ans3 == 3 ]; then
			mv -f installd installd.patched
			mv -f installd.backup installd
		elif [ $ans3 == 4 ]; then
			mv -f installd installd.backup
			mv -f installd.patched installd
		elif [ $ans3 == 5 ]; then
			echo -e "Please note that this script may not work\nas it should until you fix this anomaly.\n"
			exit
		fi
		if [ -f installd.backup ]; then
			echo Patched installd currently in place.
		elif [ -f installd.patched ]; then
			echo Original installd currently in place.
		fi
		echo  
	fi
}

#########################################################################################01#
function help {
	echo -e "Usage: switchrd -o\n       -to put original installd in place"
	echo -e "Usage: switchrd -p\n       -to put patched  installd in place"
	echo -e "Usage: switchrd -d\n       -to download installd files"
	echo -e "Usage: switchrd -u\n       -to update Switchrd"
	echo -e "Usage: switchrd -h\n       -to display this text\n"
	echo -e "v3.4.6 Written by Secretss\nContact Secretss[C4iD] on Rizon IRC server\nor @Secretss_ on Twitter.\n"
}

#########################################################################################01#
echo  

if [ "$(id -u)" != "0" ]; then
	echo -e "This script must be run as root user.\n"
	exit
fi

if [ ! -e /usr/bin/wget ]; then
	echo Installing necessary tools...
	if [ ! -e /usr/bin/apt-get ]; then
	     echo -e "Unable to automatically install tools,\nplease install APT 0.7 Strict from Cydia.\n"
	     exit
	fi
	apt-get -qq install wget
	if [ ! -e /usr/bin/wget]; then
	     echo -e "Unable to install wget.\nAre you connected to the Internet?"
	else
	     echo -e "Done.\n"
	fi
fi

if [ -f installd.bak ]; then
	mv -f installd.bak installd.backup
fi

checkd

if [ -f installd.backup ] || [ -f installd.patched ]; then
	continue
else
	echo -e "Not Found:\ninstalld.backup and installd.patched.\n"
	echo -e "You do not have a patched installd.\nWould you like to download now? (Y/N):"
	read ans
	if [ $ans != n ] && [ $ans != N ] && [ $ans != y ] && [ $ans != Y ]; then
		echo -e "Please input either Y for yes or N for no.\n"
	elif [ $ans == n ] || [ $ans == N ]; then
		echo -e "You do not have the appropriate files\nto commence switching.\n"
	elif [ $ans == y ] || [ $ans == Y ]; then
		downloadp
	fi
	exit
fi

#########################################################################################01#
case "$1" in "")
help
;;
-o)
	if [ -f installd.patched ]; then
		echo -e "Original installd currently in place.\nNo changes has been made.\n"
	else
		mv -f installd installd.patched
		mv -f installd.backup installd
		echo -e "Installd successfully switched to original.\n"
	fi
;;
-p)
	if [ -f installd.backup ]; then
		echo -e "Patched installd currently in place.\nNo changes has been made.\n"
	else
		mv -f installd installd.backup
		mv -f installd.patched installd
		echo -e "Installd successfully switched to patched.\n"
	fi
;;
-u)
	cd /usr/bin
	before=$(md5sum switchrd)
	echo -e "Checking for updates."
	wget -N -q http://dl.dropbox.com/u/1838452/switchrd
	chmod 755 switchrd
	if [ "$before" != "$(md5sum switchrd)" ]; then
		echo -e "Switchrd has been successfully updated.\n"
		exit
	else
		echo -e "No updates available.\n"
		exit
	fi
;;
-d)
	download
;;
*)
	help
;;
esac
exit
