#!/bin/sh
#
# postinstall script for Cisco Network Registrar (nwreg2)
#
# Copyright (C) 2002-2007 by Cisco Systems, Inc.  This program contains
# proprietary and confidential information.  All rights reserved except as
# may be permitted by prior written consent.
#
#

# pkgadd is going to transfer the files needed to perform the installation
# to the BASEDIR directory.  Since BASEDIR cannot change for an installed
# instance of a package, attempts to later install another mode of CNR on
# a system would fail if BASEDIR was used to store the various installation
# root directories.  Therefore, the Network Registrar installer uses the
# ROOTDIR variable to specify the install root for whatever flavor of Network
# Registrar being installed (it is not subject to the same limitations as the
# special BASEDIR variable).  On an initial installation, ROOTDIR and BASEDIR
# are identical.  Subsequent installations may cause the values to differ, in
# which case we need to migrate the installation files from the original
# BASEDIR directory to the new ROOTDIR location.
if [ $BASEDIR != $ROOTDIR ]; then
    mkdir -p $ROOTDIR
    mv $BASEDIR/$COREARCHIVE.gz $ROOTDIR/$COREARCHIVE.gz
    mv $BASEDIR/cnrdb_recover3 $ROOTDIR/cnrdb_recover3
    mv $BASEDIR/cnrdb_recover42 $ROOTDIR/cnrdb_recover42
    mv $BASEDIR/cnrinstall.tcl $ROOTDIR/cnrinstall.tcl
    mv $BASEDIR/gzip $ROOTDIR/gzip
    mv $BASEDIR/init.tcl $ROOTDIR/init.tcl
    mv $BASEDIR/libtcl8.4.so $ROOTDIR/libtcl8.4.so
    mv $BASEDIR/product_version.tcl $ROOTDIR/product_version.tcl
    mv $BASEDIR/tar $ROOTDIR/tar
    mv $BASEDIR/tclsh8.4 $ROOTDIR/tclsh8.4
    mv $BASEDIR/libaic.so $ROOTDIR/libaic.so
    mv $BASEDIR/libaicrpc.so $ROOTDIR/libaicrpc.so
    mv $BASEDIR/libaicsec.so $ROOTDIR/libaicsec.so
    mv $BASEDIR/libccmschema01.so $ROOTDIR/libccmschema01.so
    mv $BASEDIR/libcnrdb4.5.so $ROOTDIR/libcnrdb4.5.so
    mv $BASEDIR/libcnrschema.so $ROOTDIR/libcnrschema.so
    mv $BASEDIR/libmcd.so $ROOTDIR/libmcd.so
    mv $BASEDIR/libmcddb.so $ROOTDIR/libmcddb.so
    mv $BASEDIR/libmlog.so $ROOTDIR/libmlog.so
    mv $BASEDIR/libscp01.so $ROOTDIR/libscp01.so
    mv $BASEDIR/mcdadmin $ROOTDIR/mcdadmin
    mv $BASEDIR/aiclockmgr $ROOTDIR/aiclockmgr
fi

TAR=$ROOTDIR/tar
TCLSH=$ROOTDIR/tclsh8.4

if [ "$CNR_EXISTS" = "y" ]; then
    # Attempt to stop any existing nrcmd sessions
    if [ -x $ROOTDIR/usrbin/nrcmd ]; then
	PID=`/usr/bin/ps -e -o pid,comm |
		/usr/bin/grep $ROOTDIR/bin/guitest |
		/bin/cut -c1-5`
	if [ x"$PID" != x ]; then
	    /usr/bin/kill $PID
	fi
    fi
fi

# Add the local directory to the library search path for TCL script
# execution
if [ "$LD_LIBRARY_PATH"x = "x" ]; then
    LD_LIBRARY_PATH=$ROOTDIR:$ROOTDIR/lib
else
    LD_LIBRARY_PATH=$ROOTDIR:$ROOTDIR/lib:$LD_LIBRARY_PATH
fi

export LD_LIBRARY_PATH

TCL_LIBRARY=$ROOTDIR
export TCL_LIBRARY

# Determine the other potential installation mode of Network Registrar,
# used in some of the error messages generated by the cnrinstall.tcl script.
if [ "$CNR_CCM_MODE" = "local" ]; then
    other_mode="regional"
else
    other_mode="local"
fi

$TCLSH $ROOTDIR/cnrinstall.tcl
status=$?

# Check the return code of the Tcl postinstall script.  The installer will
# proceed if there are no errors, or an error is determined to be non-fatal
# by setting 'proceed=1'.  Only unknown error codes, or error code 1 will
# cause the installer to immediately terminate with an error, as there may
# be value in maintaining the installation files for debugging purposes.
proceed=0

