#!/bin/sh -

thishost=${HOST-${HOSTNAME-`(hostname) 2> /dev/null`}}
case "$thishost" in
'')     thishost=`uname -n`;;
esac

# xtmain - start an xterm login to a main host
# Steve Kinzler, steve@kinzler.com, Jul 15
# https://kinzler.com/me/home.html#x11

name=xterm-MAIN
case "$1" in
-M)		shift;;
-m)		shift; name=xterm-main;;
esac

case "$1" in
1)		num=;;
[23L])		num="$1";;
l)		num=L;;
*)		echo "usage: $0 [ -M | -m ] [ 1 | 2 | 3 | L ]" 1>&2; exit 1;;
esac

case "$num" in
''|[23])	host=`eval "echo \\\$MAILHOST$num"`;;
*)		host=;;
esac

tit=`echo "$host" | sed 's/\..*//'`
case "$tit" in
?)		tit="${tit}kinzler";;
kinzler)	tit=vkinzler;;
esac
case "$thishost" in
"$tit")		host=localhost; XT_HOSTBG="$tit"; export XT_HOSTBG;;
esac
case "$name,$num" in
*,L)		tit='-N LOCAL';;
*MAIN,*)	tit='-N '`echo "$tit" | tr '[a-z]' '[A-Z]'`;;
*main,*)	tit="-N $tit@";;
esac

case "$name,$num" in
*main,L)	name=xterm-local; tit=;;
esac

args=
case "$thishost,$tit" in
athanor,*\ ?kinzler@)		args='-R 2223:localhost:22';;
athanor-vm,*\ ?kinzler@)	args='-R 2224:localhost:22';;
esac

# 2022-07-17 to avoid ssh, to avoid snap browser remote control bug?
#   but a good optimization anyway, and improves xtbg matching
case "$host" in localhost) host=;; esac

exec xt -name $name $tit $host $args
