#!
#
# ident "@(#)preremove	1.8 02/11/05 SMI"
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
#

# preremove script for SUNWutscr - Sun Ray smartcard APIs

# if PKG_INSTALL_ROOT is not assigned from the env then
# set it to an empty string, set -u in effect below
#
if [ -z "$PKG_INSTALL_ROOT" ]; then
	PKG_INSTALL_ROOT=""
fi

set -u

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

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

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

CT_MODEL="SunRayDTU"

TMP="/tmp/$$.SUNWutscr"

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

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

    REM_ERR=0

    # 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

    exit 0
