#!/ibin/sh
#******************************************************************************
#                      Caldera OpenSSL component ccs script
#-----------------------------------------------------------------------------
#
#       Copyright (C) 2001 Caldera, Inc.
#               All Rights Reserved.
#
#       The information in this file is provided for the exclusive use of
#       the licensees of Caldera, Inc.  Such users have the right to use,
#	modify, and incorporate this code into other products for
#	purposes authorized by the license agreement provided they include
#       this notice and the associated copyright notice with any such product.
#       The information in this file is provided "AS IS" without warranty.
#
#-----------------------------------------------------------------------------
#
#       input:
#           $1 is the name of the step
#           $2 is the keyword list (e.g., INSTALL, UPGRADE, REMOVE)
#           $3 is the package list
#=============================================================================

# Read arguments into variables
scriptname="$0"
step="$1"
keywords="$2"
pkglist="$3"

CNTLDIR=${SSO_SHARED_ROOT}/cntl
SAVELIST=${CNTLDIR}/savelist
LOGFILE=/var/tmp/OpenSSL.log

[ -n "$debug" ] && {
	echo
	date
	echo "$scriptname $step \"$keywords\" $pkglist"
} >> $LOGFILE

# Source in the standard functions library, ccsSetup.sh
. /ibin/ccsSetup.sh

# Set global variables indicating whether this is an upgrade or removal.

sso_upgrade=
for word in $keywords
do
    case "$word" in 
        UPGRADE) sso_upgrade=TRUE
                 ;;
    esac
done

# Set the appropriate NLSPATH to enable the 'dspmsg' utility to work later
# in this script.  Note that pkg scripts should have to worry about
# NLSPATH, as they should inherit the copy set by the top-level ccs script.

NLSPATH="${SSO_SHARED_ROOT}/cntl/msg/%L/%N.cat":"${NLSPATH}"
export NLSPATH

#====================================================================== INT ===
#  doPreLoad --
#
#------------------------------------------------------------------------------
doPreLoad()
{
	[ -n "$debug" ] && echo "Enter doPreLoad" >> $LOGFILE
	[ -n "$debug" ] && echo "Leave doPreLoad" >> $LOGFILE
}


#====================================================================== INT ===
#  doPostattach --
#
#------------------------------------------------------------------------------
doPostAttach()
{
	[ -n "$debug" ] && echo "Enter doPostAttach" >> $LOGFILE
	[ -n "$debug" ] && echo "Leave doPostAttach" >> $LOGFILE
}

#====================================================================== INT ===
#  doPreRegister -- 
#   
#    doPreRegister must set OSA's in place.  In an upgrade, it also merges
#    the previously saved configuration files with the files just loaded on
#    to the system.  
#------------------------------------------------------------------------------
doPreRegister()
{
	[ -n "$debug" ] && echo "Enter doPreRegister" >> $LOGFILE

        # merge configuration files here; they were saved aside in the
        # preload step
	[ -n "$debug" ] && echo "calling RestoreConfigFiles" >> $LOGFILE
	RestoreConfigFiles

	[ -n "$debug" ] && echo "Leave doPreRegister" >> $LOGFILE
}

#====================================================================== INT ===
#  doPreConfig -- 
#   
#    doPreConfig will perform actions needed prior to a relink of the
#    kernel.  It will then inform custom that a relink is needed.
#------------------------------------------------------------------------------
doPreConfig()
{
	[ -n "$debug" ] && echo "Enter doPreConfig" >> $LOGFILE
	[ -n "$debug" ] && echo "Leave doPreConfig" >> $LOGFILE
}

#====================================================================== INT ===
#  doPostExport -- 
#   
#------------------------------------------------------------------------------
doPostExport()
{
	[ -n "$debug" ] && echo "Enter doPostExport" >> $LOGFILE
	[ -n "$debug" ] && echo "Leave doPostExport" >> $LOGFILE
}

