#!/bin/bash
#
# Launch Preferred Terminal
# Usage:
#	launchterm
#
# This script base is launchmail.
#
# This script is in the public domain.

error_dialog() {
    echo "$1"
    if [ -x /usr/bin/zenity ]; then
        /usr/bin/zenity --error --text="$1"
    else
        xmessage "$1"
    fi
}

sanity_check() {
	unset INVALID
	echo "$1" | grep -q "launchterm" && INVALID="yes"
	echo "$1" | grep -q "gnome-open" && INVALID="yes"
	if [ "$INVALID" == "yes" ]; then
		error_dialog "$1 is an invalid terminal.  Please reconfigure."
		[ -x /usr/bin/gnome-default-applications-properties ] && exec /usr/bin/gnome-default-applications-properties
		exit 1
	fi
}

exists() {
    which "${1%% *}" 2> /dev/null > /dev/null
    return $?
}

# Read GNOME configuration
if [ -x /usr/bin/gconftool-2 ]; then
	# Pull key from gconf, remove %s or "%s", trim leading & trailing spaces
	GCONF=$(gconftool-2 -g /desktop/gnome/applications/terminal/exec 2>/dev/null | sed -e 's/%s//; s/\"\"//; s/^\ *//; s/\ *$//')

	# Remove arguments
	GCONF="`echo $GCONF | cut -f1 -d" "`"

	# sanity check (prevent infinite loops)
	sanity_check "$GCONF"

fi

##
exec $GCONF $*
