#!/bin/sh
# $XConsortium: xon.sh,v 1.8 92/08/17 13:57:21 rws Exp $
# start up xterm (or any other X command) on the specified host
# Usage: xon host [arguments] [command]
#
# This is modified for BOOTP/DHCP clients by Taiji Y.(taiji@aihara.co.jp).
#
#  The environment variable, $DISPLAY will be set to IP address in place
# of hostname, because it is bothersome to check up IP address of
# BOOTP/DHCP-like clients when we use X clients at another host.  Please
# check security and ~/.rhosts in another host before using this xon.
#
case $# in
0)
	echo "Usage: $0 <hostname> [-user user] [-name window-name] [-debug]"
	echo "[-screen screen-number] [command ...]"
	exit 1
	;;
esac
target=$1
shift
label=$target
resource=xterm-$label
if [ -f /usr/bin/remsh ]; then
    rsh=/usr/bin/remsh
elif [ -f /usr/bin/rcmd ]; then
    rsh=/usr/bin/rcmd
else
    rsh=rsh
fi
rcmd="$rsh $target -n"
case $DISPLAY in
unix:*)
	DISPLAY=`echo $DISPLAY | sed 's/unix//'`
	;;
esac
case $DISPLAY in
:*)
	fullname=`hostname`
	hostname=`echo $fullname | sed 's/\..*$//'`
	if [ $hostname = $target -o $fullname = $target ]; then
		DISPLAY=$DISPLAY
		rcmd="sh -c"
	else
		# begin ip searching for BOOTP/DHCP clients
		# written by taiji@aihara.co.jp at Jan 29 1997
		ipaddress_list=`/sbin/ifconfig|grep "inet addr"|grep -v "inet addr:127.0.0.1"|sed -e "s/inet addr:\(\([0-9]\{1,3\}\.*\)\{4\}\).*/\1/"`
		for ipaddress in $ipaddress_list; do
			fullname=$ipaddress
			# if [ $ipaddress is correct ]; then
			break;
			# fi
		done
		# end ip searching
		DISPLAY=$fullname$DISPLAY
	fi
	;;
esac
username=
xauth=
case x$XUSERFILESEARCHPATH in
x)
	xpath='HOME=${HOME-`pwd`} '
	;;
*)
	xpath='HOME=${HOME-`pwd`} XUSERFILESEARCHPATH=${XUSERFILESEARCHPATH-"'"$XUSERFILESEARCHPATH"'"} '
	;;
esac
redirect=" < /dev/null > /dev/null 2>&1 &"
command=
ls=-ls
continue=:
while $continue; do
	case $1 in
	-user)
		shift
		username="-l $1"
		label="$target $1"
		rcmd="$rsh $target $username -n"
		shift
		case x$XAUTHORITY in
		x)
			XAUTHORITY="$HOME/.Xauthority"
			;;
		esac
		case x$XUSERFILESEARCHPATH in
		x)
			;;
		*)
			xpath="XUSERFILESEARCHPATH=$XUSERFILESEARCHPATH "
			;;
		esac
		;;
	-access)
		shift
		xhost +$target
		;;
	-name)
		shift
		label="$1"
		resource="$1"
		shift
		;;
	-nols)
		shift
		ls=
		;;
	-debug)
		shift
		redirect=
		;;
	-screen)
		shift
		DISPLAY=`echo $DISPLAY | sed 's/:\\([0-9][0-9]*\\)\\.[0-9]/:\1/'`.$1
		shift
		;;
	*)
		continue=false
		;;
	esac
done
case x$XAUTHORITY in
x)
	;;
x*)
	xauth="XAUTHORITY=$XAUTHORITY "
	;;
esac
vars="$xpath$xauth"DISPLAY="$DISPLAY"
case $# in
0)
	$rcmd 'sh -c '"'$vars"' xterm '$ls' -name "'"$resource"'" -T "'"$label"'" -n "'"$label"'" '"$redirect'"
	;;
*)
	$rcmd 'sh -c '"'$vars"' '"$*$redirect'"
	;;
esac
