#!/bin/ksh -p

# ident "@(#)utwho.sh	1.23 11/02/15 Oracle"
# Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.


# This script uses internal Sun Ray interfaces that are private and not
# guaranteed to remain stable. As such, works derived from this script
# are discouraged.

#
# Solaris 10 Trusted Extension guard
#
ORIGIN=`/usr/bin/dirname $0`
UTIL_LIB=${ORIGIN:-/opt/SUNWut/lib}/../lib/support_lib/util_lib
. $UTIL_LIB
FailExecInLocalZoneOnTx 
#
# Solaris 10 Trusted Extension guard 


PATH=/bin:/usr/bin
UTBASE=/etc/opt/SUNWut/basedir
UTNETPIPE=$UTBASE/lib/utnetpipe
XCONFIG=/tmp/SUNWut/config/xconfig/Xconfig
IDLEDIR="/var/opt/SUNWut/idle/"
IDLEEXT=".pid"
IDLENAME="????"
TMP=/tmp
cmd=$0
if [ `uname` = "Linux" ]
then
	NAWK=/usr/bin/gawk
else
	NAWK=/usr/bin/nawk
fi

connected=false
all=false
heading=false

# Text strings
disp_text=`gettext "DISP"`
token_text=`gettext "Token"`
user_text=`gettext "User"`
ip_text=`gettext "IP Address"`
modelcid_text=`gettext "Model.CID"`

if [ ! -d /tmp/SUNWut ]
then
	print -u2 `gettext "utwho must be run on a Sun Ray server"`
	exit 1
fi

trap "rm -f $TMP/map1.$$ $TMP/map2.$$ $TMP/map3.$$ $TMP/map4.$$ $TMP/map5.$$" 0 1 2 15

function Usage
{
	print -u2 `gettext "Usage: "`"$cmd"
	print -u2 "	" `gettext "list Sun Ray sessions"`
	print -u2 `gettext "Usage: "`"$cmd [-caH]"
	print -u2 "	-H	" \
		`gettext "Output column headings above the regular output"`
	print -u2 "	-c	" \
		`gettext "list only connected sessions"`
	print -u2 "	-a	" \
		`gettext "include idle (not logged in) sessions"`
	exit 1
}

while getopts :caH ARGNAME
do
	case $ARGNAME in
	   c)	connected=true;;	
	   a)	all=true;;
	   H)	heading=true;;
	   *)	print -u2 `gettext "Invalid argument"` "$*"
		Usage;;
	esac
done

sessfiles=`find /tmp/SUNWut/session_proc -type f -print`

if [ -z "$sessfiles" ]
then
	exit 1
fi

if [ $heading = "true" ]
then
	if [ $connected = "false" ]
	then
		printf "%4s    %-32s %-8s\n" \
			"$disp_text" "$token_text" "$user_text"
	else
		printf " %4s     %-28s %-8s %-15s %-s\n" \
			"$disp_text" "$token_text" "$user_text" "$ip_text" \
			"$modelcid_text"
	fi
fi

#export LC_ALL=C

$NAWK '/SUN_SUNRAY_TOKEN/ {
	sub(".*\\.\\*_", "", $1)
	sub("\\..*:", "", $1)
	sub("SUN_SUNRAY_TOKEN=", "", $2)
	sub("JavaBadge-nonPers", "JavaBadgeNP", $2)
	print $1, $2
	}' $XCONFIG | sort >$TMP/map1.$$

$NAWK -v idledir=$IDLEDIR -v idleext=$IDLEEXT -v idlename=$IDLENAME '
	BEGIN {
		name = idlename
		disp = ""
	}

	FILENAME != fn {
		if (disp != "") {
			idlefile=idledir disp idleext
			# Does the idle file exist? If so, null out name
			if ((getline foo < idlefile) != -1) {
				name = idlename
				close(idlefile)
			}
			print disp, name
		}

		fn = FILENAME
		disp = FILENAME
		name = idlename
		sub(".*/", "", disp)
	}

	/uid=/ {
		user = substr($0, index($0, "=") + 1)
		cmd = "getent passwd " user
		cmd | getline name
		close(cmd)
		sub(":.*", "", name)
	}

	END {
		if (disp != "") {
			idlefile=idledir disp idleext
			# Does the idle file exist? If so, null out name
			if ((getline foo < idlefile) != -1) {
				name = idlename
				close(idlefile)
			}
			print disp, name
		}
	}' $sessfiles | sort >$TMP/map2.$$

join -j 1 $TMP/map1.$$ $TMP/map2.$$ | sort -k 2 > $TMP/map4.$$

if [ $connected = "false" ]
then
	sort -n $TMP/map4.$$ | \
		$NAWK -v all=$all -v idlename=$IDLENAME '
			{
				name = $3
				if ($2 ~ "sessionrefused") {
					sub("\\..*", "", $2)
					name = idlename
				}
				if (all == "false" && name == idlename)
					next
				printf "%3d %-36s %-8s\n", $1, $2, name
			}'
	exit
fi
	
print status | $UTNETPIPE 0.0.0.0 7010 | \
$NAWK '
	BEGIN {
		mhi = 0
	}

	/terminalId=/ {
		tid = substr($0, index($0, "=") + 1)
		sub("^Corona", "", tid)
		sub("^SunRay", "", tid)
		sub("\\-...\\.", ".", tid)
	}

	/tokenName=/ {
		token = substr($0, index($0, "=") + 1)
		sub("JavaBadge-nonPers", "JavaBadgeNP", token)
	}

	/terminalIPA=/ {
		ipa = substr($0, index($0, "=") + 1)
	}

	/MultiheadTerminalIndex=/ {
		mhi = substr($0, index($0, "=") + 1)
	}

	/ServiceInitCommand=/ {
		service = substr($0, index($0, "=") + 1)
		sub(" add", "", service)
		if (service ~ "-c") {
			stype = "special"
			sub(".*-c ", "", service)
		} else {
			stype = "normal"
			service = "-"
		}
	}

	/^end$/ {
		print token, ipa, tid, mhi, stype, service
		mhi = 0
	}
	' | sort >$TMP/map3.$$

join -1 2 -2 1 -o 1.1,1.2,1.3,2.2,2.3,2.4,2.5,2.6 $TMP/map4.$$ $TMP/map3.$$ \
	> $TMP/map5.$$

$NAWK -v all=$all -v host=`hostname` -v idlename=$IDLENAME '{
	if ($7 == "special") {
		name = idlename
		sub("^sessionrefused.*", "sessionrefused", $2)
	} else {
		name = $3
	}
	if (all == "false" && name == idlename)
		next
	printf "%3d.%d %-29s %-8s %-15s %-s\n", $1, $6, \
			$2, name, $4, $5 }' <$TMP/map5.$$ | sort -n
