#!/bin/bash
###############################################################################
## spininstall                                                                #
###############################################################################
## Copyright 2002. Spinnaker Networks Inc.                                    #
## All rights reserved.                                                       #
##                                                                            #
###############################################################################
## This script is used by spinmgmt to upgrade the software release on the     #
## alternate RootFileSystem                                                   #
##                                                                            #
###############################################################################

#########
## The Range of Software Release from which an upgrade is possible
## Update the following variables to the valid range of software release
## versions such that an upgrade will be done only if
##     MIN_VERSION <= CUR_VERSION <= MAX_VERSION
##     Where MIN_VERSION is
##        MIN_MAJOR
##        MIN_MINOR
##        MIN_PATCH
##        MIN_BUILDNUMBER
##      MAX_VERSION is
##        MAX_MAJOR
##        MAX_MINOR
##        MAX_PATCH
##        MAX_BUILDNUMBER
#########
#MIN_VERSION
MIN_MAJOR=1
MIN_MINOR=0
MIN_PATCH=0
MIN_BUILDNUMBER=1
#MAX_VERSION
MAX_MAJOR=2
MAX_MINOR=99
MAX_PATCH=99999
MAX_BUILDNUMBER=999999


#########
## Spinnaker RPM Packages
## (Optional) Specify the Spinnaker RPM packages that have to be installed/upgraded
## If not specified, all the RPM packages in the directory (where this script is located)
## would be automatically selected for installation
#########
# SPIN_RPM_LIST="
#	kernel-2.2.17spinos-31640.i386.rpm
#	spindiags-2.0.0-31640.i386.rpm
#          "
#########
SPIN_RPM_LIST="
        kernel-*spinos*.rpm
        spindiags-*.rpm
        spindocs-*.rpm
        spinfs-*.rpm
        spingcjlib-*.rpm
        spinmgmt-*.rpm
        spinndmpd-*.rpm 
        spinrdbapp-*.rpm
	spinstatsd-*.rpm
        spinsnmp-*.rpm
        spinsyslogd-*.rpm
        spintools-*.rpm
        spinvirusd-*.rpm
         "

#########
## Other (RedHat Linux/Third Party) RPM Packages
## (Optional) Specify the Other RPM packages that have to be installed/upgraded
#########
# OTHER_RPM_LIST="
#	lsof-4.51-1.i386.rpm
#          "
#########
OTHER_RPM_LIST="
        lsof-4.51-1.i386.rpm
        patch-2.5.4-9.i386.rpm
        telnet-0.17-18.1.i386.rpm

        lkcdutils-4.1-1.i386.rpm
        SysVinit-2.78-17.i386.rpm
        dev-3.3-4.i386.rpm
        modutils-2.4.14-3.i386.rpm

        unzip-5.41-3.i386.rpm
        perl-libnet-1.0703-6.noarch.rpm

        openssl-0.9.6-19.i386.rpm
         "

## Include make & stunnel only if openssl is installed
rpm -q openssl >/dev/null 2>/dev/null
RETVAL=$?
if [ ${RETVAL} -eq 0 ]; then
    OTHER_RPM_LIST="
        ${OTHER_RPM_LIST} 
        make-3.79.1-8.i386.rpm
        stunnel-3.13-3.i386.rpm
         "
fi

#########
## RPM CommandLine Options
## Specify the options that need to be passed to the rpm commandline during
## upgrade. Possible options are (not a complete list)
#RPM_OPTIONS="
#            --replacepkgs
#            --replacefiles
#            --oldpackage
#            --nodeps
#            "
#########
RPM_OPTIONS="--replacepkgs
             --replacefiles"


#########
## Pre-Upgrade Script (Optional)
## Specify the script that would executed before the software packages are upgraded
##  (e.g.) the script could make a backup of certain configuration files
#########
PRE_UPGRADE_SCRIPT=preinstall

#########
## POST-Upgrade Script (Optional)
## Specify the script that would executed after all the software packages are upgraded
##  (e.g.) the script could restore the backup configuration files with some modifications
#########
POST_UPGRADE_SCRIPT=postinstall



###############################################################################
###############################################################################
## DO NOT EDIT BELOW THIS LINE
###############################################################################
###############################################################################
mkdir -p /var/spinmgmt/log
LOG_FILE=/var/spinmgmt/log/install.log