#====================================================================== INT ===
#  doPreUnExport -- 
#   
#    doPreUnExport performs clean-up tasks before a removal.
#====================================================================== INT ===
doPreUnExport()
{
	[ -n "$debug" ] && echo "Enter doPreUnExport" >> $LOGFILE
	if [ -n "$sso_upgrade" ]; then
		[ -n "$debug" ] && echo "calling SaveConfigFiles" >> $LOGFILE
		SaveConfigFiles
	fi
	[ -n "$debug" ] && echo "Leave doPreUnExport" >> $LOGFILE
}

#=========================================================================
#  SaveConfigFiles
#=========================================================================
SaveConfigFiles()
{
	[ -n "$debug" ] && echo "Entering SaveConfigFiles" >> $LOGFILE
	for cfgfile in /etc/ssl/openssl.cnf
	do
		if [ -f ${SSO_CLIENT_ROOT}$cfgfile ]; then
			dir=`dirname $cfgfile`
			[ "$dir" = "/" ] && dir=""
			[ -n "$debug" ] && \
			echo "cp -f ${SSO_CLIENT_ROOT}$cfgfile ${cfgfile}-" \
				>> $LOGFILE
			cp -f ${SSO_CLIENT_ROOT}$cfgfile \
				${cfgfile}- >> $LOGFILE 2>&1
		fi
	done
	[ -n "$debug" ] && echo "Leaving SaveConfigFiles" >> $LOGFILE
}

#=========================================================================
#  RestoreConfigFiles
#=========================================================================
RestoreConfigFiles()
{
	[ -n "$debug" ] && echo "Entering RestoreConfigFiles" >> $LOGFILE
	for cfgfile in /etc/ssl/openssl.cnf
	do
		if [ -n "$sso_upgrade" ]; then
			[ -n "$debug" ] && \
			echo "cp -f ${cfgfile}- ${SSO_CLIENT_ROOT}/$cfgfile" \
				>> $LOGFILE
			cp -f ${cfgfile}- \
				${SSO_CLIENT_ROOT}/$cfgfile \
				>> $LOGFILE 2>&1
			[ -n "$debug" ] && echo "rm -f ${cfgfile}-" >> $LOGFILE
			rm -f ${cfgfile}- >> $LOGFILE 2>&1
		fi
	done
	[ -n "$debug" ] && echo "Leaving RestoreConfigFiles" >> $LOGFILE
}

#====================================================================== INT ===
#  main --
#   
#    Based on step specified, perform actions of the general form:
#       Global initialization
#       Loop to call each package script for package-specific actions
#       Global cleanup
#
#    NOTE: Currently, a number of steps exist for which no action needs to
#    be specified in this product.  This will probably be true for most
#    products in the near future.
#------------------------------------------------------------------------------


# the exit value from this script
ccs_return_value=0
  
# These fns get called prior to running package-specific scripts.
case "$step" in
        PRE_UNEXPORT)    doPreUnExport;;
        PRE_REGISTER)    doPreRegister;;
esac


# the directory containing whatever package scripts we've got
pkgscripts=${SSO_SHARED_ROOT}/cntl/packages

# For each package, run that package's package script with the same args as
# this script got.  Record any exit values from the package scripts.
for pkg in $pkglist
do
    short_pkg_name=`getPackageCode $pkg`
    [ -f ${pkgscripts}/${short_pkg_name}/ccs ] && {
        ${pkgscripts}/${short_pkg_name}/ccs $step "$keywords" "$pkglist"

        # This script will exit with the most severe of exit code of any of
        # its package scripts.  Since an exit of 1 means 'fail', it's more
        # severe than an exit of 2, which means 'warning'.  An exit of 0
        # (success) is the least severe.
        case "$?" in
            1)  ccs_return_value=1
                ;;
            2)  [ "$ccs_return_value" -eq 0 ] && ccs_return_value=2
        esac
    }
done

[ -n "$debug" ] && echo "$0 exit $ccs_return_value" >> $LOGFILE
exit $ccs_return_value
