#!/bin/bash

kde_su() {
    if which kdesu &> /dev/null; then
	kdesu $*
	return 0
    else
	return 1
    fi
}

gnome_su() {
    if which gksu &> /dev/null; then
	gksu $*
	return 0
    else
	return 1
    fi
}

redhat_su() {
    # consolehelper is a pain. We have to set it up in advance for each
    # command we want to run, instead of just passing the command string.

    # Prepend '-su' to the original command name. This should be a symlink to
    # consolehelper with corresponding config files in 
    # /etc/security/console.apps and /etc/pam.d
    cmd=`dirname $1`/su-`basename $1`
    if which consolehelper &> /dev/null && [ -f $cmd ]; then
	# Ideally we'd pass all the arguments except the first, but I don't
	# know how to do that, so just one
	# purposes
	$cmd $2
	return 0
    else
	return 1
    fi
}

if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
fi


# If already root, just run the command
if [ $UID = 0 ]; then
    $*
# Always try consolehelper (Redhat-only) first
# Then the gnome/KDE methods -- order depends on the current desktop
elif [ "$DE" = "kde" ]; then
    redhat_su $* || kde_su $* || gnome_su $*
else
    redhat_su $* || gnome_su $* || kde_su $*
fi