STD_OUT=`ls -l /dev/fd/1 | sed 's/.*->//' | grep -v "pipe:"`
if [ -z ${STD_OUT} ]; then
    STD_OUT=/dev/null
fi

STD_ERR=`ls -l /dev/fd/2 | sed 's/.*->//' | grep -v "pipe:"`
if [ -z ${STD_ERR} ]; then
    STD_ERR=/dev/null
fi


#########
## Function: Usage
## Displays the Usage for this script
#########
Usage ()
{
    echo "usage:  install [-r <rpm directory>] [-a] [-s <script directory>] [-d <dblade id>] [-f] [-h]" 
    echo "            r:  (optional) The directory where RPM packages reside" 
    echo "                if not specified, defaults to the directory where install resides" 
    echo "            a:  (optional) Installs all the RPM packages in the directory" 
    echo "                if not specified, install only the Spinnaker RPMs" 
    echo "            s:  (optional) The directory where scripts reside" 
    echo "                if not specified, defaults to the directory where install resides" 
    echo "            d:  (optional) The dblade id for the SpinServer" 
    echo "                if not specified, it is obtained from spinmgmt (spinmgmt has to be running!!!)" 
    echo "            f:  (optional) Do a force upgrade (or downgrade)" 
    echo "            h:  displays this usage message" 
}

#########
## Function: QuitOnError
## Displays given error message and exits the script
#########
QuitOnError ()
{
    echo "********************************************************************"
    echo " *** Error Occured During Install!!! *** "
    echo $*
    echo "********************************************************************"

    echo " *** Installation Failed ***" >${STD_ERR}
    echo $* >${STD_ERR}
    echo "See file ${LOG_FILE} for details" >${STD_ERR}
    exit 1
}


#########
## Function: MountProcOnAltroot
## Mounts /altroot/proc
#########
PROC_MOUNTED=0;
MountProcOnAltroot ()
{
    if [ ${PROC_MOUNTED} -ne 1 ]; then
	echo "Mounting /altroot/proc"
	mount -t proc none /altroot/proc
	[ $? -eq 0 ] && PROC_MOUNTED=1;
    fi
}


#########
## Function: UnMountProcFromAltroot
## Unmounts /altroot/proc
#########
UnMountProcFromAltroot ()
{
    if [ ${PROC_MOUNTED} -eq 1 ]; then
	echo "Un-Mounting /altroot/proc"
	umount /altroot/proc
	[ $? -eq 0 ] && PROC_MOUNTED=0;
    fi
}

#########
## Function: Interrupted
## Called when the script is interrupted
#########
trap 'Interrupted' INT QUIT TERM

Interrupted ()
{
    UnMountProcFromAltroot
    QuitOnError "Install Interrupted"
    exit 1
}

#########
## Function: GetSoftwareInfoFromFile
## Reads the software info file specfied ($1) and updates the following variables
##     T_MAJOR
##     T_MINOR
##     T_PATCH
##     T_BUILDNUMBER
#########
GetSoftwareInfoFromFile ()
{
    T_SW_FILE=$1

    if [ ! -f ${T_SW_FILE} ]; then
	QuitOnError "Software Info file ${T_SW_FILE} Does not Exist"
    fi

    T_MAJOR=`cat ${T_SW_FILE} | grep "MAJOR=" | sed 's/MAJOR=//'`
    T_MINOR=`cat ${T_SW_FILE} | grep "MINOR=" | sed 's/MINOR=//'`
    T_PATCH=`cat ${T_SW_FILE} | grep "PATCH=" | sed 's/PATCH=//'`
    T_BUILDNUMBER=`cat ${T_SW_FILE} | grep "BUILDNUMBER=" | sed 's/BUILDNUMBER=//'`
}


#########
## Function: ParseSoftwareInfo
## Parse the given version information in T_VERSION and updates the following variables
##     T_MAJOR
##     T_MINOR
##     T_PATCH
##     T_BUILDNUMBER
#########
ParseSoftwareInfo ()
{
    T_MAJOR=`echo ${T_VERSION} | grep "MAJOR=" | sed 's/\(.*MAJOR=\{1\}\)\([0-9]\+\).*/\2/'`
    T_MINOR=`echo ${T_VERSION} | grep "MINOR=" | sed 's/\(.*MINOR=\{1\}\)\([0-9]\+\).*/\2/'`
    T_PATCH=`echo ${T_VERSION} | grep "PATCH=" | sed 's/\(.*PATCH=\{1\}\)\([0-9]\+\).*/\2/'`
    T_BUILDNUMBER=`echo ${T_VERSION} | grep "BUILDNUMBER=" | sed 's/\(.*BUILDNUMBER=\{1\}\)\([0-9]\+\).*/\2/'`
}


