#!/bin/ksh
#
# ident "@(#)utsessionkill.sh	1.10 04/01/16 SMI"
#
# Copyright 2000,2002,2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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

BASEDIR=/etc/opt/SUNWut/basedir
SUNWUT=`${BASEDIR}/lib/utprodinfo -r SUNWuto 2>/dev/null`/SUNWut
SUNWUTLIB=$SUNWUT/lib
UTDTSESSION=${SUNWUTLIB}/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=$(awk -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}
