#!/bin/sh
#
#*******************************************************************************
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#

# Sanitize environment
PATH=/usr/bin:/bin
export PATH

MODULE=kioskrestart
KIOSKUSERADM=/opt/SUNWkio/bin/kioskuseradm

show_usage()
{
cat 1>&2 <<!    
Usage: 
    kioskrestart
    kioskrestart -a
    kioskrestart -d <display> ...
    kioskrestart -u <username> ...
    kioskrestart -h
!
}

usage()
{
 show_usage   
 exit ${1:-1}
}

help()
{
cat 1>&2 <<!    
kioskrestart - Terminate kiosk sessions so they restart cleanly.    
!
  show_usage  
  cat 1>&2 <<!    
 
Without an option, kioskrestart restarts the current kiosk session.

Options:
  -a	Restart all kiosk sessions.
  -d	Restart kiosk sessions on specified displays.
  -u	Restart kiosk sessions for specified user accounts.
  -h	Print this help message.
!

 exit 0
}

#
## Resolve displays to user names
#
resolveSessions()
{
    theSessionMap="`$KIOSKUSERADM status -pv`"
    if [ -z "$theSessionMap" ] ; then
	return 0
    fi	    
    
    for theDisplay in "$@"
    do
	theNormDisplay=`normalizeDisplay "$theDisplay"`
	if [ -z "$theNormDisplay" ] ; then
	    echo 1>&2 "Invalid display name: $theDisplay"
	    logDebug -m $MODULE -l "Ignoring invalid display name: $theDisplay"
	    continue
	fi    	
    	echo "$theSessionMap" |
        	awk '{ if (D==$2) { print $1 } }' "D=$theNormDisplay"
    done		
    return 0
}

#
## List all current kiosk sessions
#
listAllSessions()
{
    $KIOSKUSERADM status -pv | awk '{ print $1 }'
}

#
## Restart a kiosk session
#
terminateSessionFor()
{
    inUser=$1
    launchCriticalAppForUser $MODULE "$inUser" /bin/true
}


#
## main
#

#
## Load utilities and setup basic Kiosk Environment
#
theUtilsFile=/opt/SUNWkio/lib/utils.sh
if [ ! -r $theUtilsFile ] ; then
 echo 1>&2 "Error: can't read Kiosk utils file '$theUtilsFile'"
 exit 1
else
 . $theUtilsFile
if [ $? -ne 0 ] ; then
  echo 1>&2 "Error: failed to load Kiosk utils file '$theUtilsFile'"
  exit 1
 fi
fi

#
## parse args
#

theSelection="c" # default: current session

while getopts "aduh" theOption ; do
 case $theOption in
  a) theSelection="a" ;;
  d) theSelection="d" ;;
  u) theSelection="u" ;;
  h) help ;;
  \?) usage ;;
 esac
done

shift `expr $OPTIND - 1`

case $theSelection in 
c) 	# current session only
	[ $# -eq 0 ] || usage
	theLogname=`logname`
	theUsers="${theLogname:=$USER}" 
	logInfo -m $MODULE -l "Terminating current kiosk session for user $theLogname."
	;;
a)	# all sessions
	[ $# -eq 0 ] || usage
	theUsers="`listAllSessions`"
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	logInfo -m $MODULE -l "Terminating all kiosk sessions."
	;;
u)
	[ $# -eq 0 ] && usage
	theUsers="$*"
	logInfo -m $MODULE -l "Terminating kiosk sessions for users: $theUsers"
	;;
d)
	[ $# -eq 0 ] && usage
	logInfo -m $MODULE -l "Request to terminate kiosk sessions for displays: $*"
	theUsers="`resolveSessions $*`"
	if [ $? -ne 0 ] ; then
		exit 1
	fi
	logInfo -m $MODULE -l "Terminating kiosk sessions for users: $theUsers"
	;;
esac

theRC=0
for user in $theUsers
do
	terminateSessionFor $user
	if [ $? -ne 0 ] ; then
		echo 1>&2 "$MODULE: error: could not terminate session for user $user"
		logError -m $MODULE -l "Could not terminate session for user $user"
		theRC=2
	fi
done
exit $theRC