#########
## Function: GetSoftwareInfo
## Reads the software info file specfied ($1) and updates the following variables
##     T_MAJOR
##     T_MINOR
##     T_PATCH
##     T_BUILDNUMBER
#########
GetSoftwareInfo ()
{
    T_SW_FILE=$1

    if [ -f ${T_SW_FILE} ]
    then
	GetSoftwareInfoFromFile ${T_SW_FILE}
    else

	IS_ALTROOT=`echo ${T_SW_FILE} | grep "^/altroot"`
	if [ ! -z ${IS_ALTROOT} ]
	then
	    CHANGE_ROOT="--root /altroot"
	else
	    CHANGE_ROOT=
	fi

	T_VERSION=`rpm ${CHANGE_ROOT} -q spinmgmt --queryformat "VERSION=%-18{VERSION} BUILDNUMBER=%7{RELEASE}" | sed 's/\(VERSION=\{1\}\)\([0-9]\+\)\(.\([0-9]\+\)\)\?\(.\([0-9]\+\)\)\?/ MAJOR=\2 MINOR=\4 PATCH=\6/'`

	ParseSoftwareInfo
    fi
}


#########
## Function: GetOptions
## Get the options
#########
GetOptions ()
{
    #########
    ## Retrieve the Command Line Options
    #########

    ALL_RPMS=0
    FORCE=0
    GETOPTS="getopts ':ar:s:d:fh' OPTION $*"

    ${GETOPTS}
    RET=$?
    while [ ${RET}  -le 0 ]
    do
      case ${OPTION} in
	  a)  ALL_RPMS=1
	      ;;
	  r)  RPM_DIR=${OPTARG}
	      ;;
	  s)  SCRIPT_DIR=${OPTARG}
	      ;;
	  d)  DBLADE_ID=${OPTARG}
	      ;;
	  f)  RPM_OPTIONS="--force"
	      FORCE=1
	      ;;
	  h)  Usage
	      exit 0
	      ;;
	  *)  Usage
	      exit 1
	      ;;
      esac

      ${GETOPTS}
      RET=$?
    done
}

