#
# ident "@(#)preinstall.src	1.3 04/06/29 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
#

# preinstall script for SUNWutscr - Sun Ray smartcard APIs
#	Removes Sun Ray OCF Card Terminal Driver from OCF.

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

if [ -z "$PKG_INSTALL_ROOT" ]; then PKG_INSTALL_ROOT="/"; fi

set -u

BASEDIR="`pkginfo -R ${PKG_INSTALL_ROOT} -r SUNWutscr`"

OCF_BASEDIR="`pkginfo -R ${PKG_INSTALL_ROOT} -r SUNWocfr 2>/dev/null`"
OCF_BASEDIR="${OCF_BASEDIR:-/}"

SUNWUTLIB="${BASEDIR}/SUNWut/lib"

OCF_CP_FILE="${PKG_INSTALL_ROOT}${OCF_BASEDIR}/etc/smartcard/ocf.classpath"
OCF_TERMADMIN="${PKG_INSTALL_ROOT}/${SUNWUTLIB}/ocf_termadmin"
OCFSERVER="ocfserv"

CT_JAR="${SUNWUTLIB}/sunrayCTdriver.jar"
CT_JAR_BASE="`/bin/basename $CT_JAR`"

CT_MODEL="SunRayDTU"

TMP=/var/run/SUNWutscr.$$

    #
    # If the OCF classpath file isn't on the system, then that
    # probably means that OCF is not installed. This isn't an
    # error condition, so just return sucess without actually
    # doing anything.
    #
    if test ! -r $OCF_CP_FILE ; then
	exit 0
    fi

    #
    # Remove any Sun Ray Card Terminal Driver jar file path
    # from the file that OCF uses to locate it's Java classes.
    #
    egrep -vi $CT_JAR_BASE $OCF_CP_FILE >$TMP
    mv $TMP $OCF_CP_FILE

    # default to no errors in removing CT driver
    REM_ERR=0

    #
    # Only kill ocfserv if we're being installed on the same system
    # that we're running on. Note that this is a 1/2 step towards
    # supporting activation, since we have no way of removing the
    # CT driver from ocfserv's properties file after the system
    # is booted to the real root.
    # XXX fix this
    #
    if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
	#
	# Stop the ocfserver, as the classpath file of the ocfserver
	# has been updated and ocfserver needs to pick these changes.
	#
	/usr/bin/pkill -x $OCFSERVER 2>/dev/null

	# Remove any Sun Ray Card Terminal Drivers from the OCF server.
	for SR_CT in `$OCF_TERMADMIN -l 2>/dev/null`
	  do
	    if echo $SR_CT | egrep -is $CT_MODEL ; then

		$OCF_TERMADMIN -r "$SR_CT" 2>/dev/null
		if [ "$?" != "0" ] ; then
		    REM_ERR=1
		    echo "Unable to remove: [$SR_CT] from OCF"
		fi
	    fi
	  done

	#
	# Check to see if there were any errors while removing the
	# Sun Ray Card Terminal Driver from the OCF server.
	# If there is a problem removing this driver, then just punt
	# and the admin will have to do it manually.
	# XXX Should we exit with a non-zero status if we can't remove
	# XXX the driver?
	#
	if [ "$REM_ERR" != "0" ] ; then
	    echo "Unable to remove Sun Ray Card Terminal Driver from OCF"
	else
	    echo "Sun Ray Card Terminal Driver removed from OCF"
	fi

	#
	# Stop the ocfserver, as the classpath file of the ocfserver
	# has been updated and ocfserver needs to pick these changes.
	#
	/usr/bin/pkill -x $OCFSERVER 2>/dev/null
    fi

    exit 0
