#!/bin/ksh -p
#
# ident "@(#)utwho.sh	1.25	11/10/11 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.

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

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

UTBASE=/etc/opt/SUNWut/basedir
UTNETPIPE=$UTBASE/lib/utnetpipe
XCONFIG=/tmp/SUNWut/config/xconfig/Xconfig
IDLEDIR="/var/opt/SUNWut/idle/"
IDLEEXT=".pid"
IDLENAME="????"
TMPLATE="/tmp/utwho-map.$$.XXXXXXXX"

cmd=$0
if [ $(uname) = "Linux" ]
then
	NAWK=/usr/bin/gawk
else
	NAWK=/usr/bin/nawk
fi

connected=false
all=false
heading=false


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

cleanup() {
	[ -n "$MAP1" ] && rm -f "$MAP1"
	[ -n "$MAP2" ] && rm -f "$MAP2"
	[ -n "$MAP3" ] && rm -f "$MAP3"
	[ -n "$MAP4" ] && rm -f "$MAP4"
	[ -n "$MAP5" ] && rm -f "$MAP5"
}

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


# Get localized 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 [ $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

# Before we create the first of several temporary files, arrange to have them
# all removed when this script terminates, whatever the cause.
#
trap "cleanup" 0

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

MAP2=$(mktemp "$TMPLATE")
$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 > "$MAP2"

MAP4=$(mktemp "$TMPLATE")
join -j 1 "$MAP1" "$MAP2" | sort -k 2 > "$MAP4"

if [ $connected = "false" ]
then
	sort -n "$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
	
MAP3=$(mktemp "$TMPLATE")
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 > "$MAP3"

MAP5=$(mktemp "$TMPLATE")
join -1 2 -2 1 -o 1.1,1.2,1.3,2.2,2.3,2.4,2.5,2.6 "$MAP4" "$MAP3" \
	> "$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 }' < "$MAP5" | sort -n
