#! /bin/sh -e

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Font size is: $1" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Font family is: $2" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Iconset is: $3" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Icon backgrounds is: $4" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Icon size is: $5" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Shortcut size is: $16" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Snap to grid is: $6" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Reboot is: $7" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Old color 1 is: $8 New color 1 is: $12" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Old color 2 is: $9 New color 2 is: $13" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Old color 3 is: $10 New color 3 is: $14" uint32:0 string:'NAO OK!'

# run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Old color 4 is: $11 New color 4 is: $15" uint32:0 string:'NAO OK!'


THEMEDIR='/etc/hildon/theme'

	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"
	sleep 2

	cp -rf /opt/theme-customizer/css-template /opt/theme-customizer/css-template-modded
	cp -rf /opt/theme-customizer/gtk-template /opt/theme-customizer/gtk-template-modded
#	cp -rf /opt/theme-customizer/colors-template /opt/theme-customizer/colors-template-modded

	ACTUALTHEMEDIR="`readlink -f /etc/hildon/theme`"
	GTKR="`readlink -f /etc/hildon/theme/gtk-2.0/gtkrc`"
	CSS="`readlink -f /etc/hildon/theme/css/colors.css`"
	MATCHBOX="`readlink -f /etc/hildon/theme/matchbox/theme.xml`"
	MATCHBOX2="`readlink -f /etc/hildon/theme/matchbox2/theme.xml`"
	COLORS="`readlink -f /etc/hildon/theme/colors.config`"

	if [ -f $THEMEDIR/transitions.ini ]; then

	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 to: $6"
	sleep 2

	perl -pi -e "s/snap_grid_size.*/snap_grid_size=$6/g" $THEMEDIR/transitions.ini
	elif [ -f /usr/share/hildon-desktop/transitions.ini ]; then

	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 to: $6"
	sleep 2

	perl -pi -e "s/snap_grid_size.*/snap_grid_size=$6/g" /usr/share/hildon-desktop/transitions.ini
	fi 

	if [ ! -f /opt/theme-customizer/backups$GTKR ]; then
	mkdir -p /opt/theme-customizer/backups$GTKR
	rm -rf /opt/theme-customizer/backups$GTKR
	fi

	if [ ! -f /opt/theme-customizer/backups$CSS ]; then
	mkdir -p /opt/theme-customizer/backups$CSS
	rm -rf /opt/theme-customizer/backups$CSS
	fi

	if [ ! -f /opt/theme-customizer/backups$MATCHBOX ]; then
	mkdir -p /opt/theme-customizer/backups$MATCHBOX
	rm -rf /opt/theme-customizer/backups$MATCHBOX
	fi

	if [ ! -f /opt/theme-customizer/backups$MATCHBOX2 ]; then
	mkdir -p /opt/theme-customizer/backups$MATCHBOX2
	rm -rf /opt/theme-customizer/backups$MATCHBOX2
	fi

	if [ ! -f /opt/theme-customizer/backups$COLORS ]; then
	mkdir -p /opt/theme-customizer/backups$COLORS
	rm -rf /opt/theme-customizer/backups$COLORS
	fi

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Creating backups..."
	sleep 2
	if [ ! -f /opt/theme-customizer/backups$CSS ]; then
	cp -rf $CSS /opt/theme-customizer/backups$CSS
	fi

	if [ ! -f /opt/theme-customizer/backups$COLORS ]; then
	cp -rf $COLORS /opt/theme-customizer/backups$COLORS
	fi
	
	if [ ! -f /opt/theme-customizer/backups$GTKR ]; then
	cp -rf $GTKR /opt/theme-customizer/backups$GTKR
	fi
	
	if [ -f /opt/theme-customizer/backupsMATCHBOX ]; then
	cp -rf /opt/theme-customizer/matchbox/theme.xml $MATCHBOX
	else
	cp -rf $MATCHBOX /opt/theme-customizer/backups$MATCHBOX
	cp -rf /opt/theme-customizer/matchbox/theme.xml $MATCHBOX
	fi
	
	if [ -f /opt/theme-customizer/backupsMATCHBOX2 ]; then
	cp -rf /opt/theme-customizer/matchbox2/theme.xml $MATCHBOX2
	else
	cp -rf $MATCHBOX2 /opt/theme-customizer/backups$MATCHBOX2
	cp -rf /opt/theme-customizer/matchbox2/theme.xml $MATCHBOX2
	fi

	perl -pi -e 's/.*font_name =.*//g' $GTKR
	perl -pi -e 's/.*font: .*//g' $CSS

	perl -pi -e "s/Droid Sans/$FONT/g" /opt/theme-customizer/gtk-template-modded $MATCHBOX $MATCHBOX2 /opt/theme-customizer/css-template-modded

	
