#! /bin/sh -e

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

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

if [ ! "$8" ]; 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

	sleep 2

	SNAP="$8"

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up snap-to-grid value to $SNAP"
	perl -pi -e "s/snap_grid_size=4/snap_grid_size=$SNAP/g" /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/defaults/* /usr/share/themes/blackplastictheme-themedir/

	cp -rf /home/opt/plastic-schemes/$TEEMA/* /usr/share/themes/blackplastictheme-themedir/
	
	sleep 2

	if [ "$7" = "Don't show icon backgrounds" ]; then

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up icon backgrounds."	
	rm -rf /usr/share/themes/blackplastictheme-themedir/images/ApplicationShortcutApplet.png
	
	sleep 2

	fi

	if [ "$10" = "Use DEFAULT icons" ]; then

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up icons."	
	perl -pi -e "s/black-plastic-pleego-icons/hicolor/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	perl -pi -e "s/black-plastic-icons/hicolor/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	sleep 2

	fi
	
	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: $FONT"
	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

	if [ ! "$9" = "DEFAULT" ]; then
	MUUTTUJA="$9"

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up font sizes to $MUUTTUJA"
	let KORVAA1=13$MUUTTUJA
	perl -pi -e "s/13"/$KORVAA1"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	let KORVAA2=15$MUUTTUJA
	perl -pi -e "s/15"/$KORVAA2"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	let KORVAA3=18$MUUTTUJA
	perl -pi -e "s/18"/$KORVAA3"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	let KORVAA4=20$MUUTTUJA
	perl -pi -e "s/20"/$KORVAA4"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	let KORVAA5=24$MUUTTUJA
	perl -pi -e "s/24"/$KORVAA5"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
	let KORVAA6=26$MUUTTUJA
	perl -pi -e "s/26"/$KORVAA6"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc

	fi

#	if [ "$9" = "Smaller" ]; then

#	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up font sizes to Smaller"
#	perl -pi -e "s/13"/12"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
#	perl -pi -e "s/15"/13"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
#	perl -pi -e "s/18"/16"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
#	perl -pi -e "s/20"/18"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
#	perl -pi -e "s/24"/22"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc
#	perl -pi -e "s/26"/24"/g" /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc

#	perl -pi -e "s/13px/12px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css
#	perl -pi -e "s/15px/13px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css
	perl -pi -e "s/18px/16px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css
	perl -pi -e "s/20px/18px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css
	perl -pi -e "s/24px/22px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css
	perl -pi -e "s/26px/24px/g" /usr/share/themes/blackplastictheme-themedir/css/colors.css

#	fi


hildon-theme-cacher $THEMEDIR

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

	RST="1"
fi

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


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

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

		if [ "$6" = "Use Plastic Wallpapers" ]; then

		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up wallpapers"
		gconftool -s --type string /apps/osso/hildon-desktop/views/1/bg-image /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper1.png
		gconftool -s --type string /apps/osso/hildon-desktop/views/2/bg-image /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper2.png
		gconftool -s --type string /apps/osso/hildon-desktop/views/3/bg-image /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper3.png
		gconftool -s --type string /apps/osso/hildon-desktop/views/4/bg-image /usr/share/themes/blackplastictheme-themedir/backgrounds/wallpaper4.png

		fi

		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
