:
##########################################################################
# Title      :	workon2 - create xterms to work on two specified hosts
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1996-06-20
# Requires   :	isxwindows
# Category   :	Desktop
# SCCS-Id.   :	@(#) workon2	1.13 06/03/03
##########################################################################
# Note
#    o	It is not possible to log into another account if the
#	desired host is the local host
#    o	The $XTERM command needs to support the options "-g $geometry",
#	"-t $title", and "-e $exec"
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.13'

: ${XTERM:=xterm}
: ${XTERMOPTS=}

# Get the current user name.
# Input example: "uid=1064(heiner) gid=1(other)"; output: "heiner"
: ${USER:=`expr "\`LANG=C id\`" : 'uid=[0-9]*(\([^\)]*\)).*`}

# We need a way to ping exactly one time (Linux "ping" needs -c option)
if [ -z "$PINGONCE" ]
then
    case `uname` in
        SunOS)  PINGONCE=ping;;
        Linux)  PINGONCE="ping -c 1";;
        *)      PINGONCE="ping -c 1";;  # Default
    esac
fi

#           | XOFF
#       +---|-------------------+
#       |   |-----------PX      |
# YOFF -----1111111     3333333 |
#       |   1111111     3333333 |
#       |                       |
#       |   2222222     4444444 |
#       |   2222222     4444444 |
#       |                       |
#       +-----------------------+

# The offsets may be used to provide a fixed left or top margin for
# all windows

XOFF=153
YOFF=0

# These offsets are used for the right windows (the do not include XOFF/YOFF)

PX=507					# Offset from the left margin
PY=523					# Offset from the top margin

# Two window sizes

DX1=80;	DY1=37				# Size of wins 1 and 3
DX2=80; DY2=30				# Size of wins 2 and 4

X=`expr ${XOFF:=0} + ${PX:=0}`
Y=`expr ${YOFF:=0} + ${PY:=0}`

usage () {
    echo >&2 "$PN - work on two remote hosts, $VER (hs 06/96)
usage: $PN [-c] [-l loginname] [[-L] host1] [-l loginname] [[-R] host2]
    -c:  check if host is alive (using ping(1))

A host name can have the format user@host, a shorthand for
'-l user host'."
    exit 1
}

msg () {
    for i
    do echo "$PN: $i" >&2
    done
}

fatal () { msg "$@"; exit 1; }

isxwindows || fatal "use only with X Windows"

PingHost=false
while [ $# -gt 0 ]
do
    case "$1" in
    	-c)	PingHost=true;;
	-l)	User="$2"; shift;;
	--)	shift; break;;
	-h)	usage;;
	-L)	LeftHost="$2"; shift
		LeftUser="${User:-$USER}";;
	-R)	RightHost="$2"; shift
		RightUser="${User:-$USER}";;
	-*)	usage;;
	*)	break;				# first host name
    esac
    shift
done

MyHost=`uname -n`

[ $# -lt 1 ] && set -- "$MyHost"

for host
do
    if [ -z "$LeftHost" ]
    then
	LeftHost="$host"
	LeftUser="${User:-$USER}"
    elif [ -z "$RightHost" ]
    then
	RightHost="$host"
	RightUser="${User:-$USER}"
    else
    	fatal "ERROR: only one or two host names allowed"
    fi
done

[ -z "$LeftHost$RightHost" ] && usage

if [ -z "$SSH_AGENT_PID" ]
then Rcmd=rlogin
else Rcmd=ssh
fi

[ "$PingHost" = false ] && PINGONCE=:	# disable command

# This new Sun Enterprise 450 server is too fast!
sleep 2

# Start xterms for left host
if [ -n "$LeftHost" ]
then
    case "$LeftHost" in
    	*@*)	LeftUser=`echo "$LeftHost" | cut -d@ -f1`
		LeftHost=`echo "$LeftHost" | cut -d@ -f2`;;
    esac

    if [ X"$LeftHost" = X"$MyHost" -a X"$LeftUser" = X"$USER" ]
    then
	eval $XTERM $XTERMOPTS -g ${DX1}x${DY1}+${XOFF}+${YOFF} \
		-T "$LeftUser@$LeftHost"&
	eval $XTERM $XTERMOPTS -g ${DX1}x${DY2}+${XOFF}+${Y}	\
		-T "$LeftUser@$LeftHost"&
    else
	[ -n "$LeftHost" ] && { $PINGONCE "$LeftHost" || exit; }
	eval $XTERM $XTERMOPTS -g ${DX1}x${DY1}+${XOFF}+${YOFF}	\
		-T "$LeftUser@$LeftHost" \
		-e $Rcmd ${LeftUser:+"-l $LeftUser"} "$LeftHost" &
	eval $XTERM $XTERMOPTS -g ${DX2}x${DY2}+${XOFF}+${Y}	\
		-T "$LeftUser@$LeftHost" \
		-e $Rcmd ${LeftUser:+"-l $LeftUser"} "$LeftHost" &
    fi
fi &

# Start xterms for right host
if [ -n "$RightHost" ]
then
    case "$RightHost" in
    	*@*)	RightUser=`echo "$RightHost" | cut -d@ -f1`
		RightHost=`echo "$RightHost" | cut -d@ -f2`;;
    esac

    if [ X"$RightHost" = X"$MyHost" -a X"$RightUser" = X"$USER" ]
    then
	eval $XTERM $XTERMOPTS -g ${DX1}x${DY1}+${X}+${YOFF}	\
		-T "$RightUser@$RightHost" &
	eval $XTERM $XTERMOPTS -g ${DX2}x${DY2}+${X}+${Y}		\
		-T "$RightUser@$RightHost" &
    else
	[ -n "$RightHost" ] && { $PINGONCE "$RightHost" || exit; }
	eval $XTERM $XTERMOPTS -g ${DX1}x${DY1}+${X}+${YOFF}	\
		-T "$RightUser@$RightHost" \
		-e $Rcmd ${RightUser:+"-l $RightUser"} "$RightHost" &
	eval $XTERM $XTERMOPTS -g ${DX2}x${DY2}+${X}+${Y} \
		-T "$RightUser@$RightHost" \
		-e $Rcmd ${RightUser:+"-l $RightUser"} "$RightHost" &
    fi
fi &
wait
exit 0
