#!/bin/ksh -p

#
# ident "@(#)utload.sh	1.29 04/06/07 SMI"
#
# Copyright 1999-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

UTBASE=/etc/opt/SUNWut/basedir
UTLIB=$UTBASE/lib
UTNETPIPE="$UTLIB/utnetpipe"
UTXPROP="$UTLIB/utxprop"

#
# Default values
#
DISPLAY_DIR=/var/opt/SUNWut/displays
CONTROL_IPA=0.0.0.0
CONTROL_PORT=7010

FILE=_
FLASH=false
USEFB=false
SID=""
# TIMEOUT=20

#
# Subroutines
#

function usage {
	print -u2 "Usage: ${0##*/} [-h host] [-p port] [-d display] [-S fwServer] [-f firmwareFile] [-w] [-b] [-c CID] [-m num]"
	exit 1
}

function checkFile {
	print -u2 checkFile not implemented
}

function getIPA {
	if [ $1 = "0" ]
	then
		print 0
		return
	fi
	IP=`getent hosts $1 | awk '{print $1}'`

	# If null string, check for dotted notation for an
	# interconnect address not in /etc/hosts.
	if [ -z "$IP" ]
	then
		IP=`print $1 | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'`
		if [ -z "$IP" ]
		then
			print "BAD"
			return
		fi
	fi

	# Convert dotted IP address to hex
	print $IP | awk -F. '{
		for (i = 1; i <= 4; i++) {
			if ($i == "")
				$i = 0
			printf "%02x", $i
		}
		printf "\n"
	}'
}

# Find out the current firmware version from the terminal
function queryTerminal {
	print -u2 queryTerminal is not implemented
}

# Get the session ID of the current session
function getMySid {
	$UTXPROP -s 2> /dev/null
}

# Get the session ID from the display number
function getDisplaySid {
	case "$(id)" in
		'uid=0('*) ;;
		*) print -u2 "Must be root to use -d option"
		   exit;;
	esac

	# Strip off any leading hostname, and trailing screen number as
	# a courtesy in case a $DISPLAY value is used. The display number
	# will always be interpreted as being on the CONTROL_HOST.

	D=$(print $1 | sed -e 's/.*://' -e 's/\..*//')
	sed -n 's/^SESSION=//p' $DISPLAY_DIR/$D 2>/dev/null
}

# translate a host name to its IP address
function getHostIPA {
	host=$1
	if [ "$host" = "$(hostname)" ]
	then
		print 0.0.0.0
		return
	fi
	case $host in
	localhost | 127.0.0.1 | 0.0.0.0)
		print 0.0.0.0
		return;;
	esac

	hostipa=$(ypmatch $host hosts 2>/dev/null | cut -f1)
	if [ -z "$hostipa" ]
	then
		hostipa=$(nslookup $host 2>/dev/null |
		    sed -n \
			-e '/^Server:/,/^$/d'	\
			-e 's/^Address:[ 	]*\([0-9].*\)$/\1/p')
	fi
	print $hostipa
}

#
# Main program
#

#
# Parse and validate command line options
#
while getopts :f:h:p:d:S:c:m:wb name
do
	case $name in
	b)	USEFB="true";;
	c)	CID="$OPTARG";;
	d)	DISP="$OPTARG";;
	f)	FILE="$OPTARG";;
	h)	CONTROL_HOST="$OPTARG";;
	m)	HEAD="$OPTARG";;
	p)	CONTROL_PORT="$OPTARG";;
	w)	FLASH="true";;
	S)	SRVR="$OPTARG";;
	?)	usage;;
	esac
done
shift $(expr $OPTIND - 1)

if [ $# -ne  0 ]
then
    usage
fi

if [ -z $DISP ]
then
	SID=$(getMySid)
else
	SID=$(getDisplaySid $DISP)
fi

if [ -z $SID ] 
then
	echo "Session ID cannot be determined."
	usage
fi

if [ -n "$CONTROL_HOST" ]
then
	ipa=$(getHostIPA "$CONTROL_HOST")

	if [ -n "$ipa" ]
	then
		CONTROL_IPA=$ipa
	fi
fi

if [ -n "$SRVR" ]
then
	fwIPA=$(getIPA "$SRVR")
	if [ $fwIPA = "BAD" ]
	then
		usage
		exit 1
	fi
	server="server=$fwIPA\nend"
else
	server="end"
fi

if [ -n "$CID" ]
then
	if [ -n "$HEAD" ]
	then
		print -u2 "Must specify only one of -c or -m"
	fi
	CID=$(print $CID | awk '
			/\./		{ print ; exit }
			$0 == "all"	{ print ; exit }
					{ print "IEEE802." $0 ; exit }
		')	
	server="cid=$CID\n$server"
fi

if [ -n "$HEAD" ]
then
	if [ -n "$CID" ]
	then
		print -u2 "Must specify only one of -c or -m"
	fi
	server="head=$HEAD\n$server"
fi

print "control $SID
request load
file=$FILE
flash=$FLASH
usefb=$USEFB
$server
" | $UTNETPIPE $CONTROL_IPA $CONTROL_PORT | while read line
do
	case $line in
	(ok*|end*)
		exit 0;
		;;
	(error*)
		print -u2 -- $line
		exit 1;
		;;
	(quit*)
		exit 0;
		;;
	*)
		;;
	esac
done

exit 0
