#!/bin/sh
#
#  erun: table-driven command supplier for EGTS (UNIX version)

	usage="erun [-dDvV] [-t devicetype] [-n devname] command"

# 	for the selected graphic terminal, this program sets hardware-
#	oriented environment variables that may be required by "egts"
#	(Ethernet Graphic Terminal Server) or application tasks,
#	based on information extracted from the configuration table.

#	arguments:
#	=========
# 	devicetype: 	GD (graphic display), DWR (discwriter), SCAN (scanner), PLOTTER (plotter)
# 	devicename:	see configuration table

#	default arguments:
#	=================
# 	default devicetype: 	GD
# 	default devicename:	the device name of the graphic
#		display associated with the user's terminal in
#		the configuration table
#
# 	If the devicename is "-",  then the default device name is selected.
#	If the user's terminal has no graphic display associated with
#	it, the user is prompted for a device name.

#	version control:
#	===============
#  	V1.0  KH 22 Mar 90:	initial version
#  	V1.1  KH 18 Aug 90:	PLOTTER devicetype
#  	V1.2  KH 15 Jan 91:	env vars become BG_XXX
#  	V1.3  KH 16 Mar 92:	handle config data scan error
#  	V1.4  KH  8 Dec 92:	some quotes

#	note:
#	====
#	"erun" uses the "rwho" daemon to determine physical line information
#	when terminals are connected over ethernet networks to remote hosts.
#	It is supposed that all information about other machines, required
#	by "rwho" daemons, is available.

# debug aids
# set -x

devtype=GD
devname=DEFAULT
egts=egts
flag=
options=-

if [ $# -eq 0 ]
then	echo $usage
	exit 1;
fi

while getopts dDvVPt:n: c
do
	case $c in
	d | D | v | V )	flag=on
		options=$options$c;;
	t)	devtype=`echo $OPTARG | tr '[a-z]' '[A-Z]' `
		if [ $devtype != GD -a $devtype != DWR -a $devtype != SCAN -a $devtype != PLOTTER ]
		then
			echo erun: device type must be GD \(graphic display\) or DWR \(discwriter\) or SCAN \(scanner\) or PLOTTER \(plotter\)
			exit 1
		fi;;
	n)	devname=$OPTARG;;
	P)	egts=egts\.pixie;;
	\?)	echo $usage
		exit 1;;
	esac
done
if [ $flag ]
then egts=$egts\ $options
fi
shift `expr $OPTIND - 1`

tasks=
taskct=0

for i in $*
do
	case $i in
	-C)	break;;
	*)	taskct=`expr $taskct + 1`
		tasks=$tasks$i;;
	esac
done
shift `expr $taskct + 1`

if [ $devtype = GD ]
then	descriptor=graphic\ display
fi

if [ $devtype = DWR ]
then 	descriptor=discwriter
fi

if [ $devtype = SCAN ]
then 	descriptor=scanner
fi

if [ $devtype = PLOTTER ]
then 	descriptor=plotter
fi

if [ $devname = - ]
then 	terminal=`eline`

	scanline=`escan $devtype NONE $terminal`
	echo $scanline | grep unsuccessful
	if [ $? -eq 0 ]
	then	echo Error scanning EGTS configuration data
		exit 1
	fi
	devname=`echo $scanline | cut -f1 -d" "`
fi

# exit, if existing environment has sufficient information

if [ $devname = DEFAULT ] 
then	if [ 	   ${BG_EGTS_ETH_DST:-NONE}    != NONE \
		-a ${BG_EGTS_GT_IMAGE:-NONE}   != NONE \
		-a ${BG_EGTS_GT_SCRIPT:-NONE}  != NONE ]
	then	$egts -b $BG_EGTS_GT_IMAGE -c $BG_EGTS_GT_SCRIPT -e $BG_EGTS_ETH_DST $*
		exit
	else 	devname=NONE
	fi
fi	

while [ 1 ]
do 	if  [ $devname = NONE ]
	then 	echo Which $descriptor are you using \(\? for help\) 
		devname=`line | tr '[a-z]' [A-Z]' `
	fi

	if [ $devname = \? ]
	then	echo devicename"\t"ethernet address
		ehelp $devtype
		devname=NONE
		continue
	fi

	scanline=`escan $devtype $devname norm`

	echo $scanline | grep unsuccessful
	if [ $? -eq 0 ]
	then	echo Error scanning EGTS configuration data file
		exit 1
	fi

	if [ $? -eq 0 ]
	then	break
	else	devname=NONE
	fi
done

eth_dst=`echo $scanline | nawk '{ print $2 }' `
gt_image=`echo $scanline | nawk '{ print $4 }' `
gt_script=`echo $scanline | nawk '{ print $5 }' `
gt_monitor=`echo $scanline | nawk '{ print $6 }' `
gt_point=`echo $scanline | nawk '{ print $7 }' `

$egts -b $gt_image -c $gt_script -e $eth_dst $tasks -C "$*"

#DEBUGGING
#echo $egts -b $gt_image -c $gt_script -e $eth_dst -vV $tasks -C "$*"
#$egts -b $gt_image -c $gt_script -e $eth_dst -vV $tasks -C "$*"
