#!/bin/bash
#Change Clean Splatters theme Colors script by doseofnerd
#http://doseofnerd.tumblr.com
#Contact coolipodnerd@gmail.com if any problems occur.
#I realize this script isn't technically portable but it only needs to run on iPhones/iPods so no worries. It runs on an iPhone, isn't that ..."portable" enough? 
#The deb package for this script (in repository this was installed from) installs prerequisites like bash, coreutils, ncurses, pcre ...

#This section will ask for root privileges
x=$(whoami)
if [ "$x" !=  "root" ]
then
	clear
	echo -e "$(tput bold; tput setaf 7)You will need root access to continue. \nPlease type $(tput setaf 1)su root$(tput bold; tput setaf 7) and enter your \npassword then run this command again. \n(the default password is \"alpine\")"
	exit 0
fi

#This section tests if the theme is still installed.
if [ ! -d /Library/Themes/Clean\ Splatters.theme ]
then
	echo -e "The Clean Splatters Setup can not be run because\nthe theme was either renamed or removed from this\ndevice."
	exit 0
fi

#This section will detect if this is your first time running the script, remove HTML backgrounds, and uncompress the color options.
cd /Library/Themes/Clean\ Splatters.theme
if [ -f Wallpaper.html ]
then
	mv -f LockBackground.html SetupLockscreen.html
	mv -f Wallpaper.html SetupHomescreen.html
fi

if [ -f Color\ Options/Colors.zip ]
	then
	if [ ! -d Color\ Options/Light\ Blue ]
		then
			cd /Library/Themes/Clean\ Splatters.theme/Color\ Options
			echo -e "Setting up color options for first time...\nPlease do not exit terminal."
			unzip -qq Colors
			rm Colors.zip
		fi
	fi

#This section will determine the aspect ratio of the device so that the right cpbitmap will be placed in the SpringBoard wallpapers
fullsix=false
cp -r /System/Library/CoreServices/SpringBoard.app/simulator.plist /var/mobile
cd /var/mobile
plutil -convert xml1 simulator.plist
pcregrep -M "full-6</key>\n\t\t<true/>" simulator.plist && fullsix=true
if [ "$fullsix" == "true" ]
then
	echo -e "640 x 1136 resolution detected. \nAppropiate action will be taken."
	#Line above will only appear for a glimpse then clear
fi

#The script introduces itself.
sleep 0.5s
clear
echo -e -n "$(tput bold; tput setaf 7)Change $(tput setaf 6)Clean Splatters $(tput bold; tput setaf 7)colors:\n\t(1) Light Blue\n\t(2) Coral Sunset\n\t(3) Ink Black\n\t(4) Gradual Gray\n\t(5) Galaxy Glow\n\t(6) Reset theme\nChoose the number of the color you want\nand press \"Return\": "
read choice
echo -e -n "You chose$(tput setaf 2) $choice$(tput bold; tput setaf 7). \n"
counter=5
#this section will copy new color image files
cont=1
while [ "$cont" == "1" ]
do
	echo -n "Setting up color scheme... "
	if [ "$choice" == "1" ]
	then
		cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Light\ Blue/* /Library/Themes/Clean\ Splatters.theme
		(( cont=0 ))
		if [ "$fullsix" == "true" ]
		then
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Light\ Blue/cpbitmap/full/* /var/mobile/Library/SpringBoard
		else
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Light\ Blue/cpbitmap/reg/* /var/mobile/Library/SpringBoard
		fi
	elif [ "$choice" == "2" ]
	then
		cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Coral\ Sunset/* /Library/Themes/Clean\ Splatters.theme
		(( cont=0 ))
		if [ "$fullsix" == "true" ]
		then
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Coral\ Sunset/cpbitmap/full/* /var/mobile/Library/SpringBoard
		else
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Coral\ Sunset/cpbitmap/reg/* /var/mobile/Library/SpringBoard
		fi
	elif [ "$choice" == "3" ]
	then
		cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Ink\ Black/* /Library/Themes/Clean\ Splatters.theme
		(( cont=0 ))
		if [ "$fullsix" == "true" ]
		then
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Ink\ Black/cpbitmap/full/* /var/mobile/Library/SpringBoard
		else
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Ink\ Black/cpbitmap/reg/* /var/mobile/Library/SpringBoard
		fi
	elif [ "$choice" == "4" ]
	then
		cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/* /Library/Themes/Clean\ Splatters.theme
		(( cont=0 ))
		if [ "$fullsix" == "true" ]
		then
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/cpbitmap/full/* /var/mobile/Library/SpringBoard
		else
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/cpbitmap/reg/* /var/mobile/Library/SpringBoard
		fi
	elif [ "$choice" == "5" ]
	then
		cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Galaxy\ Glow/* /Library/Themes/Clean\ Splatters.theme
		(( cont=0 ))
		if [ "$fullsix" == "true" ]
		then
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Galaxy\ Glow/cpbitmap/full/* /var/mobile/Library/SpringBoard
		else
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Galaxy\ Glow/cpbitmap/reg/* /var/mobile/Library/SpringBoard
		fi
	elif [ "$choice" == "6" ]
	then
		if [ -f /Library/Themes/Clean\ Splatters.theme/SetupLockscreen.html ]
		then
			cd /Library/Themes/Clean\ Splatters.theme
			mv -f SetupLockscreen.html LockBackground.html
			mv -f SetupHomescreen.html Wallpaper.html
			cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/* /Library/Themes/Clean\ Splatters.theme
			if [ "$fullsix" == "true" ]
			then
				cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/cpbitmap/full/* /var/mobile/Library/SpringBoard
			else
				cp -r /Library/Themes/Clean\ Splatters.theme/Color\ Options/Gradual\ Gray/cpbitmap/reg/* /var/mobile/Library/SpringBoard
			fi
			(( cont=0 ))
		else
			echo -e -n "The theme is already at default settings.\nPlease choose a different number: "
			read choice
		fi
	else
		echo -e "failed. \n$choice is invalid. Type numbers 1-6."
		echo -e -n "Please Choose a different number: "
		read choice
	fi
done
#Clean up extra cpbitmap folder made during setup
if [ -d /Library/Themes/Clean\ Splatters.theme/cpbitmap ]
then
	rm -r /Library/Themes/Clean\ Splatters.theme/cpbitmap
fi
#This section tells the user that the device will respring in 5 seconds.
echo -e -n "\nYour device will respring to apply changes in 5"
counter=5
for i in $(seq 1 5)
do
	sleep 1s
	(( counter-- ))
	echo -e -n "\b$counter"
done
killall SpringBoard
#no, really, that's it!