#!/bin/ksh -p

#
# ident "@(#)utswitch.sh	1.29 05/04/28 SMI"
#
# Copyright 1999-2004,2005 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"
DPYDIR="/var/opt/SUNWut/displays"
SESSDIR="/var/opt/SUNWut/session_proc"

#
# Default values
#
CONTROL_IPA=0.0.0.0
CONTROL_PORT=7010

SID=""

#
# Subroutines
#

function usage {
	print -u2 "Usage: ${0##*/} {-l | -t | -h host} [-p port] [-r] [-k token]"
	exit 1
}

# Get the session ID of the current session
function getSid {
	if [ -n "$SUN_SUNRAY_TOKEN" ]
	then
		DPY=${DISPLAY#*:}
		DPY=${DPY%.*}
		if [ -r "$DPYDIR/$DPY" ]
		then
			sed -n '/^SESSION=/s///p' $DPYDIR/$DPY
		else
			$UTXPROP -s 2> /dev/null
		fi
	fi
}

# Get a host IP address
function getHostIPA {
	IP=`getent hosts $1 2>/dev/null | awk '{print $1 ; exit}' `
	if [ -z "$IP" ]
	then
		IP=`print $1 | sed -n '/^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$/p'`
	fi
	print $IP
}

#
# Main program
#

NEWHOST=
LIST="false"
TOKEN="false"
REMOTE="false"
TOK=
PORT=

#
# Parse and validate command line options
#
while getopts lh:k:s:trp: name
do
	case $name in
	h)	NEWHOST="$OPTARG";;
	s)	printf -u2 "Error: -s removed for security reasons"
		usage;;
	t)	TOKEN=true;;
	l)	LIST=true;;
	k)	TOK="$OPTARG";;
	r)	REMOTE=true;;
	p)	PORT="$OPTARG";;
	?)	usage;;
	esac
done
shift $(expr $OPTIND - 1)

# no other arguments accepted
if [ ! -z $1 ]
then
	print -u2 "Error: arguments $* not recognized"
	usage
fi

if [ $LIST = "true" -a $TOKEN = "true" ]
then
	print -u2 "Error: only one of -t and -l may be specified"
	usage
fi

if [ $LIST = "false" -a $TOKEN = "false" -a -z "$NEWHOST" ]
then
	print -u2 "Error: one of -h, -t or -l must be specified"
	usage
fi

if [ $REMOTE = "true" -a -z "$NEWHOST" ]
then
	print -u2 "Error: -r must be specified with -h"
	usage
fi

SID=$(getSid)
SIDHOST=`echo $SID | sed -n 's/^\([^:]*\):.*/\1/p'`
if [ -z "$SID" -a "$LIST" = "false" -a -z "$TOK" ]
then
    print -u2 \
	"Error: cannot switch in non-Sun Ray login session"
    exit 1
fi

if [ "$SIDHOST" != `hostname` ]
then
    CONTROL_IPA=$(getHostIPA $SIDHOST)
    if [ -z $CONTROL_IPA ]
    then
	print -u2 "Error: cannot resolve hostname $SIDHOST"
	exit 1
    fi
fi

if [ $LIST = "true"  ]
then
	if [ -z "$TOK" ]
	then
		command="sessions $SID"
	else
		command="tstatus $TOK $SID"
	fi
	print "$command
end" | $UTNETPIPE $CONTROL_IPA $CONTROL_PORT
	exit 0
fi

if [ $TOKEN = "true" ]
then
	command="authtoken=$TOK"
else
	NEWIPA=$(getHostIPA $NEWHOST)

	if [ -z $NEWIPA ]
	then
		print -u2 "Error: $NEWHOST not a valid host"
		exit 1
	fi
	command="authipa=$NEWHOST"

	if [ -n "$PORT" ]
	then
		command="$command
authport=$PORT"
	fi

	#set subcause & amgh flag
	command="$command
doamgh=false
subcause=utswitch"

 	if [ $REMOTE = "true" ]
	then
		command="$command
forceInsert=true"
	fi
fi

print "control $SID
request redirect
$command
end" | $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
