#!/bin/sh
# launch something as root
# 
# (c) Eko M. Budi, 2004
# (c) VLocity Linux, 2004
#
# Released under GNU GPL

vdir=$(dirname $0)

. $vdir/../sbin/vasm-functions

if [ "$1" == "-h" ] || [ "$1" = "--help" ]; then
cat<< EOF
vsuper - launch a program as root
usage: vuser title command [arguments ...]
example:
vuser "HPPTD" service lampp start
EOF
fi

if ! which $1 2>/dev/null && which $2 2>/dev/null;then
  TITLE=$1
  shift
  COMMANDS="$@"
else
  TITLE=$1
  COMMANDS="$@"
fi
#echo $TITLE
#echo $COMMANDS

# root already ?
if [ "$UID" = 0 ]; then
    echo $COMMANDS
    $COMMANDS
elif [ -z "$DISPLAY" ]; then
    echoc "Program $TITLE needs root privilege." cyan
    su root -c $COMMANDS
elif which gksu &>/dev/null; then
   GKSU=`which gksu`
   if grep vsuper $GKSU >/dev/null;then
    passwordbox "This program needs root privilege. Please enter the password:" "$TITLE"
    result=$?
    if [ $result = 0 ]; then
	USERPASS1="`cat $freply`"
	xhost +si:localuser:root &> /dev/null
	$vdir/superx "$USERPASS1" "$COMMANDS" > /dev/null
	result=$?
	xhost -si:localuser:root &> /dev/null
	if [ $result != 0 ]; then 
	    infobox "Wrong password, cannot run $TITLE"
	    sleep 2
	fi
    fi
   else
    gksu "$COMMANDS"
   fi
elif which kdesu &>/dev/null; then
    kdesu --caption "$TITLE" -c "$COMMANDS"
else
    passwordbox "This program needs root privilege. Please enter the password:" "$TITLE"
    result=$?
    if [ $result = 0 ]; then
	USERPASS1="`cat $freply`"
	xhost +si:localuser:root &> /dev/null
	$vdir/superx "$USERPASS1" "$COMMANDS" > /dev/null
	result=$?
	xhost -si:localuser:root &> /dev/null
	if [ $result != 0 ]; then 
	    infobox "Wrong password, cannot run $TITLE"
	    sleep 2
	fi
    fi
fi

clean_exit $result