#########
## Function: Initialize
## Intializes the various variables needed to perform the install
#########
Initialize ()
{
    #########
    ## Unset LD_PRELOAD as there is no need to pre load libraries while running this script
    #########
    LD_PRELOAD=

    #########
    ## Working Directory
    ## Same as where the script is located
    #########
    WORK_DIR=`dirname $0`
    export WORK_DIR

    if [ -z ${RPM_DIR} ]; then
	RPM_DIR=${WORK_DIR}
    fi

    if [ -z ${SCRIPT_DIR} ]; then
	SCRIPT_DIR=${WORK_DIR}
    fi
    export RPM_DIR SCRIPT_DIR WORK_DIR


    #########
    ## Retrieve The New Software Release Version information
    ## This information is obtained from the spinmgmt rpm in the RPM directory
    ##     NEW_MAJOR
    ##     NEW_MINOR
    ##     NEW_PATCH
    ##     NEW_BUILDNUMBER
    #########
    MGMT_RPM=`ls ${RPM_DIR}/spinmgmt*`
    if [ -z ${MGMT_RPM} ]; then
	QuitOnError "Cannot find spinmgmt rpm; Cannot continue"
    fi

    T_VERSION=`rpm -qp ${MGMT_RPM} --queryformat "VERSION=%-18{VERSION} BUILDNUMBER=%7{RELEASE}" | sed 's/\(VERSION=\{1\}\)\([0-9]\+\)\(.\([0-9]\+\)\)\?\(.\([0-9]\+\)\)\?/ MAJOR=\2 MINOR=\4 PATCH=\6/'`
    ParseSoftwareInfo
    NEW_MAJOR=${T_MAJOR}
    NEW_MINOR=${T_MINOR}
    NEW_PATCH=${T_PATCH}
    NEW_BUILDNUMBER=${T_BUILDNUMBER}
    export NEW_MAJOR NEW_MINOR NEW_PATCH NEW_BUILDNUMBER

    echo "The Version information for the new software that would be installed"
    echo "    NEW_MAJOR=${NEW_MAJOR}"
    echo "    NEW_MINOR=${NEW_MINOR}"
    echo "    NEW_PATCH=${NEW_PATCH}"
    echo "    NEW_BUILDNUMBER=${NEW_BUILDNUMBER}"


    #########
    ## Get the software version of the current RootFileSystem
    #########
    SW_FILE="/etc/sysconfig/spinsoftware"
    GetSoftwareInfo ${SW_FILE}
    CUR_MAJOR=${T_MAJOR}
    CUR_MINOR=${T_MINOR}
    CUR_PATCH=${T_PATCH}
    CUR_BUILDNUMBER=${T_BUILDNUMBER}
    export CUR_MAJOR CUR_MINOR CUR_PATCH CUR_BUILDNUMBER

    echo "The Version information of the current RootFileSystem"
    echo "    CUR_MAJOR=${CUR_MAJOR}"
    echo "    CUR_MINOR=${CUR_MINOR}"
    echo "    CUR_PATCH=${CUR_PATCH}"
    echo "    CUR_BUILDNUMBER=${CUR_BUILDNUMBER}"


    #########
    ## Get the software version of the alternate RootFileSystem
    #########
    ALT_SW_FILE="/altroot/etc/sysconfig/spinsoftware"
    GetSoftwareInfo ${ALT_SW_FILE}
    ALT_CUR_MAJOR=${T_MAJOR}
    ALT_CUR_MINOR=${T_MINOR}
    ALT_CUR_PATCH=${T_PATCH}
    ALT_CUR_BUILDNUMBER=${T_BUILDNUMBER}
    export ALT_CUR_MAJOR ALT_CUR_MINOR ALT_CUR_PATCH ALT_CUR_BUILDNUMBER

    echo "The Version information of the alternate RootFileSystem"
    echo "    ALT_CUR_MAJOR=${ALT_CUR_MAJOR}"
    echo "    ALT_CUR_MINOR=${ALT_CUR_MINOR}"
    echo "    ALT_CUR_PATCH=${ALT_CUR_PATCH}"
    echo "    ALT_CUR_BUILDNUMBER=${ALT_CUR_BUILDNUMBER}"


    #########
    ## If no RPM packages have been specified, select all of them that are in the directory
    #########
    RPM_NAME=`echo ${SPIN_RPM_LIST} | sed 's/[ ]\+.*//'`

    SPIN_RPM_FILES=
    if [ ${ALL_RPMS} -eq 1 ] || [ -z ${RPM_NAME} ];
    then
	ALL_RPMS=1
	SPIN_RPM_FILES=`ls ${RPM_DIR}/*.rpm 2>/dev/null`
    else
	for RPM_NAME in $SPIN_RPM_LIST
	do
	  RPM_FILE=`ls ${RPM_DIR}/${RPM_NAME} 2>/dev/null`
	  SPIN_RPM_FILES="${SPIN_RPM_FILES} ${RPM_FILE}"
	done
    fi


    #########
    ## The RPM Command
    ## The Software packages are ALWAYS installed on the alternate RootFileSystem
    #########
    RPM_EXEC="rpm --root /altroot"

    #########
    ## Find the Spinserver Product ID and Model Number
    #########
    SPINSERVER_PRODUCT_ID=`cat /proc/smbios/base_board-0-0002 | grep "Product:" | sed 's/.*Product: *//'`
    SPINSERVER_MODEL="S3300"
    if [ ${SPINSERVER_PRODUCT_ID} == "X5DL8" ]; then
	SPINSERVER_MODEL="S4100"
    fi

    export SPINSERVER_PRODUCT_ID SPINSERVER_MODEL
}


