#!/bin/bash
###############################################################################
## spininstall                                                                #
###############################################################################
## Copyright 2002. Spinnaker Networks Inc.                                    #
## All rights reserved.                                                       #
##                                                                            #
###############################################################################
## This script is used during fresh software installation to generate the     #
## /etc/sysconfig/spinsoftware file                                           #
##                                                                            #
###############################################################################

#########
## 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/'`
}


#########
## 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=`rpm -q spinmgmt`
if [ -z ${MGMT_RPM} ]; then
    echo "Cannot find spinmgmt rpm; Cannot continue"
    exit 1
fi

T_VERSION=`rpm -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
NEW_MAJOR=${T_MAJOR}
NEW_MINOR=${T_MINOR}
NEW_PATCH=${T_PATCH}
NEW_BUILDNUMBER=${T_BUILDNUMBER}
EPOCH_INSTALL_TIME=`rpm -q spinmgmt --queryformat "%{INSTALLTIME}"`

#########
## Update the software version information on the alternate RootFileSystem 
#########
SW_FILE="/etc/sysconfig/spinsoftware"
INSTALLTIME=`perl -e "print scalar localtime ${EPOCH_INSTALL_TIME}"`
echo "MAJOR=${NEW_MAJOR}" > ${SW_FILE}
echo "MINOR=${NEW_MINOR}" >> ${SW_FILE}
echo "PATCH=${NEW_PATCH}" >> ${SW_FILE}
echo "BUILDNUMBER=${NEW_BUILDNUMBER}" >> ${SW_FILE}
echo "INSTALLTIME=${INSTALLTIME}" >> ${SW_FILE}

exit 0

