#!/bin/sh
#
#  escan: table lookup program for EGTS (UNIX version)

  usage="escan devicetype devicename terminal"

#	version control:
#	===============
#  	V1.0  KH  5 May 90:	initial version
#  	V1.1  KH 30 May 90:	cleaned up nawk error output
#  	V1.2  KH 17 Aug 90:	PLOTTER device type
#  	V1.3  KH 19 Nov 90:	bg replaces bics
#  	V1.4  KH 29 Oct 90:	BARCO_EGTS_DATA_DIR replaces BARCO_EGTS_CONFIG
#  	V1.5  KH 14 Dec 90:	graceful exit from no datafile
#  	V1.6  KH 15 Jan 91:	env vars become BG_XXX
#  	V1.7b KH 14 Jan 92:	added CPU30, CPU37, CPU40
#  	V1.8  KH 16 Mar 92:	syntax fix; non-trivial spelling correction 
#  	V1.9  KH  3 Dec 92:	CPU40 is booted with script file bootf40rip
#  	V1.10 KH  3 Mar 92:	Name changes for egts 2.5 -- see doc.


# debug aids
# set -xv
errlogfile=/dev/null

datafile=${BG_EGTS_DATA_DIR:-${BG_DIR:-/usr/local/bg}/egts/dat}/config.dat

if [ ! -f $datafile ]
then 	echo escan: data file $datafile does not exist
	exit 1
fi

echo $* | tr '[a-z]' '[A-Z]' | nawk '
BEGIN		{ FS = " " }
NR == 1    	{ devicetype = $1; devicename = $2; terminal = $3; FS = "," }
/^\[[A-Z]+\]$/	{ running = (match($0, devicetype)? 1: 0) }
/^[^;]/     	{ if (running) {
    	  	    gsub(" ", "")
   		    if (devicename == "NONE")
		      if (devicetype ==  "GD")
		 	if (terminal != "NONE") 
			  for (n = split($6, terminals, "+"); n > 0; n--)
			    if (terminals[n] == terminal)
			      { devicename = $1; break }
		    if (devicename == $1) {
	              n = split($2, address, ":")
		      if (n > 2)
			break
		      if (n == 2) 
			eth_dev = address[1]
		      gsub("-", ":", address[n])
		      eth_dst = address[n]

		      type = $3

		      if (devicetype ==  "GD") {
			if (type == "GT800") break
			if (type == "GT820") image = "prc20eth"
			if (type == "GT920") image = "prc20eth"

			script = "booteth"
			monitor = $4
			point = $5
		      } 

		      if ((devicetype ==  "DWR") || (devicetype ==  "SCAN") || (devicetype == "PLOTTER")) {
			if (type == "PROC20") {
			  image = "p20"
			  script = "bootp20rip"
			}
			if ((type == "PROC30") || (type == "CPU30")) {
			  image = "f30"
			  script = "bootf30rip"
			}
			if (type == "CPU37") {
			  image = "f37"
			  script = "bootf37rip"
			}
			if (type == "CPU40") {
			  image = "f40"
			  script = "bootf40rip"
			}
		      }

		      found = 1
		    }
		  }
		}
END		{ FS = "\t"
		  if (found)
		    print devicename FS eth_dst FS type FS image FS script FS monitor FS point
		  else
		    exit(1)
		 }
' - $datafile 2>$errlogfile

if [ $? -ne 0 ]
then	echo escan: unsuccessful termination
	exit 1
fi
