
##########################################################################
# Title      :	workon - create xterms to work on a specified host
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1995-10-11
# Requires   :	isxwindows
# Category   :	Desktop
# SCCS-Id.   :	@(#) workon	1.13 09/09/04
##########################################################################
# Description
#    o	The command $XTERM needs ot understand the option "-g" and "-T",
#	e.g. "-g 80x25 -T title"
##########################################################################

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

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

# 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

#########################################################################
# CONFIGURATION SECTION
#
#           | XOFF
#       +---|-------------------+
#       |   |-----------PX      |
# YOFF -----1111111     2222222 |
#       |   1111111     2222222 |
#       |   1111111     2222222 |
#       |   1111111     2222222 |
#       |                       |
#       +-----------------------+

# 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=503					# Offset from the left margin
PY=514					# Offset from the top margin

# Two window sizes

DX=80;	DY=70				# Size of wins 1 and 2

# END CONFIGURATION SECTION
#########################################################################

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

usage () {
    echo >&2 "$PN - work on remote host, $VER (stv '95)
usage: $PN [-c] [-l loginname] [host ...]
    or $PN [-c] login@host [login@host ...]
   -c:  check if host is alive (using ping(1))"
    exit 1
}

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

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

PingHost=false
# Check arguments before setting them
getopt chl: "$@" > /dev/null 2>&1	|| usage
set -- `getopt hl: "$@"`
while [ $# -gt 0 ]
do
    case "$1" in
    	-c)	PingHost=true;;
	-l)	User="$2"; shift;;
	--)	shift; break;;
	-h)	usage;;
	-*)	usage;;
	*)	break;;			# first file name
    esac
    shift
done

isxwindows || fatal "use only with X Windows"

CurHost=`uname -n`
: ${User:=$USER}

[ $# -lt 1 ] && set -- $CurHost

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

# Give the user time to change to another virtual desktop
sleep 3

# For each host there will be two windows. We handle the following cases:
#  (1) local window
#  (2) remote window
# both cases with (a) current or (b) different user.
# We set the variables
#    o	"cmd" to the command to be run in the window (none, "login", or
#       "$Rcmd")
#    o	"geom_left", "geom_right" to the geometry for the left and right
#       window, respectively

for Host
do
    case "$Host" in
    	*@*)
	    User=`echo "$Host" | cut -d@ -f1`
	    Host=`echo "$Host" | cut -d@ -f2`
	    ;;
    esac

    OPTS="$XTERMOPTS -T ${User:+$User@}$Host"

    if [ "$Host" = "$CurHost" ]
    then				# Local work
	if [ "$User" = "${USER:-INVALID}" ]
	then				# (1a) Default user - no login
	    cmd=
	    geom_left="${DX}x${DY}+${XOFF}+${YOFF}"
	    geom_right="${DX}x${DY}+${X}+${YOFF}"
	else				# (1b) User specified - login
	    cmd="$Rcmd ${User:+-l $User} \"$Host\""
	    geom_left="${DX}x${DY}+${XOFF}+${YOFF}"
	    geom_right="${DX}x${DY}+${X}+${YOFF}"
	fi
    else				# Remote login
	if [ $PingHost = true ]
	then $PINGONCE "$Host" || continue
	fi
	cmd="$Rcmd ${User:+-l $User} \"$Host\""
	geom_left="${DX}x${DY}+${XOFF}+${YOFF}"
	geom_right="${DX}x${DY}+${X}+${YOFF}"
    fi

    if [ -n "$cmd" ]
    then
	eval $XTERM $OPTS -g $geom_left  '-e "$cmd"' &
	eval $XTERM $OPTS -g $geom_right '-e "$cmd"' &
    else
	eval $XTERM $OPTS -g $geom_left &
	eval $XTERM $OPTS -g $geom_right &
    fi

done

wait
