#!/bin/bash

available(){
    which $1 >/dev/null 2>&1
}

MESSAGE="Please run ghostvpn as root with sudo or install gksu / kdesudo!"

export LD_LIBRARY_PATH="/opt/ghostvpn/lib/"

if [ `whoami` != "root" ]; then

    # choose either gksudo or kdesudo
    # if both are avilable check whoch desktop is running
    if available gksudo
    then 
        SUDO=`which gksudo`
    fi
    if available kdesudo
    then 
        SUDO=`which kdesudo`
    fi
    if ( available gksudo && available kdesudo )
    then 
        if [ $XDG_CURRENT_DESKTOP = "KDE" ]; then
	    SUDO=`which kdesudo`;
        else
	    SUDO=`which gksudo`
        fi
    fi
    
    # prefer polkit if available
    if available pkexec
    then
       SUDO=`which pkexec`
    fi

    if [ -z $SUDO ]; then
        if available zenity; then
            zenity --info --text "$MESSAGE"
            exit 0
        elif available notify-send; then
            notify-send "$MESSAGE"
            exit 0
        elif available xmessage notify-send; then
            xmessage -buttons Ok:0 "$MESSAGE"
            exit 0
        else
	    echo "$MESSAGE"
        fi
    fi
fi

$SUDO /opt/ghostvpn/bin/ghostvpn
