# !/bin/bash
# Various app tools
# v2.1.1 beta - 28th August 2010 - by Secretss
#
# Thanks go to pirogoeth and sadimusi and all beta testers

########################################################################################the#
if [ "$(id -u)" != "0" ]; then
	echo -e "\nThis script must be run in root.\n"
	exit
fi

if [ ! -e /usr/bin/plutil ] || [ ! -e /bin/sed ] || [ ! -e /usr/bin/awk ] || [ ! -e /bin/grep ]; then
	echo -n "Installing necessary tools... "
	if [ ! -e /usr/bin/apt-get ]; then
	     echo -e "\nUnable to automatically install tools,\nplease install APT 0.7 Strict from Cydia.\n"
	     exit 1
	fi
	if [ ! -e /usr/bin/plutil ]; then
	     apt-get -qq install com.ericasadun.utilities > /dev/null 2>&1
	     if [ ! -e /usr/bin/plutil ]; then
		  echo -e "\nUnable to install tools.\nAre you connected to the Internet?"
		  exit 1
	     fi
	fi
	if [ ! -e /bin/sed ]; then
	     apt-get -qq install sed > /dev/null 2>&1
	     if [ ! -e /bin/sed ]; then
		  echo -e "\nUnable to install tools.\nAre you connected to the Internet?"
		  exit 1
	     fi
	fi
	if [ ! -e /usr/bin/awk ]; then
	     apt-get -qq install gawk > /dev/null 2>&1
	     if [ ! -e /usr/bin/awk ]; then
		  echo -e "\nUnable to install tools.\nAre you connected to the Internet?"
		  exit 1
	     fi
	fi
	if [ ! -e /bin/grep ]; then
	     apt-get -qq install grep > /dev/null 2>&1
	     if [ ! -e /bin/grep ]; then
		  echo -e "\nUnable to install tools.\nAre you connected to the Internet?"
		  exit 1
	     fi
	fi
	apt-get -qq install coreutils > /dev/null 2>&1
	apt-get -qq install com.bigboss.20toolchain > /dev/null 2>&1
	echo -e "Done.\n"
fi

temp="/var/mobile/AppTool"
if [ -e "$temp" ]; then
	rm -rf "$temp"
fi
mkdir "$temp"

IMPT="/var/mobile/Library/AppTool-donotdelete"
if [ ! -d "$IMPT" ]; then
	mkdir "$IMPT"
fi

appdbackup="/var/mobile/Library/AppTool-appdbackup"
if [ ! -d "$appdbackup" ]; then
	mkdir "$appdbackup"
	chown -R mobile:mobile "$appdbackup"
	chmod -R 755 "$appdbackup"
fi

installplist="/var/mobile/Library/Caches/com.apple.mobile.installation.plist"
installplist1="/var/mobile/Library/Caches/com.apple.mobile.installation.plist1"

