#!/bin/bash

if test -r ~/.su-to-rootrc; then
. ~/.su-to-rootrc
fi

PRIV=root
COMMAND=
NEEDS=text


eshell() {
   getent passwd $1 | cut -f7 -d:
}
usage () {
  echo usage: $0 '[-X] [-p <user>] -c <command>' >&2
  echo '-X: command is a X11 program' >&2
  exit 1
}

for i in "$@"; do
   case "$prev" in
     -p)
       PRIV="$i";;
     -c)
       COMMAND="$i";;
     -X) 
       NEEDS="X11";;
   esac
   prev="$i"
done

if [ -z "$COMMAND" ] ; then
   usage;
fi

euid=$(id -u)
privid=$(id -u $PRIV)
if test "$euid" = "$privid"; then
  $COMMAND
else
  case $NEEDS in
  text)
    if test "$euid" != 0; then
      echo About to execute $COMMAND. 
      echo This command needs $PRIV privileges to be executed.
    fi
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin
    SHELL=`eshell $PRIV`
    if test "X$SU_TO_ROOT_SU" = "Xsux"; then
      echo "Using sux..."
      echo enter $PRIV passwd:
      while ! sux -p "$PRIV" "$COMMAND"; do
        echo -n 'Incorrect password or command failed. Try again? (y/n)'
        read ans
        if test "$ans" != "y" -a "$ans" != "Y"; then
          exit 1
        fi
      done
    else
      echo enter $PRIV passwd:
      while ! su -p "$PRIV" -c "$COMMAND"; do
        echo -n 'Incorrect password or command failed. Try again? (y/n)'
        read ans
        if test "$ans" != "y" -a "$ans" != "Y"; then
          exit 1
        fi
      done
    fi;;
  X11)
    if test -z "$SU_TO_ROOT_X"; then
      if which gksu >/dev/null 2>&1 ; then
        SU_TO_ROOT_X=gksu
      elif which kdesu >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=kdesu
      elif which sux >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=sux
      else
        SU_TO_ROOT_X=su-to-root
      fi
    fi
    if test "X$SU_TO_ROOT_X" = "Xgksu" ; then
      gksu -u "$PRIV" "$COMMAND"
    elif test "X$SU_TO_ROOT_X" = "Xkdesu" ; then 
      kdesu -u "$PRIV" "$COMMAND"
    elif test "X$SU_TO_ROOT_X" = "Xsux" ; then
      env SU_TO_ROOT_SU=sux \
        x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND"  
  # As a last resort, open a new x-terminal-emulator and prompt for the password
  # Do not use -X here!
    else
      x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND"
    fi;;
  esac
fi