#########
## Function: ValidateVersion
## Validate the version information
#########
ValidateVersion ()
{

    #########
    ## Validate Upgrade Range
    #########

    invalid=0
    if [ ${MIN_MAJOR} -gt ${MAX_MAJOR} ]; then
	invalid=1
    elif [ ${MIN_MAJOR} -eq ${MAX_MAJOR} ]; then
	if [ ${MIN_MINOR} -gt ${MAX_MINOR} ]; then
	    invalid=1
	elif [ ${MIN_MINOR} -eq ${MAX_MINOR} ]; then
	    if [ ${MIN_PATCH} -gt ${MAX_PATCH} ]; then
		invalid=1
	    elif [ ${MIN_PATCH} -eq ${MAX_PATCH} ]; then
		if [ ${MIN_BUILDNUMBER} -gt ${MAX_BUILDNUMBER} ]; then
		    invalid=1
		fi
	    fi
	fi
    fi

    if [ ${invalid} -eq 1 ]; then
	QuitOnError "Invalid Upgrade Range Specified"
    fi   


    #########
    ## Check that the software version on the current RootFileSystem
    ## is within the upgrade range
    #########

    invalid=0
    if [ ${MIN_MAJOR} -gt ${CUR_MAJOR} ]; then
	invalid=1
    elif [ ${MIN_MAJOR} -eq ${CUR_MAJOR} ]; then
	if [ ${MIN_MINOR} -gt ${CUR_MINOR} ]; then
	    invalid=1
	elif [ ${MIN_MINOR} -eq ${CUR_MINOR} ]; then
	    if [ ${MIN_PATCH} -gt ${CUR_PATCH} ]; then
		invalid=1
	    elif [ ${MIN_PATCH} -eq ${CUR_PATCH} ]; then
		if [ ${MIN_BUILDNUMBER} -gt ${CUR_BUILDNUMBER} ]; then
		    invalid=1
		fi
	    fi
	fi
    fi
    if [ ${CUR_MAJOR} -gt ${MAX_MAJOR} ]; then
	invalid=1
    elif [ ${CUR_MAJOR} -eq ${MAX_MAJOR} ]; then
	if [ ${CUR_MINOR} -gt ${MAX_MINOR} ]; then
	    invalid=1
	elif [ ${CUR_MINOR} -eq ${MAX_MINOR} ]; then
	    if [ ${CUR_PATCH} -gt ${MAX_PATCH} ]; then
		invalid=1
	    elif [ ${CUR_PATCH} -eq ${MAX_PATCH} ]; then
		if [ ${CUR_BUILDNUMBER} -gt ${MAX_BUILDNUMBER} ]; then
		    invalid=1
		fi
	    fi
	fi
    fi

    if [ ${invalid} -eq 1 ]; then
	ERROR_MSG=`echo "Current Software Release on Altroot is not in upgradable range.
	                Valid Range is ${MIN_MAJOR}.${MIN_MINOR}.${MIN_PATCH}-${MIN_BUILDNUMBER} 
                                    to ${MAX_MAJOR}.${MAX_MINOR}.${MAX_PATCH}-${MAX_BUILDNUMBER}"`
	QuitOnError ${ERROR_MSG}
    fi   

    #########
    ## Check that it is a upgrade
    #########

    if [ ${CUR_MAJOR} -gt ${NEW_MAJOR} ]; then
	invalid=1
    elif [ ${CUR_MAJOR} -eq ${NEW_MAJOR} ]; then
	if [ ${CUR_MINOR} -gt ${NEW_MINOR} ]; then
	    invalid=1
	elif [ ${CUR_MINOR} -eq ${NEW_MINOR} ]; then
	    if [ ${CUR_PATCH} -gt ${NEW_PATCH} ]; then
		invalid=1
	    elif [ ${CUR_PATCH} -eq ${NEW_PATCH} ]; then
		if [ ${CUR_BUILDNUMBER} -gt ${NEW_BUILDNUMBER} ]; then
		    invalid=1
		fi
	    fi
	fi
    fi

    if [ ${invalid} -eq 1 ]; then
	QuitOnError "Cannot Downgrade Software"
    fi   
}


