#! /bin/ksh -p
#
# Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)wcapp.ksh	1.21	02/12/18 SMI"
#
# Author: macdon
#
# first set up directories that we need,
# whether running installed package or from ws

export PATH=/bin:/usr/sbin

typeset -r PROG=${0##*/}

typeset -r HOSTNAME=`/usr/bin/hostname`
typeset -r SMS_BASEDIR=/opt
typeset -r SMS_PKGDIR=SUNWSMS/SMS1.3
typeset -r SMS_LIBPATH=${SMS_BASEDIR}/${SMS_PKGDIR}/lib
typeset -r SMS_CLASSPATH=${SMS_BASEDIR}/${SMS_PKGDIR}/classes
typeset -r SMS_MESSAGES=/var${SMS_BASEDIR}/${SMS_PKGDIR}/adm/platform/messages
typeset -r WCAPP_JAVAPKGSUBDIR=com/sun/wildcat/common
typeset -r WCAPP_CLASSFILE=${SMS_CLASSPATH}/${WCAPP_JAVAPKGSUBDIR}/ScImpl.class
typeset -r WCAPP_POLICY_FILE=${SMS_CLASSPATH}/${WCAPP_JAVAPKGSUBDIR}/server-policy
typeset -r WCAPP_SSL_FILE=${SMS_CLASSPATH}/${WCAPP_JAVAPKGSUBDIR}/socket-properties

typeset -r WCFM_BASE_DIR=/opt
typeset -r WCFM_PKG_DIR=SUNWwcfm
typeset -r WCFM_JARFILE_SHARED=${WCFM_BASE_DIR}/${WCFM_PKG_DIR}/classes/sc_shared.jar

print_usage()
{
	printf "Usage:\t%s [ -d ]\n" "${PROG}"
	print
	printf "  Description:\n"
	printf "\t- When \"-d\" is used, the C++ wcapp is used.\n"
	printf "\t- If no \"-d\" is used, the Java wcapp is used.\n"

	return
}

logit() 
{
	# $1 severity level as defined by SMS logging (ERR, NOTICE, etc.)
	# $2 name of offending wcapp subcomponent
	#$3 descriptive string (already internationalized)
	if [ -w  ${SMS_MESSAGES} ] ; then
		# SMS platform log is defined:
		# log there with the specified severity
		date_format="date +'%b %e %T %Y'"
		log_format="${HOSTNAME} wcapp: [$1]"
		echo "`eval ${date_format}` ${log_format} $2: $3" \
		    >> ${SMS_MESSAGES}
	fi
	if [ -t 2 ] ; then
		# wcapp is being run from the command line:
		# echo to standard error as well
		echo "wcapp: $2: $3" 1>&2
	fi
}

#
# 4774933 wcapp decommitment
#
# Run the placeholder instead of the real wcapp, bypass checking all the
# java stuff
#
start_CC()
{
        exec ${SMS_BASEDIR}/${SMS_PKGDIR}/lib/smsadmin/wcappholder
}




start_java()
{
	if [ ! -r ${WCAPP_CLASSFILE} ] ; then
		# wcapp top-level class file not found and readable:
		# log it and start the wcappholder
		logit ERR ${WCAPP_CLASSFILE} \
		"`gettext 'wcapp classfile not found or not readable by user, wcappholder started'`"
		start_CC
		return 0
	fi

	if [ ! -r ${WCFM_JARFILE_SHARED} ] ; then
		# required jarfile not found and readable:
		# log it and start the wcappholder
		logit ERR ${WCFM_JARFILE_SHARED} \
		"`gettext 'required jarfile not found or not readable by user, wcappholder started'`"
		start_CC
		return 0
	fi

	# begin constructing jarfile list for classpath
	typeset WCAPP_JARFILES=${WCFM_JARFILE_SHARED} 

	typeset -r WCAPP_JAVADIR=/usr/java1.2

	if [ ! -x ${WCAPP_JAVADIR}/bin/java ] ; then
		# required JVM not found and executable:
		# log it and start the wcappholder 
		logit ERR ${WCAPP_JAVADIR}/bin/java \
		"`gettext 'required Java runtime environment not found or not executable, wcappholder started'`"
		start_CC
		return 0
	fi

	# split the "java -fullversion" phrase into the release identifier 
	# (e.g. 1.2.2), the numeric part of the version (e.g., 07), 
	# and the build (which we ignore)
	fv=`${WCAPP_JAVADIR}/bin/java -fullversion 2>&1 | /usr/xpg4/bin/awk -F '[^0-9.]' \
	' { for (i=1; i< NF; i++) {if ("" !~ $i) {printf "%s ", $i}  } printf "\n" }'`

	# $a gets the release identifier
	a=`echo $fv | sed 's~ .*$~~'`

	# $b gets the version number
	b=`echo $fv | sed "s~^$a ~~;"'s~ .*$~~'`
	
	if [ $a = "1.2.2" -a $b -le 6  ] ; then
		# Java release 1.2.2 versions 0..6 are no good:
		# log as an error on the SMS platform log
		logit ERR ${a}_${b}
		"`gettext 'unsupported Java release, wcappholder started'`"
		start_CC
		return 0
	fi

	if [ "${a}" != "1.2.2" -a "${a}" != "1.4.0" ] ; then
		# Java releases other than 1.2.2 or 1.4.0 are no good:
		# log as an error on the SMS platform log
		logit ERR ${a}_${b}
		"`gettext 'unsupported Java release, wcappholder started'`"
		start_CC
		return 0
	fi

	typeset -r WCAPP_JARDIR=${WCAPP_JAVADIR}/lib/ext
	typeset -r WCAPP_JARFILE_JSSE=${WCAPP_JARDIR}/jsse.jar
	typeset -r WCAPP_JARFILE_JNET=${WCAPP_JARDIR}/jnet.jar
	typeset -r WCAPP_JARFILE_JCERT=${WCAPP_JARDIR}/jcert.jar

	if [ -r ${WCAPP_JARFILE_JSSE} -a \
	-r ${WCAPP_JARFILE_JNET} -a -r ${WCAPP_JARFILE_JCERT} ] ; then
		# all 3 JSSE jarfiles for SSL were found installed
		# as extensions to java1.2:
		# include them in the jarfile list
		WCAPP_JARFILES=${WCAPP_JARFILES}:${WCAPP_JARFILE_JSSE}
		WCAPP_JARFILES=${WCAPP_JARFILES}:${WCAPP_JARFILE_JNET}
		WCAPP_JARFILES=${WCAPP_JARFILES}:${WCAPP_JARFILE_JCERT}
	else
		# JSSE jarfiles for SSL weren't found (or weren't all found):
		# THIS IS NOT AN ERROR until ScImpl decides that
		# it is supposed to use SSL
		# therefore, continue with preparations to launch
		# log as a NOTICE and continue
		logit NOTICE \
		"${WCAPP_JARFILE_JSSE}, ${WCAPP_JARFILE_JNET}, ${WCAPP_JARFILE_JCERT}" \
		"`gettext 'optional JSSE jarfiles not all found or not \
readable by user; running without SSL support'`"
	fi

	# finally run wcapp, which takes over the process $$
	# so that ssd will be able to kill it.
	# Always define the dummy System property "wcapp" at the start
	# of the java command to permit detection by ssd and fomd
	# which look for it with the regexp command "pgrep -f wcapp"
	# cd to bin directory to put "-Dwcapp" as early as possible 
	# in command line
	cd ${WCAPP_JAVADIR}/bin
	exec ./java \
		-Dwcapp \
		-classpath ${WCAPP_JARFILES}:${SMS_CLASSPATH} \
		-DSERVERHOST=$HOSTNAME \
		-DHOST=$HOSTNAME \
		-DLOCATION=server \
		-Djava.security.policy=$WCAPP_POLICY_FILE \
		-DScImpl.ssl_file=$WCAPP_SSL_FILE\
		-Djava.library.path=$SMS_LIBPATH \
		com.sun.wildcat.common.ScImpl <&- >&- 2>&-
}

main()
{
	integer dflag=0
	typeset c

	while getopts d c; do
		case ${c} in
		d)
			if (( dflag == 0 )); then
				(( dflag += 1 ))
			else
				printf "%s: \"%s\" has already been used.\n" ${PROG} "-d" >&2
				return 1
			fi
			;;
		*)
			print_usage
			return 2
			;;
		esac
	done

	shift $(expr $OPTIND - 1)

	if [[ $# > 0 ]]; then
		print_usage
		return 2
	fi

	if (( dflag == 0 )); then
		start_java
	else
		start_CC
	fi

	return $?
}

main $*

exit 0
