#!/usr/bin/env bash

# Copyright (C) 2008-2013 Bob Hepple
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# http://bhepple.freeshell.org

check_and_process_opts() {
    ARGS="
ARGP_DELETE=quiet
ARGP_VERSION=$VERSION
ARGP_PROG=$PROG
##############################################################   
#OPTIONS:
#name=default sname arg       type range           description
##############################################################   
USE_X=''      X     ''        b    '$DISPLAY'      prefer X instead of console
##############################################################   
ARGP_ARGS=[--] $ARGUMENTS
ARGP_SHORT=$SHORT_DESC
ARGP_USAGE=$USAGE"

    exec 4>&1
    eval $(echo "$ARGS" | argp.sh "$@" 3>&1 1>&4 || echo exit $? )
    exec 4>&-

    export PREFER_X=$USE_X

    NEW_ARGS=( "$@" )
    return 0
}

##########################
#         M A I N        #
##########################

PROG=$(basename $0)
DIR=$(dirname $0)
VERSION="7"
# customise this for your favourite:
#XTERM="konsole --schema WhiteOnBlack"
#XTERM="urxvtc -rv"
XTERM="myterm --"
ARGUMENTS="[command]"

USAGE="Run 'command' as root via the 'root' program if available \
else sudo. If command is not given then start up a root shell.

You might want to customise the XTERM setting in this script if you \
want something different from \"$XTERM\"

Note that you need to take care of the /etc/sudoers file by adding the \
NOPASSWD:: flag to your line. Ideally be a member of wheel group and:

%wheel  ALL=(ALL)       NOPASSWD: ALL
"
SHORT_DESC="Run as root"
ARGUMENTS="[command]"
export VERBOSE=${VERBOSE:-""}
USE_X=""
SUDO=sudo
export POSIXLY_CORRECT=1

NEW_ARGS=( )
check_and_process_opts "$@" && set -- "${NEW_ARGS[@]}"
unset POSIXLY_CORRECT

[ "$VERBOSE" ] && set -x

if echo $PATH|grep sbin >/dev/null 2>&1 ; then
    :
else
    PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH
fi

if type root &>/dev/null; then
    SUDO=root
fi

if [ $# -gt 0 ]; then
    # we have parameters - just do it and exit
    exec $SUDO "$@"
else
    # No parameters - run a su shell

    V=`echo 'echo $BASH_VERSINFO' |bash`
    if [ "$V" -lt 3 ]; then
        OPT="-"
    else
        OPT="-l"
    fi

    unset BASH_RC BASH_PROFILE
    unset BH_BASH_RC

    if [ "$DISPLAY" -a "$USE_X" ]; then
        xhost + localhost >/dev/null 2>&1
        export TERM_BACKGROUND
        #$SUDO rxvt -ls -tn rxvt -sk -rv -title raita -e bash $OPT &
        # Have to jump through hoops to set an environment param. sudo
        # on gentoo is 1.6.8p12 and rejects the -E/SETENV: options
        # despite what the changelog says, so I'm not using that.
        [ "$TERM_BACKGROUND" = dark ] && TERM_BACKGROUND=light || TERM_BACKGROUND=dark
        exec $XTERM -e $SUDO bash -c "TERM_BACKGROUND=$TERM_BACKGROUND bash $OPT"
    else
        exec $SUDO bash $OPT
    fi
fi
