#!/ibin/sh
#******************************************************************************
#                OpenSSL cqs script
#-----------------------------------------------------------------------------
#       input:
#           $1 is the name of the step
#           $2 is the keyword list (e.g. UPGRADE)
#           $3 is the package list
#=============================================================================

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

LOGFILE=/var/tmp/OpenSSL.log

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

[ -n "$debug" ] && echo "$scriptname \"$keywords\" $pkglist" >> $LOGFILE

# Set global variables that may be used elsehere in this script

CNTLDIR=${SSO_SHARED_ROOT}/cntl

OSR504_HERE=
OSR505_HERE=
OSR506_HERE=

# some messages

OS_MSG="\nThe following are supported Operating System environments for this\nproduct:\n\nSCO OpenServer 5.0.4\nSCO OpenServer 5.0.5\nSCO OpenServer 5.0.6\n\nNone of these products are installed on your system. Installation may \nresult in an unsupported configuration."

# are we suggesting the correct patches?
MSG_missPatch="WARNING: The following supplements(s) are STRONGLY recommended
    for all systems running this software. You should install
    these supplement(s) as soon as this product installation
    is complete. The supplement(s) are available via anonymous ftp
    from ftp.sco.com in the /SLS directory (ftp://ftp.sco.com/SLS/).
    In particular, the Network Maintenance Supplement can be downloaded from 
    ftp://ftp.sco.com/SLS/oss449f/ ."

MSG_noDB="ERROR: Cannot locate component database for _cmpnt_ component."

old_upgrade=
sso_upgrade=
iqm_file_exists=
local_client=
true_client=
customMode=LAYERED
for word in $keywords
do
    case "$word" in 
        OLD_CUSTOM_UPGRADE) old_upgrade=TRUE
                 ;;
	UPGRADE)            sso_upgrade=TRUE
		;;
	IQM_SAVE)	    iqm_file_exists=TRUE
                	    customMode="IQM"
		;;
	FOR_LOCAL_CLIENT)   local_client=TRUE
		;;
	TRUE_CLIENT_ROOT)   true_client=TRUE
		;;
    esac
done

# Set the appropriate NLSPATH to enable the 'dspmsg' utility to work later
# in this script.  

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

#******************************************************************************
#  cleanup --
#
#    cleanup exits the package script with the passed argument
#
#******************************************************************************

cleanup() {
	# do we need to make sure ${CCS_PERSISTENT_STORAGE} is empty?
	# pkg ccs scripts might save old config, but not this cqs script.
	# For now, leave the cleanup the responsibility of the pkg ccs.
        exit $1
}

#=============================================================================
#
# getyn
#
# Prompt for yes or no answer - returns non-zero for no
#
# Usage: getyn "question"
# Argument is a quoted question
# Notes: getyn returns 0 if the answer is y or Y
#        and 1 if the answer is n or N
#        and 2 if the answer is q or Q
#=============================================================================
getyn() {
        while   echo "$* (y/n/q) \c"
        do      read yn rest
                case $yn in
                [yY])   return 0                                ;;
                [nN])   return 1                                ;;
                [qQ])   return 2                                ;;
                *)      echo "Please Make a selection\n"          ;;
                esac
        done
}

#=============================================================================
# check_patch <patchID> <patchDescription>
#
# Check that a given patch is applied to the component that we are
# working on. We need to check for this in the *client* databases.
#=============================================================================

check_patch() {
    if [ -f $CLIENT_DB ] ; then
	pattern=`echo $1 | sed "s!\.!\\\\\.!g"`
	pattern="patches.*[= ]${pattern}"

	grep -q "${pattern}" $CLIENT_DB
	if [ $? -ne 0 ] ; then
	    List="$List\n\t$2"
	fi
    else
	echo "\n$MSG_noDB" | sed "s!_cmpnt_!${CMPNT}!"
    fi
}

#====================================================================== INT ===
#  main --
#   
#------------------------------------------------------------------------------
#
#
# check for an OS first

if [ "${customMode}" = "LAYERED" ]
then
	component=`customquery ListComponents -p CONFIGURE SCO:Unix | grep "SCO:Unix" 2>/dev/null`
else	# IQM install
	if [ -x /ibin/uname ]
	then
		component=`/ibin/uname -v 2>/dev/null`
	fi
fi

echo "${component}" | grep "5.0.4" >/dev/null 2>&1
if [ $? -eq 0 ]
then
        OSR504_HERE=TRUE
fi

echo "${component}" | grep "5.0.5" >/dev/null 2>&1
if [ $? -eq 0 ]
then
        OSR505_HERE=TRUE
fi

echo "${component}" | grep "5.0.6" >/dev/null 2>&1
if [ $? -eq 0 ]
then
        OSR506_HERE=TRUE
fi

# if this is 504, 505 or 506, good, else,
# inform of invalid configuration and ask to continue or not

[ -n "$debug" ] && echo "component: $component" >> $LOGFILE

if [ -n "${OSR506_HERE}" ] || [ -n "${OSR504_HERE}" ] || [ -n "${OSR505_HERE}" ]
then
	# continue
	:
else
	ccsWarning "${OS_MSG}"
	getyn "\nDo you wish to proceed with the installation? "
	
	if [ $? -eq 0 ]
	then
		# continue
		:
	else
		exit ${FAIL}
	fi
fi

# look for important supplements; print WARNING if NOT installed
# check these

[ "$customMode" = "LAYERED" ] && {
    unixVer=`customquery ListComponents -p CONFIGURE SCO:Unix`

[ -n "$debug" ] && echo "unixVer=$unixVer" >> $LOGFILE

    case $unixVer in
#	SCO:Unix::5.0.7*)
#		break
#		;;
	SCO:Unix::5.0.4Eb)
		break
		;;
	SCO:Unix::5.0.5*)
		break
		;;
	SCO:Unix::5.0.6*)
		break
		;;
	*)	echo "WARNING: Use of this product on this Operating System
	environment is unsupported. You should be using
	Release 5.0.4, 5.0.5 or 5.0.6 of SCO OpenServer."
		;;
    esac
}

# the exit value from this script

cqs_return_value=0

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