#! /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 /home/opt/plastic-schemes/$TEEMA/backgrounds/theme_bg.desktop
	rm -rf /home/opt/plastic-schemes/$TEEMA/index.theme
	rm -rf /home/opt/plastic-schemes/$TEEMA/matchbox
	rm -rf /home/opt/plastic-schemes/$TEEMA/matchbox2
	rm -rf /home/opt/plastic-schemes/$TEEMA/transitions.ini
	rm -rf /home/opt/plastic-schemes/$TEEMA/gtk-2.0/*.cache
	perl -pi -e 's/.*pixmap_path.*/pixmap_path "\/usr\/share\/themes\/blackplastictheme-themedir\/images"/g' /home/opt/plastic-schemes/$TEEMA/gtk-2.0/gtkrc
	perl -pi -e 's/.*gtk-theme-name.*/gtk-theme-name = "Black Plastic Theme"/g' /home/opt/plastic-schemes/$TEEMA/gtk-2.0/gtkrc
	perl -pi -e "s/hicolor/black-plastic-icons/g" /home/opt/plastic-schemes/$TEEMA/gtk-2.0/gtkrc
	perl -pi -e 's/.*font_name =.*//g' /home/opt/plastic-schemes/$TEEMA/gtk-2.0/gtkrc
	perl -pi -e 's/.*font: .*//g' /home/opt/plastic-schemes/$TEEMA/css/colors.css
	

	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"

	cp -rf /opt/plastic-schemes/css-template /opt/plastic-schemes/css-template-modded
	cp -rf /opt/plastic-schemes/gtk-template /opt/plastic-schemes/gtk-template-modded

	perl -pi -e "s/Droid Sans/$FONT/g" /opt/plastic-schemes/gtk-template-modded /usr/share/themes/blackplastictheme-themedir/matchbo*/theme.xml /opt/plastic-schemes/css-template-modded

	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" /opt/plastic-schemes/gtk-template-modded
	let KORVAA2=15$MUUTTUJA
	perl -pi -e "s/15"/${KORVAA2}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA3=18$MUUTTUJA
	perl -pi -e "s/18"/${KORVAA3}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA4=20$MUUTTUJA
	perl -pi -e "s/20"/${KORVAA4}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA5=24$MUUTTUJA
	perl -pi -e "s/24"/${KORVAA5}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA6=26$MUUTTUJA
	perl -pi -e "s/26"/${KORVAA6}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA7=36$MUUTTUJA
	perl -pi -e "s/36"/${KORVAA7}"/g" /opt/plastic-schemes/gtk-template-modded
	let KORVAA8=52$MUUTTUJA
	perl -pi -e "s/52"/${KORVAA8}"/g" /opt/plastic-schemes/gtk-template-modded


	perl -pi -e "s/13px/${KORVAA1}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/15px/${KORVAA2}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/18px/${KORVAA3}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/20px/${KORVAA4}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/24px/${KORVAA5}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/26px/${KORVAA6}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/36px/${KORVAA7}px/g" /opt/plastic-schemes/css-template-modded
	perl -pi -e "s/52px/${KORVAA8}px/g" /opt/plastic-schemes/css-template-modded


	fi

	cat /opt/plastic-schemes/css-template-modded >> /usr/share/themes/blackplastictheme-themedir/css/colors.css
	cat /opt/plastic-schemes/gtk-template-modded >> /usr/share/themes/blackplastictheme-themedir/gtk-2.0/gtkrc

	rm /opt/plastic-schemes/css-template-modded
	rm /opt/plastic-schemes/gtk-template-modded


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
		sleep 2
		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
		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:'Restarting desktops, your screen will blink!'
#		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Restarting hildon-status-menu...' uint32:0 string:'NAO OK!'
                sleep 2
		killall hildon-status-menu
#		sleep 2
#		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Shutting down hildon-home...' uint32:0 string:'NAO OK!'
		dsmetool -k /usr/bin/hildon-home
		sleep 3
#		run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Waking up hildon-home...' uint32:0 string:'NAO OK!'
		dsmetool -t /usr/bin/hildon-home
		sleep 4
#		run-standalone.sh dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view
#		run-standalone.sh dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_tklock_mode_change string:"locked"
#               sleep 2
#		killall hildon-desktop
#		sleep 15
		dsmetool -k /usr/bin/hildon-desktop
		sleep 2
		dsmetool -t /usr/bin/hildon-desktop
		sleep 4
#		run-standalone.sh dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_tklock_mode_change string:"unlocked"
		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! Rebooting the device is recommended to reload all new settings.' uint32:0 string:'NAO OK!'
		killall setupblackplastic
		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 go and manually select Black Plastic Theme to use, remember to reboot afterwards to refresh all theme elements.' uint32:0 string:'NAO OK!'

exit 0
