#!/bin/ksh -p

#
# ident "@(#)utload.sh	1.34 09/06/28 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

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

#
# Default values
#
DISPLAY_DIR=/var/opt/SUNWut/displays
CONTROL_HOST=localhost
CONTROL_PORT=7010

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

#
# Subroutines
#

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

function checkFile {
	print -u2 checkFile not implemented
}

function getIPA {
	if [ $1 = "0" ]
	then
		print 0
		return
	fi
	if [ `uname -s` = "SunOS" ]; then
		IP=`getent ipnodes $1 | awk '{print $1 ; exit}'`
	else
		IP=`getent hosts $1 | awk '{print $1 ; exit}'`
	fi

	# 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
			IP=`print $1 | sed -n '/:[0-9a-fA-F][0-9a-fA-F]*/p'`
			if [ -z "$IP" ]
			then
				print "BAD"
				return
			fi
		fi
	fi

	# Convert dotted IP address to hex
	echo "$IP" | grep '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' > /dev/null 2>&1
	if [ $? = 0 ]; then
		print $IP | awk -F. '{
			for (i = 1; i <= 4; i++) {
				if ($i == "")
					$i = 0
				printf "%02x", $i
			}
			printf "\n"
		}'
	else
		$IPADDR2HEX $IP
	fi
}

# 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
}

#
# Main program
#

#
# Parse and validate command line options
#
while getopts :f:h:p:d:S:c:m:t:wbr 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";;
	r)	TYPE="reset";;
	t)	TOKEN="$OPTARG";;
	?)	usage;;
	esac
done
shift $(expr $OPTIND - 1)

if [ $# -ne  0 ]
then
    usage
fi

if [ -n "$TOKEN" ]
then
	TF=`echo $TOKEN  | sed 's^\.^/^'`
	SID=`cat /tmp/SUNWut/config/tokens/$TF`
fi

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

if [ -z $SID ] 
then
	echo "Session ID cannot be determined."
	usage
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
type=$TYPE
file=$FILE
flash=$FLASH
usefb=$USEFB
$server
" | $UTNETPIPE $CONTROL_HOST $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
