#!/bin/bash
MYDIR=${0%/*}
MYDIR=${MYDIR%/bin}

function sayboth() {
	echo "$*"
	xmessage -center -timeout 3 "$*"
	sleep 3;
}

function failout() {
	sayboth "UPDATE FAILED!
$*"
	sleep 5;
	exit 1
}

echo "Checking Privileges ..."
if [ "$(id -u)" != "0" ] ; then 
	sayboth "Administrator password required."
	if [ "$(which gksu)" != "" ] ; then
		exec gksu -k $0 "$@"
	fi
	if [ "$(which kdesu)" != "" ] ; then
		exec kdesu -c $0 "$@"
	fi
	if [ "$(which sudo)" != "" ] ; then
		exec sudo $0 "$@"
	fi
	if [ "$(which su)" != "" ] ; then
		exec su -c "$0 $@"
	fi
fi

echo "Changing to /cv directory."
cd /cv || failout "Cannot change to /cv directory"

echo "Checking update consistency."
tar tzf "$1" >/dev/null || failout "Update is not a valid .tgz file."

sayboth "Starting extraction of update."

rm -f "./ECCET_AUTOEXEC" 2>/dev/null
tar xzf "$1" || failout "Update failed."

tar tzf "$1" ./ECCET_AUTOEXEC 2>/dev/null >/dev/null && {
	sayboth "Update contained a postinstall script. Running it."
	./ECCET_AUTOEXEC || sayboth "Postinstall script returned failure."
}

sayboth "Update beendet."
sleep 10
exit 0
