#!/bin/ksh
#
# @(#)utsessionkill.sh	1.7   09/05/02
#
# Product:      Sun Ray server 2.0
#
# Purpose:      Wrapper script for 'utuser -k' option
#
#

#
# This script is a wrapper script called by
# 'utuser -k <-xdisplay|-tokenid> <token_id|display_value>'
# The function of this wrapper is to take the supplied token id,
# find from it the tokenid in the correct format, as expected by
# the utdtsession script, then call that script with the correct 
# value passed in as a parameter.

DEVICE_PKG_LOC="`/bin/pkginfo -r SUNWuta`/SUNWut"
UTDTSESSION=${DEVICE_PKG_LOC}/lib/utdtsession
ITOKENDIR=/var/opt/SUNWut/itokens
DISPLAYSDIR=/var/opt/SUNWut/displays
CHOICE=$1
INPUTARG=$2
SUBCOMMAND=

if (( $# != 2 ))
then
	exit 1
fi

#
# check there is an active session for this tokenid
#

if [[ ${CHOICE} == "token" ]]
then
	if [[ ! -f ${ITOKENDIR}/${INPUTARG} ]]
	then
		exit 1
	else
        	FILENAME=${ITOKENDIR}/${INPUTARG}
	fi

	# get long token value
	LONGTOKENID=$(nawk -F[=] '$1 == "TOKEN" {print $2}' ${FILENAME})

	if [[ -z ${LONGTOKENID} ]]
	then
		exit 1
	fi

	# get subcommand
	SUBCOMMAND="-t ${LONGTOKENID}"

elif [[ ${CHOICE} == "display" ]]
then

	#
	# need to trim the display variable
	# if it is decimalised
	#
	FILTEREDINPUTARG=$(print ${INPUTARG%\.*})

	# get subcommand
	SUBCOMMAND="-n ${FILTEREDINPUTARG}"
else
	exit 1
fi

retval=$(exec ${UTDTSESSION} ${SUBCOMMAND} delete)

return ${retval}
