#!/bin/sh
#PupShutdown 3.0
#launch wrapper
#20130204 Internationalized (vicmz)

export TEXTDOMAIN=pupshutdown
export OUTPUT_CHARSET=UTF-8

[ "`whoami`" != "root" ] && exec sudo -A ${0} ${@}

WORKDIR="/usr/local/PupShutdown"
EXIT_STATUS1=$(cat $HOME/.PupShutdown/pupshutdown.rc | grep -a EXIT= | tail -n 1 | cut -d "=" -f 2 | sed 's/\"//g')
EXIT_STATUS2=$(cat $WORKDIR/PSD-apps.rc | grep -a EXIT= | tail -n 1 | cut -d "=" -f 2 | sed 's/\"//g')
RESTORE=$(cat $HOME/.PupShutdown/pupshutdown-restore.rc | grep -a NEWFONTCOLOR= | cut -d "=" -f 2  | sed 's/\"//g')

if [ "$EXIT_STATUS1" = "abort" -o "$EXIT_STATUS1" = "" ]; then
	echo "$RESTORE" > /tmp/PSD-color.tmp
	grep -aE 'NEWFONT|NEWGRAPHIC|NEWLABEL|EXIT="quit_now' $HOME/.PupShutdown/pupshutdown-restore.rc > $HOME/.PupShutdown/pupshutdown.rc
else
	grep -aE 'NEWFONT|NEWGRAPHIC|NEWLABEL|EXIT="quit_now' $HOME/.PupShutdown/pupshutdown.rc > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.PupShutdown/pupshutdown.rc
	cp -f $HOME/.PupShutdown/pupshutdown.rc $HOME/.PupShutdown/pupshutdown-restore.rc 2>/dev/null
fi

if [ "$EXIT_STATUS2" = "abort" -o "$EXIT_STATUS2" = "" ]; then
	cp -f $WORKDIR/PSD-apps2.rc $WORKDIR/PSD-apps.rc &
else
	cp -f $WORKDIR/PSD-apps.rc $WORKDIR/PSD-apps2.rc &
fi

if [ "`pidof PSD-pref`" ]; then
	sleep .8
	[ "`pidof PSD-pref`" ] && Xdialog --title Alert --beep --msgbox "  $(gettext 'Another instance of PupShutdown will not  \n  run when the Preferences dialog is active.')  " 0 0 && exit 0
fi

if	[ "`pidof PSD-bin`" ]; then
	 sleep .8
	[ "`pidof PSD-bin`" ] && Xdialog --title Alert --beep --msgbox "  $(gettext 'PupShutdown is already active.')  " 0 0 && exit 0
fi

$WORKDIR/PSD-bin &

#end
