#! /sbin/sh
########
# Product: Ignite-UX
# Fileset: FILE-SRV-*
# preinstall
# @(#) $Revision: 10.4 $
########
#
########################################################################
#
# (c) Copyright Hewlett-Packard Company 2000
#
########

    UTILS="/usr/lbin/sw/control_utils"
    if [[ ! -f $UTILS ]]
    then
        echo "ERROR: Cannot find $UTILS"
        exit 1
    fi
    . $UTILS
    exitval=$SUCCESS
################################################################################
# FUNCTIONS
########
#
# check for the get_sw_spec funtion
# if we got it from control_utils great, otherwise roll our own
#
if typeset +f|grep -qx get_sw_spec
then
  :
else
  # copied from 11i control_utils
  get_sw_spec(){
    typeset pfs=${1%%,*}
    typeset spec=""

    # A debug value messes up 'swlist' output, so unset it for this function.
    if [[ -n $SDU_DEBUG ]]
    then
        typeset SDU_DEBUG_SAVED=$SDU_DEBUG
        unset SDU_DEBUG
    fi

    set -- $(swlist -l fileset -a software_spec $pfs 2>/dev/null)
    while [[ $# -gt 1 ]]
    do
        [[ $1 = $pfs ]] && spec=$2 && break
        shift
    done
    print -u1 $spec

    [[ -n $SDU_DEBUG_SAVED ]] && SDU_DEBUG=$SDU_DEBUG_SAVED

  }
fi
#
# check for the get_fileset_name funtion
# if we got it from control_utils great, otherwise roll our own
#
if typeset +f|grep -qx get_fileset_name
then
  :
else
  # copied from 11i control_utils
    get_fileset_name ()
    {
        typeset spec

        case $# in
        0)  ( IFS="," ; set -- $SW_SOFTWARE_SPEC ; print ${1##*.})
                ;;
        1)  spec=$1
                ( IFS="," ; set -- $spec ; print ${1##*.})
                ;;
        *)  print ""
            return $ERROR
            ;;
        esac
    }
fi

# GetRev
# args: software spec
GetRev(){
    echo ${1} |
         awk '{cnt=split($0,ary,",")
            for(i=1;i<=cnt;i++)
              if( ary[i] ~ /^r?=/)
                printf("%s", substr(ary[i],3))}'
}
########
# GetRevLtr
# args: revision field of software spec
GetRevLtr(){
    GetRev ${1} |
         awk '{printf("%s", substr($0,1,1))}'
}

##############################################################################
# MAIN
#    
#

# 
# If the bundle wrapper for OS support by IUX was previously installed and
# now only the fileset from the new release of IUX is being installed
# remove the old bundle wrapper
# Example
#   Ignite-UX-10-20,r=A.2.4.307 bundle is on the system and we are installing
#   the CPL bundle B5725AA which contains Ignite-UX.FILE-SRV-10.20. If we
#   don't remove the bundle wrapper Ignite-UX-10-20 it will remain on the
#   system and give confusing swlist results. swlist would show the "A" 
#   version 10.20 bundle but the underlying filesets would be the "B" version
#
typeset bundleSWspec=""
typeset -i doSwmodify=0
iuxRevLtr=$(GetRevLtr $SW_SOFTWARE_SPEC)

OSpart=$(get_fileset_name|awk '{FS="-";print $3"-"$4}')

# IA and PA were added later. Catch this here. For example fileset
# FILE-SRV-11-22IA goes with Ignite-IA-11-22 bundle. 

if [ ${OSpart%%PA} != ${OSpart} ]
then
    bundleSWspec=$(get_sw_spec Ignite-PA-${OSpart%%IA})
elif [ ${OSpart%%IA} != ${OSpart} ]
then
    bundleSWspec=$(get_sw_spec Ignite-IA-${OSpart%%IA})
else
    bundleSWspec=$(get_sw_spec Ignite-UX-${OSpart})
fi

if [[ -n ${bundleSWspec} ]];then #we found a bundle for this OS fileset

    bundleIuxRevLtr=$(GetRevLtr ${bundleSWspec}) 

    if [[ ${iuxRevLtr} != ${bundleIuxRevLtr} ]];then
       doSwmodify=1              
    elif [[ ${iuxRevLtr} = ${bundleIuxRevLtr} ]];then
         [[ $(GetRev $SW_SOFTWARE_SPEC) > $(GetRev ${bundleSWspec}) ]] && \
                  doSwmodify=1
    fi 
       #
       # remove the old bundle wrapper
       #
    (( doSwmodify )) && swmodify -u ${bundleSWspec} 2>/dev/null
fi
exit 0
