# !/bin/bash
# Grab iTunes artwork from apps
# v1.0.4 - 2nd July 2010 - by Secretss

#########################################################################################00#
function appstore {
echo "Grabbing iTunesArtwork from AppStore apps..."
for dir in $(ls /User/Applications); do
	cd /User/Applications/"$dir"
	app=$(grep -L '' *.app)
	cd /User/Applications/"$dir"/"$app"
	if [ -f info.plist ]; then
		mv info.plist Info.plist
	fi
	plutil -convert xml1 Info.plist > /dev/null 2>&1
	filename=$(plutil -key CFBundleIdentifier Info.plist 2>&1)
	cd ..
	cp iTunesArtwork "$filename".png
	mv "$filename".png /var/mobile/ArtGrab/tmp/"$filename".png
done
echo Done.
}

function native {
echo "Grabbing Icons from Native and Cydia apps..."
chown -R root:wheel /Applications
for dir in $(ls /Applications); do
	cd /Applications/"$dir"
	chown -R root:wheel .
	if [ -f info.plist ]; then
		mv info.plist Info.plist
	fi
	if [ -f icon.png ]; then
		mv icon.png Icon.png
	fi
	if [ -f IconHarvester.png ]; then
		mv IconHarvester.png Icon.png
	fi
	if [ ! -f Icon.png ] || [ ! -f Info.plist ]; then
		if [ ! -f Icon.png ]; then
			echo $dir is missing Icon.png >> /var/mobile/ArtGrab/ArtGrab.log
		fi
		if [ ! -f Info.plist ]; then
			echo $dir is missing Info.plist >> /var/mobile/ArtGrab/ArtGrab.log
		fi
		continue
	fi
	plutil -convert xml1 Info.plist > /dev/null 2>&1
	filename=$(plutil -key CFBundleIdentifier Info.plist 2>&1)
	cp Icon.png "$filename".png
	mv "$filename".png /var/mobile/ArtGrab/tmp/"$filename".png
done
echo Done.
}

#########################################################################################00#
function ans1 {
	appstore
	echo "Creating archive... "
	cd /var/mobile/ArtGrab
	mv tmp AppStore
	archive=AppStore.$(date +%d-%m.%H%M).tar
	tar -cf $archive AppStore
	echo -e "Done.\n$archive created in\n$(pwd)."
}

function ans2 {
	native
	echo "Creating archive... "
	cd /var/mobile/ArtGrab
	mv tmp Native
	archive=Native.$(date +%d-%m.%H%M).tar
	tar -cf $archive Native
	echo -e "Done.\n$archive created in\n$(pwd)."
}

function help {
	echo -e "Usage: artgrab\n       -to make a selection at the menu"
	echo -e "Usage: artgrab -u\n       -to update ArtGrab"
	echo -e "Usage: artgrab -h\n       -to display this text\n"
	echo -e "v1.0.4 Written by Secretss\nContact Secretss[C4iD] on Rizon IRC server\nor @Secretss_ on Twitter.\n"
}

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

if [ ! -e /usr/bin/wget ] || [ ! -e /usr/bin/plutil ]; 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
	if [ ! -e /usr/bin/wget ]; then
	     apt-get -qq install wget
	     if [ ! -e /usr/bin/wget ]; then
		  echo -e "Unable to install wget.\nAre you connected to the Internet?"
		  exit
	     fi
	fi
	if [ ! -e /usr/bin/plutil ]; then
	     apt-get -qq install com.ericasadun.utilities
	     if [ ! -e /usr/bin/plutil ]; then
		  echo -e "Unable to install Erica Utilities.\nAre you connected to the Internet?"
		  exit
	     fi
	fi
	echo -e "Done.\n"
fi

#########################################################################################00#
case "$1" in "")
	echo -e "\nWhat icons do you require?
\t1 = AppStore app icons
\t2 = Native+Cydia app icons
\t3 = Pack both up in two zips
\t4 = Pack both up in one zip"
	read ans
	if [ $ans != 1 ] && [ $ans != 2 ] && [ $ans != 3 ] && [ $ans != 4 ]; then
		echo -e "Please input an integer from 1 to 4.\n"
		exit
	else
		if [ ! -d /var/mobile/ArtGrab ]; then
			mkdir /var/mobile/ArtGrab
		fi
		mkdir /var/mobile/ArtGrab/tmp
		echo -e "=== Start $(date +%d/%m) at $(date +%H:%M) ===" >> /var/mobile/ArtGrab/ArtGrab.log
	fi
	if [ $ans == 1 ]; then
		ans1
		rm -rf /var/mobile/ArtGrab/AppStore
	elif [ $ans == 2 ]; then
		ans2
		rm -rf /var/mobile/ArtGrab/Native
	elif [ $ans == 3 ]; then
		ans1
		rm -rf /var/mobile/ArtGrab/AppStore
		mkdir /var/mobile/ArtGrab/tmp
		ans2
		rm -rf /var/mobile/ArtGrab/Native
	elif [ $ans == 4 ]; then
		appstore
		native
		echo "Creating archive... "
		cd /var/mobile/ArtGrab
		mv tmp Icons
		archive=Icons.$(date +%d-%m.%H%M).tar
		tar -cf $archive Icons
		echo -e "Done.\n$archive created in\n$(pwd)."
		rm -rf /var/mobile/ArtGrab/Icons
	fi
	echo -e "===  End $(date +%d/%m) at $(date +%H:%M)  ===\n" >> /var/mobile/ArtGrab/ArtGrab.log
	echo -e "\nProcess complete.\nCheck log at /var/mobile/ArtGrab/ArtGrab.log\nfor missing files.\n"
;;
-u)
	cd /usr/bin
	before=$(md5sum artgrab)
	echo -e "\nChecking for updates."
	wget -N -q http://dl.dropbox.com/u/1838452/artgrab
	chmod 755 artgrab
	if [ "$before" != "$(md5sum artgrab)" ]; then
		echo -e "ArtGrab has been successfully updated.\n"
		exit
	else
		echo -e "No updates available.\n"
		exit
	fi
;;
-h)
	help
;;
*)
	help
;;
esac

exit