case $status in
   0)
	# Success
	proceed=1
	;;

   1)
	echo "General installation error.  Please see '$LOGDIR/install_cnr_log' for more details"
	exit 1
	;;

   5)
	echo "Java version located in $JAVADIR appears to be older than 1.5.0_06"
	;;

   6)
	echo "Java directory specified by $JAVADIR does not appear to contain a valid Java installation"
	;;

   7)
	echo "JSSE directory specified by $JSSEDIR does not appear to contain a valid JSSE installation"
	;;

   8)
	echo "JSSE directory specified by $JSSEDIR is missing one or more required .jar files"
	;;

   10)
	echo "There is not enough diskspace on the $ROOTDIR filesystem to install the product"
	;;

   11)
	echo "There is not enough diskspace on the $DATADIR filesystem to install the product"
	;;

   12)
        echo "The configuration database cannot be upgraded"
	;;

   20)
	echo "The $ROOTDIR directory is already in use by a '$other_mode' mode installation of the product"
	;;

   21)
	echo "The $DATADIR directory is already in use by a '$other_mode' mode installation of the product"
	;;

   22)
	echo "The $LOGDIR directory is already in use by a '$other_mode' mode installation of the product"
	;;

   30)
	echo "Port number $CCM_PORT is configured for use by a '$other_mode' mode installation of the product"
	;;

   31)
	echo "Port number $WEBUI_PORT is configured for use by a '$other_mode' mode installation of the product"
	;;

   32)
	echo "Port number $WEBUI_SEC_PORT is configured for use by a '$other_mode' mode installation of the product"
	;;

   35)
	echo "$KEYSTORE_FILE does not appear to be a valid keystore file"
	;;

   36)
	echo "The password used to create the keystore file $KEYSTORE_FILE does not match the provided keystore password"
	;;

   101)
	echo "WARNING:  The installer detected that one or more network ports used by Network Registrar are in use, which may have a negative impact on product functionality.  Please examine the checkports_log file in the logs directory for more details."
	proceed=1
	;;

   *)
        echo "Unknown error: $status occurred during installation."
	exit 1
	;;
esac

# Remove the files which were used to perform the CNR installation
# from both from the system and the pkgadd database.  The cnrinstall.tcl
# script has registered the individual files.
echo "Unregistering files used to perform the $NAME installation..."
rm -f $ROOTDIR/$COREARCHIVE
rm -f $ROOTDIR/cnrdb_recover3
rm -f $ROOTDIR/cnrdb_recover42
rm -f $ROOTDIR/cnrinstall.tcl
rm -f $ROOTDIR/gzip
rm -f $ROOTDIR/init.tcl
rm -f $ROOTDIR/libtcl8.4.so
rm -f $ROOTDIR/product_version.tcl
rm -f $ROOTDIR/tar
rm -f $ROOTDIR/tclsh8.4
rm -f $ROOTDIR/libaic.so
rm -f $ROOTDIR/libaicrpc.so
rm -f $ROOTDIR/libaicsec.so
rm -f $ROOTDIR/libccmschema01.so
rm -f $ROOTDIR/libcnrdb4.5.so
rm -f $ROOTDIR/libcnrschema.so
rm -f $ROOTDIR/libmcd.so
rm -f $ROOTDIR/libmcddb.so
rm -f $ROOTDIR/libmlog.so
rm -f $ROOTDIR/libscp01.so
rm -f $ROOTDIR/mcdadmin
rm -f $ROOTDIR/aiclockmgr
# The following two files may exist on a failed installation
rm -f $ROOTDIR/$COREARCHIVE.gz
rm -f $ROOTDIR/debug_install

# Note that the above commands remove the ROOTDIR specified files, while the
# removef commands below must use BASEDIR
removef $PKGINST $BASEDIR/$COREARCHIVE.gz
removef $PKGINST $BASEDIR/cnrdb_recover3
removef $PKGINST $BASEDIR/cnrdb_recover42
removef $PKGINST $BASEDIR/cnrinstall.tcl
removef $PKGINST $BASEDIR/gzip
removef $PKGINST $BASEDIR/init.tcl
removef $PKGINST $BASEDIR/libtcl8.4.so
removef $PKGINST $BASEDIR/product_version.tcl
removef $PKGINST $BASEDIR/tar
removef $PKGINST $BASEDIR/tclsh8.4
removef $PKGINST $BASEDIR/libaic.so
removef $PKGINST $BASEDIR/libaicrpc.so
removef $PKGINST $BASEDIR/libaicsec.so
removef $PKGINST $BASEDIR/libccmschema01.so
removef $PKGINST $BASEDIR/libcnrdb4.5.so
removef $PKGINST $BASEDIR/libcnrschema.so
removef $PKGINST $BASEDIR/libmcd.so
removef $PKGINST $BASEDIR/libmcddb.so
removef $PKGINST $BASEDIR/libmlog.so
removef $PKGINST $BASEDIR/libscp01.so
removef $PKGINST $BASEDIR/mcdadmin
removef $PKGINST $BASEDIR/aiclockmgr

removef -f $PKGINST

# Terminate the script if an error occurred.
if [ $proceed -ne 1 ]; then
    echo "$NAME $CNR_CCM_MODE mode installation did not complete successfully."
    exit 1
fi

echo "$NAME $CNR_CCM_MODE mode installation completed successfully."

exit 0
