#!/bin/bash
#
# it's a maintainance script, which can be called to add the 
# icon on the Desktop every time, it might get lost. Therefore
# two commands are possible
# - install: will add the icon
# - remove: will delete the icon
#
# Note: Just before you change ICONPATCH, please remove the old ICONPATH.
#       Otherwise you can detect, if the ICONPATCH is already done
#
# todo: do kill and restart AsusLauchner automatically
#       but first check, if this works with all desktop modes

set -e

PATCHFILE="/opt/xandros/share/AsusLauncher/simpleui.rc"
LAUNCHER="/opt/xandros/bin/AsusLauncher"

# search pattern
PATTERNs1="<parcel simplecat=\"Internet\" extraargs=\"/usr/bin/sudo /usr/bin/GlobeTrotterConnect\""

# first and last line of patch text with quoted "/"
PATTERNr1="<parcel simplecat=\"Internet\" extraargs=\"\/usr\/bin\/sudo \/usr\/bin\/GlobeTrotterConnect\""
PATTERNrl="<\/parcel>"

# patch pattern with double quoted "
PATTERN01="<parcel simplecat=\\\"Internet\\\" extraargs=\\\"/usr/bin/sudo /usr/bin/GlobeTrotterConnect\\\""
PATTERN02=" 	icon=\\\"icon225_wireless_networks_norm.png\\\""
PATTERN03="	selected_icon=\\\"wireless_networks_hi.png\\\">"
PATTERN04="	<name lang=\\\"en\\\">ICON 225</name>"
PATTERN05="	<name lang=\\\"zh_TW\\\">iCON 225</name>"
PATTERN06="	<name lang=\\\"zh_HK\\\">iCON 225</name>"
PATTERN07="	<name lang=\\\"zh_CN\\\">iCON 225</name>"
PATTERN08="	<name lang=\\\"ar_AE\\\">iCON 225</name>"
PATTERN09="	<name lang=\\\"de_DE\\\">iCON 225</name>"
PATTERN10="	<name lang=\\\"es_AR\\\">iCON 225</name>"
PATTERN11="	<name lang=\\\"es_ES\\\">iCON 225</name>" 
PATTERN12="	<name lang=\\\"fr_FR\\\">iCON 225</name>"
PATTERN13="	<name lang=\\\"it_IT\\\">iCON 225</name>"
PATTERN14="	<name lang=\\\"nl_NL\\\">iCON 225</name>"
PATTERN15="	<name lang=\\\"pt_PT\\\">iCON 225</name>"
PATTERN16="	<name lang=\\\"ru_RU\\\">iCON 225</name>"
PATTERN17="	<name lang=\\\"th_TH\\\">iCON 225</name>"
PATTERN18="	<name lang=\\\"tr_TR\\\">iCON 225</name>"
PATTERN19="	<name lang=\\\"ja_JP\\\">iCON 225</name>"
PATTERN20="	<name lang=\\\"ko_KR\\\">iCON 225</name>"
PATTERN21="	<name lang=\\\"cs_CZ\\\">iCON 225</name>"
PATTERN22="	<name lang=\\\"hu_HU\\\">iCON 225</name>"
PATTERN23="	<name lang=\\\"sk_SK\\\">iCON 225</name>"
PATTERN24="	<name lang=\\\"el_GR\\\">iCON 225</name>"
PATTERN25="	<name lang=\\\"pl_PL\\\">iCON 225</name>"
PATTERN26="</parcel>"


function check_files() {
	if [ ! -e "${PATCHFILE}" ]
	then
		echo "ERROR: ${PATCHFILE} not found"
		exit 0
	fi

#	if [ ! -e "${LAUNCHER}" ]
#	then
#		echo "ERROR: ${LAUNCHER} not found"
#		exit 0
#	fi
}

function check_uid() {
	if [ $( id -u) != "0" ]
	then
		echo "ERROR: wrong uid, call add-getconnect-icon as root user"
		exit 0
	fi
}

# fully untested
function restart_Launcher() {
#   killall AsusLauncher
#   sleep 1
#   su user -c /opt/xandros/bin/AsusLauncher&
    echo "done"
}


# check if the first line exists. Then we assume the pacth is already done

case "${1}" in
	add|install)
		check_files
		check_uid
                X="$(grep "${PATTERNs1}" "${PATCHFILE}" || true)"
	        if [ -z "${X}" ]
                then
	               # applay patch
	               mv -f ${PATCHFILE} ${PATCHFILE}.tmp
	               awk '/\s*<\/simpleui>/ {
                           print "'"${PATTERN01}"'";
                           print "'"${PATTERN02}"'";
                           print "'"${PATTERN03}"'";
                           print "'"${PATTERN04}"'";
                           print "'"${PATTERN05}"'";
                           print "'"${PATTERN06}"'";
                           print "'"${PATTERN07}"'";
                           print "'"${PATTERN08}"'";
                           print "'"${PATTERN09}"'";
                           print "'"${PATTERN10}"'";
                           print "'"${PATTERN11}"'";
                           print "'"${PATTERN12}"'";
                           print "'"${PATTERN13}"'";
                           print "'"${PATTERN14}"'";
                           print "'"${PATTERN15}"'";
                           print "'"${PATTERN16}"'";
                           print "'"${PATTERN17}"'";
                           print "'"${PATTERN18}"'";
                           print "'"${PATTERN19}"'";
                           print "'"${PATTERN20}"'";
                           print "'"${PATTERN21}"'";
                           print "'"${PATTERN22}"'";
                           print "'"${PATTERN23}"'";
                           print "'"${PATTERN24}"'";
                           print "'"${PATTERN25}"'";
                           print "'"${PATTERN26}"'";
			   }
                           {
		           print
		           }' ${PATCHFILE}.tmp > ${PATCHFILE}
		       restart_Launcher
		else
		       echo "nothing to do"
		fi
		;;

	del|remove)
		check_files
		check_uid
                X="$(grep "${PATTERNs1}" "${PATCHFILE}" || true)"
	        if [ -n "${X}" ]
                then
	               # remove patch
	               mv -f ${PATCHFILE} ${PATCHFILE}.tmp
	               sed -n -e "/${PATTERNr1}/,/${PATTERNrl}/!p" ${PATCHFILE}.tmp > ${PATCHFILE}
		       restart_Launcher
		else
		       echo "nothing to do"
		fi
		;;
	help|--help)

		echo "add-gtconnect-icon"
		echo " maintenance script with patches the simpleui.rc. You should now reboot your"
		echo " machine to finalize your changes"
#		echo " maintenance script with patches the simpleui.rc and restarts the AsusLauncher"
		echo
		echo "synopsis:"
		echo " add-gtconnect-icon <command>"
		echo
		echo "command:"
		echo " The following commands are defined"
		echo
		echo "install"
#		echo "     install the patch and restart AsusLauncher, if the patch is not already"
		echo "     install the patch, if the patch is not already"
		echo "     installed. Otherwise nothing happen."
		echo
		echo "remove"
#		echo "     remove the patch and restart AsusLauncher, if the patch is not already"
		echo "     remove the patch, if the patch is not already"
		echo "     removed. Otherwise nothing happen."
		echo
		echo "help"
		echo "     show this help screen. No Argument is required"
		echo
		echo "version"
		echo "     show current version of this script"
		;;

        version|--version)
                echo "add-gtconnect-icon version 0.2"
		;;
	*)
		echo "add-gtconnect-icon is called with unknown command \`${1}'" >&2
		$0 help
		exit 1
		;;

esac

exit 0