#####################################################################################psycho#
if [ "$1" = "-h" ] || [ $# -eq 0 ] || [ $# -gt 2 ]; then
	echo -e "Usage: apptool -s\n\t-to list stats for all apps"
	echo -e "Usage: apptool -l\n\t-to list all apps by appleID"
	echo -e "Usage: apptool -c\n\t-to list only cracked apps"
	echo -e "Usage: apptool -e\n\t-to list only encrypted apps"
	echo -e "Usage: apptool \"app name\"\n\t-to find the given app's appleID"
	echo -e "Usage: apptool -rm \"apple@id.here\"\n\t-to remove iTunesMetadata"
	echo -e "Usage: apptool -rs \"apple@id.here\"\n\t-to restore iTunesMetadata"
	echo -e "Usage: apptool -cu\n\t-to list updated cracked apps"
	echo -e "Usage: apptool -uc\n\t-to list uncracked apps"
	echo -e "Usage: apptool -cd\n\t-to change application directory"
	echo -e "Usage: apptool -ad\n\t-to backup or restore app data"
	echo -e "Usage: apptool -un \"app name\"\n\t-to uninstall given app"
	echo -e "Usage: apptool -u\n\t-to update AppTool"
	echo -e "Usage: apptool -h\n\t-to display this text\n"
	echo -e "v2.1.1 beta Written by Secretss\nContact Secretss[C4iD] on Rizon IRC server\nor @Secretss_ on Twitter.\n"
	exit
fi

if [ "$1" = "-u" ]; then
	if [ ! -e /usr/bin/wget ]; then
		echo "Installing wget first... "
		if [ ! -e /usr/bin/apt-get ]; then
			echo -e "\nUnable to automatically install wget,\nplease install it manually from Cydia.\n"
			exit 1
		fi
		apt-get -qq install wget > /dev/null 2>&1
		if [ ! -e /usr/bin/wget ]; then
			echo -e "\nUnable to install wget.\nAre you connected to the Internet?"
			exit 1
		fi
		echo "Done."
	fi
	cd /usr/bin
	before=$(md5sum apptool)
	echo -n "Checking for updates... "
	wget -N -q http://dl.dropbox.com/u/1838452/apptool
	chmod 755 apptool
	if [ "$before" != "$(md5sum apptool)" ]; then
		echo "Updated."
	else
		echo -e "\nNo updates available."
	fi
	exit
fi

#######################################################################################baby#
if [ "$1" = "-s" ]; then
	echo -n "Please wait... "
	num=0
	for dir in $(ls -d /User/Applications/*); do
		let num+=1

		AppDir=$(echo $dir | sed 's:/User/Applications/::g')
		cd $dir
		ShortName=$(grep -L '' *.app | sed 's:.app::g')
		cd "$(grep -L '' *.app)"

		LongName=$(plutil -key CFBundleDisplayName Info.plist 2>&1)
		AppVersion=$(plutil -key CFBundleVersion Info.plist 2>&1)
		AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
		CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')
		cd ..

		if [ "$CryptID" = "1" ]; then
			Encrypted="Encrypted"
			appleID=$(plutil -key appleId iTunesMetadata.plist 2>&1)
			Date=$(plutil -key purchaseDate iTunesMetadata.plist 2>&1)
			LongName=$(plutil -key itemName iTunesMetadata.plist 2>&1)

			if [ "${appleID:0:6}" = "Error:" ]; then 
				plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
				appleID=$(echo $(grep -A 1 AppleID iTunesMetadata.plist | tr -d '\n') | sed -e "s:.*<string>::" -e "s:<\/string>::")
				Date=$(echo $(grep -A 1 purchaseDate iTunesMetadata.plist | tr -d '\n') | sed -e "s:.*<string>::" -e "s:<\/string>::")
				LongName=$(echo $(grep -A 1 itemName iTunesMetadata.plist | tr -d '\n') | sed -e "s:.*<string>::" -e "s:<\/string>::")
			fi

		else
			Encrypted="Decrypted"
		fi

		echo $LongName >> "$temp/stats"
		echo $ShortName >> "$temp/stats"
		echo $Encrypted >> "$temp/stats"
		echo $AppDir >> "$temp/stats"
		if [ -f iTunesMetadata.plist ]; then
			echo $appleID >> "$temp/stats"
			echo ${Date:0:10} >> "$temp/stats"
		fi
		echo $AppVersion >> "$temp/stats"
		echo   >> "$temp/stats"
	done
	echo $num apps found. >> "$temp/stats"

	echo -e "Done\nData stored in $temp/stats."
	read -p "Display on screen? (Y/N):" answer
	if [ "$answer" = y ] || [ "$answer" = Y ]; then
		echo -e "$(cat "$temp/stats")"
	fi
	exit
fi

#######################################################################################doll#
if [ "$1" = "-l" ]; then
	echo -n "Please wait... "
	for app in $(ls -d /User/Applications/*); do
		cd "$app"
		cd "$(grep -L '' *.app)"
		AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
		CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')
		cd ..

		if [ "$CryptID" = "1" ]; then
			appleID=$(plutil -key appleId iTunesMetadata.plist 2>&1)
			if [ "${appleID:0:6}" = "Error:" ]; then 
				plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
				appleID=$(echo $(grep -A 1 AppleID iTunesMetadata.plist | tr -d '\n') | sed -e "s/.*<string>//" -e "s/<\/string>//")
			fi
		else
			appleID="Cracked"
		fi
		echo $(echo $(grep -L '' *.app) | sed 's:.app::g') >> "$temp/$appleID"
     done

     total=0
     for id in $(ls "$temp/"); do
		sort -f "$temp/$id" -o "$temp/$id"
		echo $id >> "$temp/list"
		num=0
		while read line; do
			let num+=1
			echo -e "$num $(echo -n $line)" >> "$temp/list"
		done < "$temp/$id"
		let total+=$num
		echo   >> "$temp/list"
	done

	echo Total number of apps: $total. >> "$temp/list"
	echo -e "Done\nData stored in $temp/list."
	read -p "Display on screen? (Y/N):" answer
	if [ "$answer" = y ] || [ "$answer" = Y ]; then
		echo -e "$(cat "$temp/list")"
	fi
	exit
fi

######################################################################################loves#
if [ "$1" = "-c" ]; then
	echo -n "Please wait... "
	num=0
	for dir in $(ls -d /User/Applications/*); do
		cd $dir
		AppName=$(grep -L '' *.app | sed 's:.app::g')
		cd "$(grep -L '' *.app)"
		AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
		CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')

		if [ "$CryptID" = "1" ]; then
			continue
		else
			let num+=1
			echo $AppName >> "$temp/unencrypted"
		fi
	done

	echo -e "\n$num cracked apps found." >> "$temp/unencrypted"
	echo -e "Done.\nStored in $temp/unencrypted."
	read -p "Display on screen? (Y/N):" answer
	if [ "$answer" = y ] || [ "$answer" = Y ]; then
		echo -e "$(cat "$temp/unencrypted")"
	fi
	exit
fi

########################################################################################you#
if [ "$1" = "-e" ]; then
	echo -n "Please wait... "
	num=0
	for dir in $(ls -d /User/Applications/*); do
		cd $dir
		AppName=$(grep -L '' *.app | sed 's:.app::g')
		cd "$(grep -L '' *.app)"
		AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
		CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')

		if [ "$CryptID" = "1" ]; then
			let num+=1
			echo $AppName >> "$temp/encrypted"
		else
			continue
		fi
	done

	echo -e "\n$num encrypted apps found." >> "$temp/encrypted"
	echo -e "Done\nStored in $temp/encrypted."
	read -p "Display on screen? (Y/N):" answer
	if [ "$answer" = y ] || [ "$answer" = Y ]; then
		echo -e "$(cat "$temp/encrypted")"
	fi
	exit
fi

#######################################################################################very#
if [ "$1" = "-rm" ]; then
	echo -n "Please wait... "
	for dir in $(ls -d /User/Applications/*); do
		cd $dir
		if [ -e iTunesMetadata.plist ]; then
			itemId=$(plutil -key itemId iTunesMetadata.plist 2>&1)
			appleID=$(plutil -key appleId iTunesMetadata.plist 2>&1)
			if [ "${appleID:0:6}" = "Error:" ]; then 
				plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
				appleID=$(echo $(grep -A 1 AppleID iTunesMetadata.plist | tr -d '\n') | sed -e "s/.*<string>//" -e "s/<\/string>//")
			fi
			if [ "$appleID" = "$2" ]; then
				echo   > apptool-$itemId
				if [ ! -d "$IMPT/$2" ]; then
					mkdir "$IMPT/$2"
				fi
				if [ ! -d "$IMPT/$2/$itemId" ]; then
					mkdir "$IMPT/$2/$itemId"
				fi
				mv -f iTunesMetadata.plist "$IMPT/$2/$itemId/iTunesMetadata.plist"
			fi
		fi
	done

	echo -e "Done."
	exit
fi

#######################################################################################much#
if [ "$1" = "-rs" ]; then
	echo -n "Please wait... "
	if [ ! -d "$IMPT/$2" ] || [ "$(ls "$IMPT/$2")" = "" ]; then
		echo There is nothing to restore.
		exit
	fi
	for itemId in $(ls "$IMPT/$2"); do
		for dir in $(ls -d /User/Applications/*); do
			cd $dir
			if [ -f "apptool-$itemId" ]; then
				mv -f "$IMPT/$2/$itemId/iTunesMetadata.plist" iTunesMetadata.plist
				rm -f "apptool-$itemId"
				continue
			fi
		done
	done

	echo -e "Done."
	exit
fi

#########################################################################################do#
if [ "$1" = "-cd" ]; then
	if [ ! -d /var/mobile/Applications ]; then
		echo "Applications folder does not exist."
		read -p "Restore or create new? (R=restore/C=create):" answer
		if [ "$answer" = "R" ] || [ "$answer" = "r" ]; then
			while [ ! "$name2" ] || [ ! -d /var/mobile/Applications-$name2 ]; do
				echo "Enter directory callname to restore:"
				ls /var/mobile > $temp/appfolder
				folders=$(grep 'Applications' "$temp/appfolder" | sed 's:Applications-::g')
				for word in $folders; do
					echo $word
				done
				echo  
				read name2
				if [ ! "$name2" ] || [ ! -d /var/mobile/Applications-$name2 ]; then
					echo Invalid input.
				fi
			done
			mv -f "/var/mobile/Applications-$name2" /var/mobile/Applications
			mv -f "/var/mobile/Library/Caches/com.apple.mobile.installation.plist-$name2" "/var/mobile/Library/Caches/com.apple.mobile.installation.plist"
			echo "Done. Please respring."
		elif [ "$answer" = "C" ] || [ "$answer" = "c" ]; then
			mkdir /var/mobile/Applications
			chown -R mobile:mobile /var/mobile/Applications
			echo "Done. Please respring."
		fi
		exit
	fi

	read -p "Please set a callname for current directory:" name
	if [ ! "$name" ]; then
		echo No input detected.
		exit
	else
		name=$(echo $name | tr -d ' ')
		if [ -e "/var/mobile/Applications-$name" ]; then
			echo $name already exists.
			exit
		fi
		mv -f /var/mobile/Applications "/var/mobile/Applications-$name"
		mv -f /var/mobile/Library/Caches/com.apple.mobile.installation.plist "/var/mobile/Library/Caches/com.apple.mobile.installation.plist-$name"
	fi
	while [ "$answer" != "R" ] && [ "$answer" != "r" ] && [ "$answer" != "C" ] && [ "$answer" != "c" ]; do
		read -p "Restore or create new? (R=restore/C=create):" answer
		if [ "$answer" = "R" ] || [ "$answer" = "r" ]; then
			while [ ! "$name2" ] || [ ! -d /var/mobile/Applications-$name2 ]; do
				echo "Enter directory callname to restore:"
				ls /var/mobile > $temp/appfolder
				folders=$(grep 'Applications' "$temp/appfolder" | sed 's:Applications-::g')
				for word in $folders; do
					echo $word
				done
				echo  
				read name2
				if [ ! "$name2" ] || [ ! -d /var/mobile/Applications-$name2 ]; then
					echo Invalid input.
				fi
			done
			mv -f "/var/mobile/Applications-$name2" /var/mobile/Applications
			mv -f "/var/mobile/Library/Caches/com.apple.mobile.installation.plist-$name2" "/var/mobile/Library/Caches/com.apple.mobile.installation.plist"
			echo "Done. Please respring."
		elif [ "$answer" = "C" ] || [ "$answer" = "c" ]; then
			mkdir /var/mobile/Applications
			chown -R mobile:mobile /var/mobile/Applications
			echo "Done. Please respring."
		fi
	done
	exit
fi

########################################################################################you#
if [ "$1" = "-cu" ]; then
	if [ ! -e "$IMPT/cracklist" ] || [ "$(cat "$IMPT/cracklist")" = "" ]; then
		echo "You don't seem to have cracked anything yet!"
		exit
	fi
	while read line; do
		for app in $(ls -d /User/Applications/*); do
			cd "$app"
			if [ -e iTunesMetadata.plist ]; then
				itemId=$(plutil -key itemId iTunesMetadata.plist 2>&1)
				if [ "${itemId:0:6}" = "Error:" ]; then 
					plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
					itemId=$(echo $(grep -A 1 itemId iTunesMetadata.plist | tr -d '\n') | sed -e "s/.*<string>//" -e "s/<\/string>//")
				fi
				if [ "${line:0:9}" = "$itemId" ]; then
					cd "$(grep -L '' *.app)"
					if [ -f info.plist ]; then
						mv info.plist Info.plist
					fi
					AppVer=$(plutil -key CFBundleVersion Info.plist 2>&1)
					cd ..
					if [ "$AppVer" != "${line:12}" ]; then
						echo "$(grep -L '' *.app | sed 's:.app::g') - $AppVer"
					fi
					continue
				fi
			fi
		done
	done < "$IMPT/cracklist"
	exit
fi

#######################################################################################love#
if [ "$1" = "-uc" ]; then
	if [ ! -e "$IMPT/cracklist" ] || [ "$(cat "$IMPT/cracklist")" = "" ]; then
		echo "You don't seem to have cracked anything yet!"
		exit
	fi
	for app in $(ls -d /User/Applications/*); do
		cd "$app"
		cd "$(grep -L '' *.app)"
		AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
		CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')
		cd ..

		if [ "$CryptID" = "1" ]; then
			itemId=$(plutil -key itemId iTunesMetadata.plist 2>&1)
			if [ "${itemId:0:6}" = "Error:" ]; then 
				plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
				itemId=$(echo $(grep -A 1 itemId iTunesMetadata.plist | tr -d '\n') | sed -e "s/.*<string>//" -e "s/<\/string>//")
			fi
			if [ $(grep -c "$itemId" "$IMPT/cracklist") -eq 0 ]; then
				echo "$(grep -L '' *.app | sed 's:.app::g')"
			fi
		fi
	done
	exit
fi

########################################################################################her#
if [ "$1" = "-ad" ]; then
	read -p "Backup or restore? (B=backup/R=restore):" answer

	if [ "$answer" = "B" ] || [ "$answer" = "b" ]; then
		echo -n "Please wait... "
		for app in $(ls -d /User/Applications/*); do
			cd "$app"
			cd "$(grep -L '' *.app)"
			if [ -f info.plist ]; then
				mv info.plist Info.plist
			fi
			identifier=$(plutil -key CFBundleIdentifier Info.plist 2>&1)
			cd ..
			if [ -e "$appdbackup/$identifier" ]; then
				rm -rf "$appdbackup/$identifier"
			fi
			mkdir "$appdbackup/$identifier"
			ls "$app" > $temp/log1
			while read line; do
				cp -dpRf "$line" "$appdbackup/$identifier"
			done < $temp/log1
			chown -R mobile:mobile "$appdbackup/$identifier"
			chmod -R 755 "$appdbackup/$identifier"
		done
		echo -e "Done.\nPlease find the archives in\n$appdbackup."
	elif [ "$answer" = "R" ] || [ "$answer" = "r" ]; then
		echo -n "Please wait... "
		ls $appdbackup > "$temp/log2"
		if [ "$(cat "$temp/log2")" = "" ]; then
			echo There are no backups present.
			exit
		fi
		while read id; do
			for app in $(ls -d /User/Applications/*); do
				cd "$app"
				cd "$(grep -L '' *.app)"
				if [ -f info.plist ]; then
					mv info.plist Info.plist
				fi
				identifier=$(plutil -key CFBundleIdentifier Info.plist 2>&1)
				cd ..
				if [ "$identifier" = "$id" ]; then
					cd /var/mobile/Applications/
					rm -rf "$app"
					mkdir "$app"
					chown -R mobile:mobile "$app"
					ls "$appdbackup/$identifier/" > $temp/log3
					while read line; do
						cp -dpRf "$appdbackup/$identifier/$line" "$app"
					done < $temp/log3
					rm -rf "$appdbackup/$identifier"
					continue
				fi
			done
		done < "$temp/log2"
		echo -e "Done."
	fi

	exit
fi

#######################################################################################back#
function uninstallapp {
	cd ..
	appname="$(ls | grep '' *.app | sed 's:.app::g')"
	read -p "Uninstall $appname? (Y/N):" answer
	if [ "$answer" = "Y" ] || [ "$answer" = "y" ]; then
		echo -n "Please wait... "
		appdir=$(pwd)
		cd /var/mobile/Applications
		rm -rf "$appdir"
		echo Done.
		read -p "Respring now? (Y/N): " answer2
		if [ "$answer2" = "Y" ] || [ "$answer2" = "y" ]; then
			echo Restarting SpringBoard...
			killall SpringBoard
			exit
		fi
	else
		echo Uninstallation cancelled.
	fi
}

if [ "$1" = "-un" ]; then
	ls -d /User/Applications/*/*.app > "$temp/log4"
	dir=$(grep -i "$2" "$temp/log4")
	dircount=$(echo "$dir" | wc -l)
	if [ $dircount -gt 1 ]; then
		echo -e "Multiple apps found with the same name."
		exit 1
	fi

	if [ ! -d "$dir" ]; then
		echo Simple search failed.
		read -p "Execute long approach? (y/n): " answer
		if [ "$answer" = y ]; then
			echo -n "Locating application... "
			for appfolder in $(ls -d /User/Applications/*/*.app); do
				if [ -d "$appfolder" ]; then
					cd "$appfolder"
					if [ -f info.plist ]; then
						mv info.plist Info.plist
					fi
					plutil -convert xml1 Info.plist > /dev/null 2>&1
					if [ "$(echo $2 | tr A-Z a-z)" = "$(echo $(plutil -key CFBundleDisplayName Info.plist 2>&1) | tr A-Z a-z)" ]; then
						dir=$(pwd)
						echo Done.
						uninstallapp
						exit
					fi
				fi
			done
			echo Could not find app.
		else
			echo User terminated.
		fi
	else
		cd "$dir"
		uninstallapp
	fi
	exit
fi

##########################################################################################?#
function appleIDsearch {
     cd "$dir"
     AppExec=$(plutil -key CFBundleExecutable Info.plist 2>&1)
     CryptID=$(otool -l "$AppExec" | grep cryptid | awk '{print $2}')
     cd ..

     if [ "$CryptID" = "1" ]; then
		appleID=$(plutil -key appleId iTunesMetadata.plist 2>&1)
		if [ "${appleID:0:6}" = "Error:" ]; then 
			plutil -convert xml1 iTunesMetadata.plist > /dev/null 2>&1
			appleID=$(echo $(grep -A 1 AppleID iTunesMetadata.plist | tr -d '\n') | sed -e "s/.*<string>//" -e "s/<\/string>//")
		fi
		echo $appleID
     else
		echo This app is cracked.
     fi
}

if [ "$#" = "1" ]; then
	ls -d /User/Applications/*/*.app > "$temp/log"
	dir=$(grep -i "$1" "$temp/log")
	dircount=$(echo "$dir" | wc -l)
	if [ $dircount -gt 1 ]; then
		echo -e "Multiple apps found with the same name."
		exit 1
	fi

	if [ ! -d "$dir" ]; then
		echo Simple search failed.
		read -p "Execute long approach? (y/n): " answer
		if [ "$answer" = y ]; then
			echo -n "Locating application... "
			for appfolder in $(ls -d /User/Applications/*/*.app); do
				if [ -d "$appfolder" ]; then
					cd "$appfolder"
					if [ -f info.plist ]; then
						mv info.plist Info.plist
					fi
					plutil -convert xml1 Info.plist > /dev/null 2>&1
					if [ "$(echo $1 | tr A-Z a-z)" = "$(echo $(plutil -key CFBundleDisplayName Info.plist 2>&1) | tr A-Z a-z)" ]; then
						dir=$(pwd)
						echo Done.
						appleIDsearch
						exit
					fi
				fi
			done
			echo Could not find app.
		else
			echo User terminated.
		fi
	else
		appleIDsearch
	fi
fi

exit