#########
## Function: ValidateFiles
## Validate that the RPM and script files exist
#########
ValidateFiles ()
{

    #########
    ## Check the RPMs exist
    #########
    RPM_FILE=`echo ${SPIN_RPM_FILES} | sed 's/[ ]\+.*//'`
    if [ -z ${RPM_FILE} ]; then
	QuitOnError "No RPM specified for install"
    fi
    
    for RPM_FILE in $SPIN_RPM_FILES
    do
      if [ ! -f ${RPM_FILE} ]; then
	  QuitOnError "The RPM ${RPM_FILE} does not exist"
      fi
    done


    #########
    ## Check the Pre-Upgrade Script exists
    #########
    if [ ! -z ${PRE_UPGRADE_SCRIPT} ]
    then
	if [ ! -f ${SCRIPT_DIR}/${PRE_UPGRADE_SCRIPT} ]
	then
	    QuitOnError "The Pre upgrade Script:${SCRIPT_DIR}/${PRE_UPGRADE_SCRIPT} does not exist"
	fi
    fi
    

    #########
    ## Check the Post-Upgrade Script exists
    #########
    if [ ! -z ${POST_UPGRADE_SCRIPT} ]
    then
	if [ ! -f ${SCRIPT_DIR}/${POST_UPGRADE_SCRIPT} ]
	then
	    QuitOnError "The Post upgrade Script:${SCRIPT_DIR}/${POST_UPGRADE_SCRIPT} does not exist"
	fi
    fi

    if [ ! -z ${DBLADE_ID} ]; then
	if [ ! -z `echo ${DBLADE_ID} | sed 's/[0-9]*//'` ]; then
	    QuitOnError "Specified Dblade Id:${DBLADE_ID} is not numeric"
	fi

	export DBLADE_ID
    fi
}

#########
## Function: Validate
## Validate the various variables needed to perform the install
#########
Validate ()
{

    if [ ${FORCE} -eq 0 ]; then
	ValidateVersion
    fi

    ValidateFiles
}

#########
## Function: PerformInstall
## Peforms the various steps involved in install
#########
PerformInstall ()
{
    #########
    ## Mount /altroot/proc
    #########
    MountProcOnAltroot

    #########
    ## Execute the Pre-Upgrade Script
    #########
    if [ ! -z ${PRE_UPGRADE_SCRIPT} ]
    then
	echo " *** Pre-Upgrade ***"
	echo "Calling Pre-Upgrade Script"
	echo "${SCRIPT_DIR}/${PRE_UPGRADE_SCRIPT}"
	${SCRIPT_DIR}/${PRE_UPGRADE_SCRIPT}
	if [ $? -ne 0 ]; then
	    QuitOnError "Pre-Upgrade Script ${SCRIPT_DIR}/${PRE_UPGRADE_SCRIPT} Failed!!!; Cannot Continue!!!"
	fi
	echo " *** Pre-Upgrade Complete ***"
	echo
    fi


    #########
    ## Add the Other RPMs to the list of RPMs to be installed
    ## Add them only if they are not already installed.
    #########
    OTHER_RPM_FILES=
    if [ ${ALL_RPMS} -eq 0 ]; then
	for RPM_NAME in $OTHER_RPM_LIST
	do
	    RPM_FILE="${RPM_DIR}/${RPM_NAME}"
	    if [ -f ${RPM_FILE} ]; then
		RPM_LABEL=`${RPM_EXEC} --queryformat "%{NAME}-%{VERSION}-%{RELEASE}" -qp ${RPM_FILE}`
		${RPM_EXEC} -q ${RPM_LABEL} > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
		    OTHER_RPM_FILES="${OTHER_RPM_FILES} ${RPM_FILE}"
		else
		    echo "The RPM ${RPM_LABEL} already installed"
		fi
	    else
		RPM_LABEL=`echo ${RPM_NAME} | sed 's/\.??86\.rpm//'`
		${RPM_EXEC} -q ${RPM_LABEL} > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
		    QuitOnError "The RPM File ${RPM_FILE} does not exist"
		else
		    echo "The RPM ${RPM_LABEL} already installed"
		fi
	    fi
	done
    fi

    #########
    ## Execute the RPM Command to install/upgrade software
    #########
    echo " *** RPM Install ***"
    echo "Installing the RPM packages"
    echo "(cd ${RPM_DIR}; ${RPM_EXEC} ${RPM_OPTIONS} -U ${SPIN_RPM_FILES} ${OTHER_RPM_FILES})"
    (cd ${RPM_DIR}; ${RPM_EXEC} ${RPM_OPTIONS} -U ${SPIN_RPM_FILES} ${OTHER_RPM_FILES})
    #########
    ## WARNING: Do not check for error in previous command
    ##          Even an error in one of the pre/post install scripts of the RPM FILES results in
    ##          the return value ($?) not being zero
    #########
    #if [ $? -ne 0 ]; then
    #    QuitOnError "RPM Installation Failed!!!; Cannot Continue!!!"
    #fi


    #########
    ## Check the RPMS were installed successfully
    #########
    if [ ${ALL_RPMS} -eq 0 ]; then
	RPM_FILES="${SPIN_RPM_FILES} ${OTHER_RPM_FILES}"
    else
	RPM_FILES="${SPIN_RPM_FILES}"
    fi

    for RPM_FILE in ${RPM_FILES}
    do
        RPM_LABEL=`${RPM_EXEC} --queryformat "%{NAME}-%{VERSION}-%{RELEASE}" -qp ${RPM_FILE}`
	${RPM_EXEC} -q ${RPM_LABEL} > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
	    QuitOnError "The RPM ${RPM_FILE} was not installed"
	else
	    echo "RPM ${RPM_LABEL} installed"
	fi
    done

    echo " *** RPM Install Complete ***"
    echo

    #########
    ## Execute the Post-Upgrade Script
    #########
    if [ ! -z ${POST_UPGRADE_SCRIPT} ]
    then
	echo " *** Post-Upgrade ***"
	echo "Calling Post-Upgrade Script"
	echo "${SCRIPT_DIR}/${POST_UPGRADE_SCRIPT}"
	${SCRIPT_DIR}/${POST_UPGRADE_SCRIPT}
	if [ $? -ne 0 ]; then
	    QuitOnError "Post-Upgrade Script ${SCRIPT_DIR}/${POST_UPGRADE_SCRIPT} Failed!!!; Cannot Continue!!!"
	fi
	echo " *** Post-Upgrade Complete ***"
	echo
    fi

    #########
    ## UnMount /altroot/proc
    #########
    UnMountProcFromAltroot

}

