#!/bin/sh
# Xfterm script revised in order to make it easier to expand and to be 
# more compatible with applications such as gnome-terminal

if [ "x$TERMCMD" = "x" ]; then
	for term in exo-open  Terminal xterminal xterm aterm rxvt
	do
	    if which "$term" > /dev/null 2>&1 ; then
		TERMCMD=$term
	    	break;
	    fi
	done
fi

if [ "x$TERMCMD" = "x" ]; then
	echo " *** Error ***: xfterm4: Could not find terminal to use."
	echo "                Please set the TERMCMD variable."
	exit 1
fi


if [ "$1" = "-e" ]; then
	if [ -n "$2" ]; then
		shift
		ESTRING="$@"
	else
		shift
	fi
fi	

# Add an entry for your own strange non-standard xterm replacement here
case "$TERMCMD" in
	"powershell")
		EXEC="--execute="
		# The following dosen't seem to work, but it's the best
		# equalient to a title switch powershell has
		TITLE="--name="
		;;
	"gnome-terminal"|"gnome2-terminal"|"Terminal"|"terminal"|"xterminal")
		EXEC="-x "
		TITLE="--title "
		;;
	"Eterm")
		EXEC="-e "
		TITLE="-T "
		;;
	"exo-open")
		EXEC=""
		TITLE=""
		TERMCMD="$TERMCMD --launch TerminalEmulator"
		;;
	*)
		EXEC="-e "
		TITLE="-title "
		;;
esac

if [ ! "x$ESTRING" = "x" ]; then
	TSTRING="$ESTRING"
elif [ "x$1" = "x" ]; then
	TSTRING="Terminal"
	ESTRING=""
	unset EXEC
elif [ -d "$*" ]; then
	cd "$*"
	TSTRING="Terminal"
	ESTRING=""
	unset EXEC
elif [ -x "$*" ]; then
	cd `dirname "$*"`
	TSTRING=`basename "$*"`
	ESTRING="`which pauseme`"
	MSTRING="$*"
elif [ -f "$*" ]; then
	TSTRING="Viewing $*"
	ESTRING="`which less`"
	MSTRING="$*"
elif [ "`echo $* | grep "http:/"`" -o "`echo $* | grep "ftp:"`" ]; then
	# This requires lynx or links. If you don't have it, don't drop URLs.
	if which lynx >/dev/null 2>&1; then
		TSTRING="Lynx: $*"
		ESTRING="`which lynx`"
	elif which links >/dev/null 2>&1; then
		TSTRING="Links: $*"
		ESTRING="`which links`"
	fi
	MSTRING="$*"
else
	exec $TERMCMD $@
fi


# Ugly, why can't they just make all terms work the same way?
# Note that you cannot feed this script with a text file which
# contains spaces in its name and/or path if your $TERMCMD = powershell
if [ x"$TITLE" = x"" -o x"$EXEC" = x"" ]; then
	exec $TERMCMD $ESTRING
elif [ "$TERMCMD" = "powershell" ]; then
 	exec $TERMCMD $TITLE"$TSTRING" $EXEC"$ESTRING $MSTRING"
elif [ "x$MSTRING" = "x" ]; then
	exec $TERMCMD $TITLE"$TSTRING" $EXEC$ESTRING
else
	exec $TERMCMD $TITLE"$TSTRING" $EXEC$ESTRING "$MSTRING"
fi

exit 0			
