#! /bin/sh -e

THEMEDIR='/usr/share/themes/blackplastictheme-themedir'

if [ ! -d $THEMEDIR ]; then
  exit 0
fi


 # let's check if black plastic is current active theme and set variable RST=1 for later use
 if [ "`readlink /etc/hildon/theme`" = /usr/share/themes/blackplastictheme-themedir ]; then
   if [ -x /usr/bin/personalisation ]; then
	RST="1"
   fi
 fi

if [ "$1" = "Use CUSTOM transitions" ]; then

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Enabling custom transitions"


    	cp -rf /usr/share/themes/blackplastictheme-themedir/transitions.tmp /usr/share/themes/blackplastictheme-themedir/transitions.ini


elif [ "$1" = "Use DEFAULT transitions" ]; then

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Disabling custom transitions"
	
	if [ -f /usr/share/themes/blackplastictheme-themedir/transitions.ini ]; then

		rm -rf /usr/share/themes/blackplastictheme-themedir/transitions.ini

	fi

fi

sleep 4

TEEMA="$3"

	if [ "$RST" = "1" ]; then
		# resetting theme to default (nseries one)
		/usr/bin/personalisation /usr/share/themes/default
	fi

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up $TEEMA scheme."

	rm -rf /usr/share/themes/blackplastictheme-themedir/gtk-2.0/*.cache

	cp -rf /usr/share/themes/blackplastictheme-themedir/variations/defaults/* /usr/share/themes/blackplastictheme-themedir/

	cp -rf /usr/share/themes/blackplastictheme-themedir/variations/$TEEMA/* /usr/share/themes/blackplastictheme-themedir/
	
	RS='1'

FONT="$2"

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up $FONT, this can take up to one minute. Please be patient :)"
	perl -pi -e "s/Droid Sans/$FONT/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc.cache /usr/share/themes/blackplastictheme-themedir/matchbo*/theme.xml /usr/share/themes/blackplastictheme-themedir/css/colors.css

	sleep 2


hildon-theme-cacher $THEMEDIR

if [ "$4" = "Activate Plastic Theme" ]; then

	RST="1"
fi

	if [ "$RST" = "1" ]; then

	# setting Black Plastic as theme to use
	/usr/bin/personalisation /usr/share/themes/blackplastictheme-themedir

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Activating Black Plastic Theme"

	# let's copy the background stuff into it's place
	cp -rf /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper1.png /home/user/.backgrounds/background-1.png
	cp -rf /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper2.png /home/user/.backgrounds/background-2.png
	cp -rf /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper3.png /home/user/.backgrounds/background-3.png
	cp -rf /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper4.png /home/user/.backgrounds/background-4.png	
	chown user /home/user/.backgrounds/background-1.png
	chown user /home/user/.backgrounds/background-2.png
	chown user /home/user/.backgrounds/background-3.png
	chown user /home/user/.backgrounds/background-4.png


		if [ "$5" = "Reboot" ]; then
		
		sleep 2

		reboot

		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Rebooting in 5 seconds, fasten your seatbelts!"

		exit 0

		else

		sleep 2

		run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Theme setup done! Remember to restart the phone to apply all changes!' uint32:0 string:'NAO OK!'

		exit 0

		fi

	fi

run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Black Plastic settings saved! You can now and manually select Black Plastic Theme to use, remember to reboot afterwards to refresh all theme elements.' uint32:0 string:'NAO OK!'

exit 0
