#!/bin/sh -

recent="$HOME/.cache/xt-recenthosts"
maxrecent=100
case "$1" in --twmmenu_recent)
	(ssh "$MAILHOST2" tail -100 \< .cache/xt-recenthosts
	 ssh "$MAILHOST3" tail -100 \< .cache/xt-recenthosts) 2> /dev/null |
		sed 's/\..*med\.umich\.edu$//; /^\./d' |
		sortc -c | tail -50 | sed 's/^[0-9]* //' | sort |
		sed 's/^.*$/	"&"	!"xt & \&"/' | align
	exit;;
esac

# hostpatt	color[	pixmap]
hostbg='?	:xtbg -n @${XT_HOSTBG-$host}'

# xt - convenience front-end for X11R5/X11R6/color xterm logins
# Steve Kinzler, steve@kinzler.com, Jan 94/Jan 02/Dec 15
# https://kinzler.com/me/home.html#x11

doit='eval exec'
cmd=${RSHCMD-rsh}
remote=; title=; host=; args=; args2=

while :
do
	case $# in
	0)	break;;
	*)	case "$1" in
		-h)	cat << EOF 1>&2
usage: $0 [ -nodo ] [ -remote ] [ -N title ]
       [ -rsh | -ssh | -telnet | -std ]
       [ xterm options ] [ host [ arguments ] ]
	-nodo	no execute, just print the command that would execute
	-remote	run the xterm on the remote host (default local)
	-N	title and iconname the xterm window with the given title
	-rsh	use rsh to login to the remote host (default $cmd)
	-ssh	use ssh to login to the remote host
	-telnet	use telnet to login to the remote host
	-std	equivalent to "-telnet -name xterm-std -N ''"
The default host is the local host.  A host beginning with a : is taken
to be a command which outputs the hostname.  Any final arguments are
passed to the remote login program.  Unless a -N option with a non-null
title or a -name xterm option is given, the xterm window is titled and
iconnamed with the hostname.  Use -N DEFAULT to use xterm's default title
and iconname.  Use -N with a null title to explicitly use the hostname
as the title and iconname.
EOF
			exit 1;;

		-nodo)		doit=echo;;
		-remote)	remote=t;;
		-N)		shift; title="$1";;
		-rsh)		cmd=rsh;;
		-ssh)		cmd=ssh;;
		-telnet)	cmd=telnet;;
		-std)		cmd=telnet; title=
				args="$args -name xterm-std";;

		-*)	case "$host" in
			'')	args="$args '$1'"
				case "$1" in
				-name)	title=DEFAULT
					shift; args="$args '$1'";;
				-b|-b[dgw]|-c[cr]|-display|-f[bgn]|-geometry)
					shift; args="$args '$1'";;
				-lf|-m[cs]|-n|-nb|-T|-t[mn]|-title|-sl|-w|-xrm)
					shift; args="$args '$1'";;
				-hc)
					shift; args="$args '$1'";;
				esac;;
			*)	args2="$args2 '$1'";;
			esac;;

		*)	case "$host" in
			'')	host="$1";;
			*)	args2="$args2 '$1'";;
			esac;;
		esac
		shift;;
	esac
done

case "$host" in
:*)	host=`echo "$host" | sed s/://`
	host=`eval "$host" | sed 1q`;;
esac

case "$host" in
'')	host=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
	case "$host" in
	'')	host=`uname -n`;;
	esac
	host=`echo "$host" | sed 's/\..*//'`
	login=-ls;;

*)	case "$cmd" in
	rsh)	if   test -f /usr/bin/remsh; then cmd=/usr/bin/remsh
		elif test -f /usr/bin/rcmd;  then cmd=/usr/bin/rcmd
		fi;;
	esac
	login="-ut -e $cmd '$host' $args2";;
esac

case "$recent" in
?*)	recenthosts=`(cat $recent; echo "$host") | tail -${maxrecent:-1000}`
	echo "$recenthosts" > $recent; recenthosts=;;
esac

args="$args"`
	echo "$hostbg" | sed '/^[ 	]*$/d' |
	while read patt color
	do
		case "$color" in
		:*)	color=\`echo "$color" | sed s/.//\`
			color=\`eval "$color"\`;;
		esac
		case "$host" in
		*$patt*)
			image=
			case "$color" in
			*'	'*)	
				image=\`echo "$color" | sed 's/.*	//'\`
				color=\`echo "$color" | sed 's/	.*//'\`;;
			esac
			case "$color" in    # -bg colors scrollbar/menus too
			?*)	echo " -xrm 'XTerm*vt100.background: $color'" \
				      "-xrm 'URxvt*background:       $color'";;
			esac
			case "$image" in
			?*)	img=\`echo "$image" | sed 's/;.*//'\`
				test -f "$img" &&
				echo " -xrm 'URxvt*backgroundPixmap: $image'";;
			esac;;
		esac
	done`

case "$title" in
DEFAULT) case "$remote" in
	 '')	$doit xterm $args $login &;;
	 *)	$doit xrsh "'$host'" xterm $args -ls;;
	 esac;;

*)	 case "$title" in
	 '')	title="$host";;
	 esac
	 case "$remote" in
	 '')	$doit xterm -n "'$title'" -title "'$title'" $args $login &;;
	 *)	$doit xrsh "'$host'" \
		      xterm -n "'$title'" -title "'$title'" $args -ls;;
	 esac;;
esac
