#!/bin/sh
# xaumix:  run aumix in ncurses mode in a terminal emulator under X
# copyright (C) 1999,2000 Paul Slootman <paul@debian.org>
# released under GNU General Public License
#
# - Initial idea and version by Paul Slootman
#
# - Searching for different terminal emulator and the accompanying options
#   by Trevor Johnson <trevor@jpj.net> (and others?)
#
# - Reworked to first select a suitable terminal emulator and then select
#   the appropriate options by Paul Slootman to enable usage of debian's
#   alternatives mechanism, where the local admin can select what the
#   default terminal emulator is. (I don't know if other distributions have
#   similar mechanisms?)
#
# - Modified by Eduard Bloch <blade@debian.org> to try x-terminal-emulator and
#   rely on the new behaviour of the menu system.

set -e  # exit upon any error

# Are we under X11?
if test -z "$DISPLAY"; then
	exec aumix
	exit 1
fi

XTERM=  # nothing found yet

if [ -x /usr/sbin/update-alternatives ]; then   # debian
	# Example line:
	#	link currently points to /usr/X11R6/bin/rxvt-xterm
	XTERM=`/usr/sbin/update-alternatives --display x-terminal-emulator |
		grep 'link currently' | sed 's,.* points to .*/\(.*\)$,\1,'`
else
	XTERM=''
fi
# from gzexe:  add colon to internal field separator because PATH
# is colon-delimited
IFS=${IFS= 	}
saveifs=$IFS
IFS=${IFS}:
# Test for GNOME before KDE:  I flipped a coin. --Trevor
if [ -z "$XTERM" ] && xprop -root | grep -q '^GNOME_SM_PROXY'; then
	for dir in $PATH; do
		# Although gnome-terminal works, it gives error messages:
 		#	Gdk-WARNING **: locale not supported by C library
 		#	GnomeUI-WARNING **: Could not open help topics file NULL
	    if test -x $dir/gnome-terminal.wrapper; then XTERM=gnome-terminal.wrapper; break; fi
	done
fi
if [ -z "$XTERM" ] && xprop -root | grep -q 'Unnamed Desktop'; then
	for dir in $PATH; do
		if test -x $dir/kvt; then XTERM=kvt; break; fi
		#    This one doesn't do color.  Also it gives an error message:
		# 	TEScreen.C(172) : setRegion(0,64) : bad range.
		# if test -f $HOME/.kde/share/config/konsolerc; then
		#	if test -x $dir/konsole; then XTERM=konsole; break; fi
	done
fi

if [ -z "$XTERM" ]; then	# find an available xterm-alike
	for dir in $PATH; do
		if test -x $dir/xterm; then PREFERRED=xterm; break; fi
		if test -x $dir/rxvt;  then PREFERRED=rxvt;  break; fi
		if test -x $dir/aterm; then PREFERRED=aterm; break; fi
		if test -x $dir/kterm; then PREFERRED=kterm; break; fi
		if test -x $dir/wterm; then PREFERRED=wterm; break; fi
		if test -x $dir/Eterm; then PREFERRED=Eterm; break; fi
		if test -x $dir/eterm; then PREFERRED=eterm; break; fi
	done
fi
IFS=$saveifs

# set defaults
XTERMOPTIONS='-T aumix -n aumix'
GEOMETRY='-geometry 79x${LINES}'
SCHEME='-C ansi'

case "$XTERM" in
	gnome-terminal.wrapper)
		XTERMOPTIONS='-t aumix --name aumix'
		GEOMETRY='--geometry 79x${LINES}'
		;;
	kvt)	XTERMOPTIONS='-no_scrollbar -sl 0 -caption aumix -n aumix'
		GEOMETRY='-vt_geometry 79x${LINES}'
		SCHEME='-C xterm'
		;;
	konsole)
		XTERMOPTIONS=' -sl 0 -nowelcome -caption aumix -n aumix'
		GEOMETRY='' #'-vt_sz 79x{LINES}' doesn't work
		SCHEME='-C xterm'
		;;
	xterm*|rxvt*|aterm)
		XTERMOPTIONS='-rv +sb -T aumix -n aumix'
		;;
	kterm)	XTERMOPTIONS=' -T aumix -n aumix'
		;;
	wterm)	XTERMOPTIONS='-T aumix -name aumix -rv +sb'
		;;
	Eterm)	XTERMOPTIONS='--scrollbar=0 --term-name xterm -T aumix -n aumix'
		GEOMETRY='-g 79x${LINES}'
		;;
	# need to check geometry option for eterm
	# Is this the same as Eterm ("Enlightened Terminal Emulator
	# for X Windows[sic]")?
	eterm)	XTERMOPTIONS='-T aumix -n aumix'
		GEOMETRY=''
		SCHEME='-C xterm'
		;;
	*) if [ ! -z $XTERM ] ; then
   # we have x-terminal-emulator symlink that should allways support -e and -T.
   # If it doesn't, file bugs against the corresponding program
         echo "Unknown X terminal emulator. xaumix will invoke $XTERM now
   (pointed by your x-terminal-emulator alternatives entry)"
         # safer options
         XTERMOPTIONS=""
         GEOMETRY=""
         SCHEME=""
         #XTERM="x-terminal-emulator"
      fi
esac

if test -z "$XTERM"; then
	echo "xaumix:  no terminal emulator found" >&2
        exit 1
fi

if [ ! -z "$GEOMETRY" ]; then
	LINES=`aumix -q | wc -l`
	LINES=`expr $LINES + 1`
	eval GEOMETRY=\"$GEOMETRY\"
fi
exec $XTERM $GEOMETRY $XTERMOPTIONS -e aumix $SCHEME
exit 1
