# !/bin/bash
# App statistics
# v1.6 - 22nd August 2010 - by Secretss

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

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

#########################################################################################uh#
if [ "$1" = "-h" ] || [ $# -gt 1 ] || [ $# -eq 0 ]; then
	echo -e "Usage: appstats -s\n       -to list stats for all apps"
	echo -e "Usage: appstats -l\n       -to list all apps by appleID"
	echo -e "Usage: appstats \"app name\"\n       -to find the given app's appleID"
	echo -e "Usage: appstats -u\n       -to update AppStats"
	echo -e "Usage: appstats -h\n       -to display this text"
	echo -e "v1.6 Written by Secretss\nContact Secretss[C4iD] on Rizon IRC server\nor @Secretss_ on Twitter.\n"
	exit
fi

if [ "$1" = "-u" ]; then
	cd /usr/bin
	before=$(md5sum appstats)
	echo -e "Checking for updates."
	wget -N -q http://dl.dropbox.com/u/1838452/appstats
	chmod 755 appstats
	if [ "$before" != "$(md5sum appstats)" ]; then
		echo -e "AppStats has been successfully updated.\n"
		exit
	else
		echo -e "No updates available.\n"
		exit
	fi
	exit
fi

######################################################################################stats#
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"
		else
			Encrypted="Decrypted"
		fi


		if [ -f iTunesMetadata.plist ]; then
			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
		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

#######################################################################################list#
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
		if [ "$id" = "log" ]; then
			continue
		else
			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"
		fi
	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

########################################################################################app#
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 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)
						appleIDsearch
						exit
					fi
				fi
			done
			echo Could not find app.
		else
			echo User terminated.
		fi
	else
	     appleIDsearch
	fi
fi

exit