# setting the colors...

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up colors..."
	sleep 2

	COLOR1NEW="$12"
	COLOR2NEW="$13"
	COLOR3NEW="$14"
	COLOR4NEW="$15"

	perl -pi -e "s/.*color\[\"DefaultTextColor\"\].*/color\[\"DefaultTextColor\"\] = \"$COLOR1NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"SecondaryTextColor\"\].*/color\[\"SecondaryTextColor\"\] = \"$COLOR2NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"ActiveTextColor\"\].*/color\[\"ActiveTextColor\"\] = \"$COLOR3NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"DisabledTextColor\"\].*/color\[\"DisabledTextColor\"\] = \"$COLOR4NEW\"/g" $GTKR

	perl -pi -e "s/.*color\[\"TitleTextColor\"\].*/color\[\"TitleTextColor\"\]        = \"$COLOR1NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"ButtonTextColor\"\].*/color\[\"ButtonTextColor\"\]        = \"$COLOR1NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"ButtonTextPressedColor\"\].*/color\[\"ButtonTextPressedColor\"\]        = \"$COLOR3NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"ButtonTextDisabledColor\"\].*/color\[\"ButtonTextDisabledColor\"\]        = \"$COLOR4NEW\"/g" $GTKR

	perl -pi -e "s/.*color\[\"AccentColor1\"\].*/color\[\"AccentColor1\"\] = \"$COLOR3NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"AccentColor2\"\].*/color\[\"AccentColor2\"\] = \"$COLOR1NEW\"/g" $GTKR
	perl -pi -e "s/.*color\[\"AccentColor3\"\].*/color\[\"AccentColor3\"\] = \"$COLOR3NEW\"/g" $GTKR

	perl -pi -e "s/DefaultTextColor.*/DefaultTextColor=$COLOR1NEW/g" $COLORS
	perl -pi -e "s/SecondaryTextColor.*/SecondaryTextColor=$COLOR2NEW/g" $COLORS
	perl -pi -e "s/ActiveTextColor.*/ActiveTextColor=$COLOR3NEW/g" $COLORS
	perl -pi -e "s/DisabledTextColor.*/DisabledTextColor=$COLOR4NEW/g" $COLORS

	perl -pi -e "s/TitleTextColor.*/TitleTextColor=$COLOR1NEW/g" $COLORS
	perl -pi -e "s/ButtonTextColor.*/ButtonTextColor=$COLOR1NEW/g" $COLORS
	perl -pi -e "s/ButtonTextPressedColor.*/ButtonTextPressedColor=$COLOR3NEW/g" $COLORS
	perl -pi -e "s/ButtonTextDisabledColor.*/ButtonTextDisabledColor=$COLOR4NEW/g" $COLORS

	perl -pi -e "s/AccentColor1.*/AccentColor1=$COLOR3NEW/g" $COLORS
	perl -pi -e "s/AccentColor2.*/AccentColor2=$COLOR1NEW/g" $COLORS
	perl -pi -e "s/AccentColor3.*/AccentColor3=$COLOR3NEW/g" $COLORS

	perl -pi -e "s/#COLOR1/$COLOR1NEW/g" /opt/theme-customizer/css-template-modded
	perl -pi -e "s/#COLOR2/$COLOR2NEW/g" /opt/theme-customizer/css-template-modded
	perl -pi -e "s/#COLOR3/$COLOR3NEW/g" /opt/theme-customizer/css-template-modded
	perl -pi -e "s/#COLOR4/$COLOR4NEW/g" /opt/theme-customizer/css-template-modded

	sleep 2

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

	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"
	sleep 2

	let KORVAA1=13$MUUTTUJA
	perl -pi -e "s/13"/${KORVAA1}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA2=15$MUUTTUJA
	perl -pi -e "s/15"/${KORVAA2}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA3=18$MUUTTUJA
	perl -pi -e "s/18"/${KORVAA3}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA4=20$MUUTTUJA
	perl -pi -e "s/20"/${KORVAA4}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA5=24$MUUTTUJA
	perl -pi -e "s/24"/${KORVAA5}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA6=26$MUUTTUJA
	perl -pi -e "s/26"/${KORVAA6}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA7=36$MUUTTUJA
	perl -pi -e "s/36"/${KORVAA7}"/g" /opt/theme-customizer/gtk-template-modded
	let KORVAA8=52$MUUTTUJA
	perl -pi -e "s/52"/${KORVAA8}"/g" /opt/theme-customizer/gtk-template-modded


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


	fi

# let's set icon theme

	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up Icon theme to: $3"
	perl -pi -e "s/.*gtk-icon-theme-name.*/gtk-icon-theme-name = \"$3\"/g" $GTKR
	sleep 2

	sed -i "/MODDING STARTS/,/MODDING ENDS/d" $CSS
	sed -i "/MODDING STARTS/,/MODDING ENDS/d" $GTKR
#	sed -i "/MODDING STARTS/,/MODDING ENDS/d" $COLORS

	cat /opt/theme-customizer/css-template-modded >> $CSS
	cat /opt/theme-customizer/gtk-template-modded >> $GTKR
#	cat /opt/theme-customizer/colors-template-modded >> $COLORS

	rm /opt/theme-customizer/css-template-modded
	rm /opt/theme-customizer/gtk-template-modded
#	rm /opt/theme-customizer/colors-template-modded


	hildon-theme-cacher $ACTUALTHEMEDIR

# finally let's set shortcut size and bookmark size and choose to show or not to show icon backgrounds on desktop

	run-standalone.sh gconftool -s /apps/osso/hildon-home/task-shortcuts-size -t int $5
	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up app shortcut size to: $5"
	sleep 2

	run-standalone.sh gconftool -s /apps/osso/hildon-home/task-bookmarks-size -t int $16
	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Setting up web bookmarks size to: $16"
	sleep 2

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

	run-standalone.sh gconftool -s /apps/osso/hildon-home/task-shortcuts-back -t int 0
	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Hiding app shortcut backgrounds..."
	sleep 2

	else

	run-standalone.sh gconftool -s /apps/osso/hildon-home/task-shortcuts-back -t int 1
	run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Showing app shortcut backgrounds..."
	sleep 2

	fi

		if [ "$7" = "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 services, your screen will blink!"
		
		run-standalone.sh dbus-send --print-reply --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"Restarting services, your screen will blink! After all is done restarting device is recommended to reload all new values." uint32:0 string:'NAO OK!'
		
		sleep 5

		exit 0

		fi



exit 0
