#!/bin/sh
#
# Gtkdialog to execute program as root from a WM menu.
# (c) SliTaz GNU/Linux 2008-2010 - GNU gpl.
#
VERSION=20100324

# Usage.
if [ -z "$1" ]; then
    echo "Slitaz subox version : $VERSION"
    echo -e "\nUsage : subox program\n"
    exit 0
fi

# Nothing to do if we are root
test $(id -u) = 0 && exec $1

# Include gettext helper script.
. /usr/bin/gettext.sh

# Export package name for gettext.
TEXTDOMAIN='subox'
export TEXTDOMAIN

# Keep command in an exported variable to be used by SU_DIALOG.
export SU_CMD=$1

# Main window and root password default entry set to 'root'.
SU_DIALOG='
<window title="Subox" skip_taskbar_hint="true">
<vbox>

	<text use-markup="true">
		<label>"
<b>SliTaz - Subox</b>"
		</label>
	</text>
	<text wrap="true" width-chars="50">
		<label>"'`gettext "
Please enter root password (default root) to execute :"`'
'$@'
"
		</label>
	</text>
	
	<hbox>
		<text use-markup="true">
			<label>"'`gettext "<b>Admin password :</b>"`'"</label>
		</text>
		<entry visibility="false">'

# Check if we have a saved passwd.
if [ -s $HOME/.config/slitaz/subox.conf ]; then
	PASSWD=`cat $HOME/.config/slitaz/subox.conf`
	[ -n "$PASSWD" ] && SU_DIALOG="$SU_DIALOG <default>$PASSWD</default>"
fi

export SU_DIALOG=''${SU_DIALOG}'
			<variable>PASSWD</variable>
		</entry>
	</hbox>
	
	<hbox>
		<checkbox>
			<label>"'`gettext "Autosave password"`'"</label>
			<variable>AUTOSAVE</variable>
			<default>false</default>
		</checkbox>
		<button ok>
			<action>echo $PASSWD | su -c "$SU_CMD" &</action>
			<action>[ $AUTOSAVE == true ] && echo $PASSWD > '$HOME/.config/slitaz/subox.conf'</action>
			<action>[ $AUTOSAVE == true ] && chmod 0600 '$HOME/.config/slitaz/subox.conf'</action>
			<action type="closewindow">SU_DIALOG</action>
		</button>
		<button cancel></button>
	</hbox>

</vbox>
</window>'

gtkdialog --center --program=SU_DIALOG > /dev/null

exit 0
