#!/bin/ksh -p
#
# ident "@(#)utauthd.sh	1.36 05/06/27 SMI"
#
# Copyright 1998-2005 Sun Microsystems, Inc.  All rights reserved.
#

function usage {
	print -u2 "$prog: unknown option $OPTARG"
	print -u2 "Usage: $prog [-b|-e] [-s signal] [-n fds]"
	print -u2 "    -e # End execution of authentication manager"
	print -u2 "    -s # Signal to send to utauthd"
	print -u2 "    -b # Begin execution of authentication manager (default)"
	print -u2 "    -n # Number of file descriptors to make available"
	exit 1
}

function checkJavaVersion {
#
# Returns 0, if java version is greater than 1.4.0
# otherwise returns 1
#
	PRODVERS=$(/etc/opt/SUNWut/basedir/lib/utprodinfo -p SUNWuto PRODVERS)
	. $SUNWUTLIB/support_lib/util_lib
	if [ ! -x $JAVA ]
	then
		print -u2 "Cannot find $JAVA"
		exit 1
	fi

	JAVA_VERSION=$($JAVA -version 2>&1 | \
		sed -n '1s/java version "\([0-9.]*\).*"/\1/p')
	if [[ -z "$JAVA_VERSION" ]]; then
		print -u2 "Unable to determine the Java version installed on this server."
		exit 1
	fi
	CompareVersion ${JAVA_VERSION} "1.4"
	if [[ $? -eq 2 ]]; then
		# installed Java version is older than 1.4
    		print -u2 "Supported java version not found in $JAVA_HOME. Please refer to the SRSS${PRODVERS} release guide for the supported java version(s)."
		exit 1
	fi
	return 0
}

################################################################
#
# MAIN
#
################################################################

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

cd $ROOTDIR
SUNWUT=$(/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuto)/SUNWut
SUNWUTLIB=$SUNWUT/lib

ETCDIR=/etc/opt/SUNWut
JAVA_HOME=$ETCDIR/jre
JAVA=$JAVA_HOME/bin/java

CLASSPATH=$JAVA_HOME/lib/:$JAVA_HOME/jre/lib/rt.jar
CLASSPATH=$SUNWUTLIB/utauthd.jar:$CLASSPATH
CLASSPATH=$SUNWUTLIB/sdk.jar:$CLASSPATH
CLASSPATH=$SUNWUTLIB/protocol.jar:$CLASSPATH
CLASSPATH=$(print $SUNWUTLIB/modules/AuthModule*.jar | tr ' ' ':'):$CLASSPATH
if [ -f "$SUNWUTLIB/admin.jar" ]
then
	CLASSPATH=$SUNWUTLIB/admin.jar:$CLASSPATH
fi
export CLASSPATH

export LD_LIBRARY_PATH=$SUNWUTLIB

action=""
signal=TERM
#maxfds=$(ulimit -n)
maxfds=9000

prog=${0##*/}
while getopts bdens c
do
	case $c in
	b)	# Begin
		[[ -n "$action" ]] && usage || action="start"
		;;
	e)	# End
		[[ -n "$action" ]] && usage || action="stop"
		;;
	s)	# Signal
		signal=$OPTARG
		;;
	n)	# Max number of file descriptors
		maxfds=$OPTARG
		;;
	\?)
		usage;
		;;
	esac
done

[[ -z "$action" ]] && action=start

if [[ $action == start ]]
then
	checkJavaVersion	# this routine will exit if incorrect Java is installed
	ulimit -n $maxfds	# Default number of file descriptors is usually 64

	CURR_POLICY=$($SUNWUTLIB/utglpolicy 2>/dev/null)

	if [[ $? -eq 0 ]]
	then
		$SUNWUTLIB/utgenpolicy $CURR_POLICY
	fi

	exec env -i	\
		PATH="$PATH"	\
		CLASSPATH="$CLASSPATH:$LD_LIBRARY_PATH"	\
		LD_LIBRARY_PATH="$LD_LIBRARY_PATH"	\
		JAVA_HOME=$JAVA_HOME			\
	         $JAVA auth.utauthd.utauthd 
else
	ps -eo pid,args |
	    grep "auth.utauthd.utauthd" |
	    fgrep -v grep |
	    while read pid args
	do
		kill -$signal $pid
	done
fi
