#!/bin/bash
#
# gksu replacement
# 
# save as /usr/local/bin/gksu
# mark executable: chmod +x /usr/local/bin/gksu
# set readonly for all, writable for root and group root
# use gksu command as usual

# exit, if no gui
if ! [ x$DISPLAY != x ] ; then
  echo "no gui detected - exiting!"
  exit 1
fi

# fetch command to execute
execcmd=$@

# fetch current user name
curuser=$(whoami)

# message to show
msg="sudo $execcmd\n\nEnter password for $curuser:"

# request user password
if PASS=$(zenity --entry --hide-text --text "$msg" --title "GKSU" 2> /dev/null); then
  if ! [ -z $PASS ]; then
    # check if password is valid
    echo "$PASS" | sudo -S -i -k pwd > /dev/null 2> /dev/null
    if ! [ $? -eq 0 ]; then
      # password is not valid: show warning
      zenity --warning --no-wrap --text "The password supplied was invalid!" --title "GKSU" 2> /dev/null
    else
      # password is valid: execute command
      echo "$PASS" | sudo -S -i -k $execcmd 2> /dev/null
      # eval exit code of command
      if ! [ $? -eq 0 ]; then
        zenity --warning --no-wrap --text "The command supplied could not be executed!" --title "GKSU" 2> /dev/null
      fi
    fi
  else
    # empty password: show warning
    zenity --warning --no-wrap --text "An empty password was supplied!" --title "GKSU" 2> /dev/null
  fi
fi

# clean exit
exit 0