# !/bin/bash
# A script to switch installd between original and patched
# Changelog at http://secretss.com/switchrd
# v3.4.5b - 18th April 2010
#
# Thanks go to rcg, Helpmy360isEMO, iBlaze
# ChingPow, applefour, ShapeShifter499, Lswpb, diytto, artypig78, Jeroenz0r

scriptname="switchrd"
cd /usr/libexec

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 - 6):
\t1 = iPodTouch 1G
\t2 = iPodTouch 2G
\t3 = iPodTouch 3G
\t4 = iPhone
\t5 = iPhone 3G
\t6 = iPhone 3GS"
		read num
		if [ $num != 1 ] && [ $num != 2 ] && [ $num != 3 ] && [ $num != 4 ] && [ $num != 5 ] && [ $num != 6 ]; then
			echo -e "Please input an integer from 1 to 6.\n"
			exit
		fi
		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"
		;;
		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
		if [ $ans4 != 1 ] && [ $ans4 != 2 ] && [ $ans4 != 3 ]; then
			echo -e "Please input an integer from 1 to 3.\n"
			exit
		elif [ $ans4 == 1 ]; then
			downloadfileo installd.backup
			echo -e "\nIf there was an error in selecting your\ndevice model, please delete installd.backup\nusing iFile from Cydia and run this\nprocess again."
		elif [ $ans4 == 2 ]; then
			downloadfileo installd.$file
		elif [ $ans4 == 3 ]; then
			cd /var/mobile
			downloadfileo installd.$file
			cd /usr/libexec
		fi
		echo	
		checkd
	elif [ $ans2 == 2 ]; then
		downloadp
	fi
}

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 775 $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/$1
	chmod 775 $1
	echo -e "Patched installd file downloaded to\n$(pwd)/$1."
}

function downloadp {
	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
}

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 "You know what you're doing, good for you.\nPlease note that this script may not work\nas it should until you fix this anomaly.\n"
			exit
		fi
		current
	fi
}

function current {
	if [ -f installd.backup ]; then
		echo Patched installd currently in place.
	elif [ -f installd.patched ]; then
		echo Original installd currently in place.
	fi
	echo  
}

function help {
	echo -e "To use, please type '$scriptname' followed by:
\t-o To use original installd
\t-p To use patched  installd
\t-u To update $scriptname
\t-d To download installd files\n"
}

echo  
if [ "$(id -u)" != "0" ]; then
	echo -e "This script must be run as root.\nTo login, type 'su', then key in your root\npassword at the prompt.\n"
	echo -e "Or type 'sudo $scriptname' to run the\nscript (works only if you've edited your\nsudoers file).\n"
	exit
fi

if [ ! -e /usr/bin/wget ]; then
	echo Installing necessary tools.
	if [ ! -e /usr/bin/apt-get ]; then
	     echo -e "Please install APT 0.7 Strict from Cydia.\nUnable to install necessary tools,\nexiting Cyfix.\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 You do not have a patched installd.
	echo -e "(If that is a wrong assessment, please\ncontact Secretss[C4iD] on IRC on\nthe irc.rizon.net server.)"
	echo -e "Would 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

case "$1" in "")
help
;;

-o)
if [ -f installd.patched ]; then
	echo Original installd currently in place.
	echo -e "No changes has been made.\n"
else
	mv -f installd installd.patched
	mv -f installd.backup installd
	echo Installd successfully switched to original.
	echo -e "\nTo make in-app purchases in legit apps that\nwere installed with patched installd,\nplease reinstall them now.\n"
fi
;;

-p)
if [ -f installd.backup ]; then
	echo Patched installd currently in place.
	echo -e "No 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. If an update is\navailable, download will start immediately.\nPlease do not interrupt the process.\n"
wget -N -q http://dl.dropbox.com/u/1838452/$scriptname
chmod 775 $scriptname
if [[ $before != $(md5sum switchrd) ]]; then
	echo -e "$scriptname has been successfully updated.\n"
	exit
else
	echo -e "No updates available.\n"
fi
;;
  
-d)
download
;;

*)
echo v3.4.5b Written by Secretss[C4iD]
echo Contact Secretss[C4iD] on Rizon IRC server
echo or @secretss777 on Twitter.
echo  
help
;;

esac
exit