#!bin/sh
#
# Qnext Linux Environment Detection Script
# 1.00


detect_kde_running()
{
    RETVAL=$(echo $KDE_FULL_SESSION | grep -c "true")
    echo IS_KDE_RUNNING:$RETVAL
    return $RETVAL
}


exec_konqueror()
{
    RETVAL=$(find /usr/bin -name "konqueror" | grep -c "konqueror")
    if [ $RETVAL -gt 0 ] ; then
	konqueror "$1" &
	exit 0
    fi
    return 127
}


exec_mozilla() 
{
    RETVAL=$(find /usr/bin -name "mozilla" | grep -c "mozilla")
    if [ $RETVAL -gt 0 ] ; then
	mozilla "$1" &
	exit 0
    fi
    return 127
}


exec_htmlview() 
{
    RETVAL=$(find /usr/bin -name "htmlview" | grep -c "htmlview")
    if [ $RETVAL -gt 0 ] ; then
	htmlview "$1" &
	exit 0
    fi
    return 127
}


exec_kfmclient()
{
    RETVAL=$(find /usr/bin -name "kfmclient" | grep -c "kfmclient")
    if [ $RETVAL -gt 0 ] ; then
	kfmclient exec file:"$1" &
	exit 0
    fi
    return 127
}


exec_nautilus()
{
    RETVAL=$(find /usr/bin -name "nautilus" | grep -c "nautilus")
    if [ $RETVAL -gt 0 ] ; then
	nautilus "$1" &
	exit 0
    fi
    return 127
}


#
# check our parameter count
#
if [ $# -eq 0 ] ; then
    echo "too few parameters"
    exit 1
fi


# 
# check if KDE is running
#
detect_kde_running

# 
# if KDE was detected we launch konqueror first for consistency
#
if [ $? -gt 0 ] ; then
    # use the best kfmclient first if possible
    exec_kfmclient "$1"

    # use konqueror if we are in KDE and kfmclient did not work
    exec_konqueror "$1"
fi

#
# if KDE was not running we try nautlius
#
exec_nautilus "$1"

#
# we try kfmclient now even if KDE was not running
#
exec_kfmclient "$1"

#
# we try konqueror now even if KDE was not running
#
exec_konqueror "$1"

#
# if we got this far it's time for a last ditch effort
# so we launch mozilla even if we didn't find it in case
# it's in a funny place, but still in the path
#
mozilla "$1" &




