#! /bin/sh -e

THEMEDIR='/etc/hildon/theme'

FONT="$1"

	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/fontchanger/css-template /opt/fontchanger/css-template-modded
	cp -rf /opt/fontchanger/gtk-template /opt/fontchanger/gtk-template-modded

	perl -pi -e "s/Droid Sans/$FONT/g" /opt/fontchanger/gtk-template-modded /opt/fontchanger/matchbo*/theme.xml /opt/fontchanger/css-template-modded

	sleep 2

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

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


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


	fi
	
	BACKUPDIR="`readlink /etc/hildon/theme`"
	if [ ! -d /opt/fontchanger/backups/$BACKUPDIR ]; then
	mkdir -p /opt/fontchanger/backups/$BACKUPDIR
	fi

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Creating backup of $BACKUPDIR"

	if [ -d /opt/fontchanger/backups/$BACKUPDIR/css ]; then
	cp -rf /opt/fontchanger/backups/$BACKUPDIR/css /etc/hildon/theme/
	else
	cp -rf /etc/hildon/theme/css /opt/fontchanger/backups/$BACKUPDIR/
	fi
	
	if [ -d /opt/fontchanger/backups/$BACKUPDIR/gtk-2.0 ]; then
	cp -rf /opt/fontchanger/backups/$BACKUPDIR/gtk-2.0 /etc/hildon/theme/
	else
	cp -rf /etc/hildon/theme/gtk-2.0 /opt/fontchanger/backups/$BACKUPDIR/
	fi
	
	if [ -d /opt/fontchanger/backups/$BACKUPDIR/matchbox ]; then
	cp -rf /opt/fontchanger/matchbox /etc/hildon/theme/
	else
	cp -rf /etc/hildon/theme/matchbox /opt/fontchanger/backups/$BACKUPDIR/
	cp -rf /opt/fontchanger/matchbox /etc/hildon/theme/
	fi
	
	if [ -d /opt/fontchanger/backups/$BACKUPDIR/matchbox2 ]; then
	cp -rf /opt/fontchanger/matchbox2 /etc/hildon/theme/
	else
	cp -rf /etc/hildon/theme/matchbox2 /opt/fontchanger/backups/$BACKUPDIR/
	cp -rf /opt/fontchanger/matchbox2 /etc/hildon/theme/
	fi	

	cat /opt/fontchanger/css-template-modded >> /etc/hildon/theme/css/colors.css
	cat /opt/fontchanger/gtk-template-modded >> /etc/hildon/theme/gtk-2.0/gtkrc

	rm /opt/fontchanger/css-template-modded
	rm /opt/fontchanger/gtk-template-modded


hildon-theme-cacher $THEMEDIR


		if [ "$3" = "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!'
                sleep 2
		killall hildon-status-menu
		dsmetool -k /usr/bin/hildon-home
		sleep 3
		dsmetool -t /usr/bin/hildon-home
		sleep 4
		dsmetool -k /usr/bin/hildon-desktop
		sleep 2
		dsmetool -t /usr/bin/hildon-desktop
		sleep 4
		run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:'Font setup done! Reboot is recommended to reload new font values to all applications.' uint32:0 string:'NAO OK!'
		killall fontchanger
		exit 0

		fi

	fi

exit 0