#########
## Function: Finalize
## Writes the software version information
#########
Finalize ()
{

    #########
    ## Get the last sync times from software information file on alternate RootFileSystem
    #########
    if [ -f ${ALT_SW_FILE} ]; then
	LAST_SYNCTIME=`grep "LAST_SYNCTIME=" ${ALT_SW_FILE}`
	LAST_CFG_SYNCTIME=`grep "LAST_CFG_SYNCTIME=" ${ALT_SW_FILE}`
    fi

    #########
    ## Update the software version information on the alternate RootFileSystem 
    #########
    INSTALLTIME=`date +"%a %b %d %T %Y"`
    echo "MAJOR=${NEW_MAJOR}" > ${ALT_SW_FILE}
    echo "MINOR=${NEW_MINOR}" >> ${ALT_SW_FILE}
    echo "PATCH=${NEW_PATCH}" >> ${ALT_SW_FILE}
    echo "BUILDNUMBER=${NEW_BUILDNUMBER}" >> ${ALT_SW_FILE}
    echo "INSTALLTIME=${INSTALLTIME}" >> ${ALT_SW_FILE}
    echo "${LAST_SYNCTIME}" >> ${ALT_SW_FILE}
    echo "${LAST_CFG_SYNCTIME}" >> ${ALT_SW_FILE}

    #########
    ## Create the software version information on the current RootFileSystem 
    ## if it does not exist already
    #########
    if [ ! -f ${SW_FILE} ]; then
	echo "MAJOR=${CUR_MAJOR}" > ${SW_FILE}
	echo "MINOR=${CUR_MINOR}" >> ${SW_FILE}
	echo "PATCH=${CUR_PATCH}" >> ${SW_FILE}
	echo "BUILDNUMBER=${CUR_BUILDNUMBER}" >> ${SW_FILE}
    fi

}

#########
## Function: Main
## This is the Main routine which is called
#########
Main ()
{
    echo
    echo "********************************************************************"
    echo " *** Installation Started at " `date +"%a %b %d %T %Y"` "***"
    echo "********************************************************************"

    Initialize $*
    Validate
    PerformInstall
    Finalize    

    echo "********************************************************************"
    echo " *** Installation Completed at " `date +"%a %b %d %T %Y"` "***"
    echo "********************************************************************"
    echo

    return 0
}

GetOptions $*

echo " *** Installation In Progress ***"
echo "To monitor progress type command (tail -f ${LOG_FILE}) "
Main >>${LOG_FILE} 2>&1
echo " *** Installation Completed ***"
echo "Eject CDROM before reboot"

exit 0
