#! /sbin/sh
########
# Product: Ignite-UX
# preinstall
# @(#) $Revision: 10.22 $
########
#
# (c) Copyright Hewlett-Packard Company 1997
#
########################################################################

##########################################################################
# This is the product level preinstall script for Ignite-UX.
# It is responsible for cleaning up the old HPUX-Install product
# which is being overwritten with this.  Plus, any 'old' Ignite-UX
# patches that are obsolete as of the latest release, and various
# other obsolete things.

UTILS=/usr/lbin/sw/control_utils
if [ -f $UTILS ]
then
    . $UTILS
else
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

#
# 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
  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

########
# 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))}'
}

ETC_DIR=/etc/opt/ignite

NETINST_PATCHES="
 PHCO_19778
 PHCO_17585
 PHCO_16612
 PHCO_14984
 PHCO_14983
 PHCO_14734
 PHCO_13853
 PHCO_13020
 PHSS_11022
 PHCO_10751
 PHSS_9906
 PHCO_8480
 PHCO_8014
"

# Only one of the following patches might be on the system, the
# first was for the A version, the second for the B version.
IUX_PATCHES="
 PHSS_21827
 PHSS_21828
"

if [ -f /etc/instl_bootab -a ! -f $ETC_DIR/instl_boottab ]
then
    cond_mkdir 555 bin bin $ETC_DIR
    # Save instl_boottab before removing the product
    cp_retain /etc/instl_boottab $ETC_DIR/instl_boottab
fi

# to prevent parsing problems from verbose debug output
# unset any SDU_DEBUG flags

set -A debug_vars $(set|awk '$0 ~ /^SDU_DEBUG/ {FS="=";print $1}')
[[ -n ${debug_vars[*]} ]] && unset ${debug_vars[*]}
unset debug_vars 

if [ -f /usr/lib/sw/hpux.install/700INSTALL -o -f /usr/lbin/instl_bootd \
     -o -f /usr/sbin/instl_adm ]
then
    /usr/sbin/instl_adm -d > /usr/lib/sw/hpux.install/config.fs 2>/dev/null
    if [ "$?" = 0 -a -s /usr/lib/sw/hpux.install/config.fs ] ; then
        echo "\
NOTE:    Old instl_adm defaults are saved in the file
         \"/usr/lib/sw/hpux.install/config.fs\". They will NOT be automatically
         applied to Ignite-UX but may be useful for latter reference."
     fi

     # Remove HPUX-Install, but first remove any patches that were
     # for that product, or else removing them later will put back
     # obsolete commands.
     for prod in $NETINST_PATCHES HPUX-Install
     do
         swlist $prod >/tmp/$$.swlist 2>&1
         if $(grep -q "not found" /tmp/$$.swlist) ; then
             : # product is not on the system.
         else
             echo "\
       * Removing the obsoleted $prod product/patch."
             
             swremove $prod > /dev/null
         fi
         rm -f /tmp/$$.swlist
     done
fi

# Check again that the old instl_adm has been removed, if not remove
# it for sure here, or else it may get accidently used by the user
rm -f /usr/sbin/instl_adm

#
# Clean up old bundle names that this product used to called
# if it looks like they may have been loaded
#
for dir in /var/adm/sw/products/NetInstall*
do
   if [ -d $dir ] ; then
        # Just remove the bundle containers
        echo "\
       * Removing old NetInstall bundles."
        swremove NetInstall. NetInstallPast. > /dev/null 2>&1
        break;
   fi
done

#
# For those that received beta copies of Ignite-UX (pre 0.34), then
# there was an /opt/ignite/data/INDEX file that is obsolete now.
# If that file has configurations in it, then move them over
# to the /var/opt/ignite/INDEX file and give a message here and
# in the INDEX file.
#
OPT_INDEX=/opt/ignite/data/INDEX
VAR_INDEX=/var/opt/ignite/INDEX
if [ -f $OPT_INDEX ] && \
    grep -q "[^#]*cfg[ \t]\"" $OPT_INDEX
then
    #
    # Add the data from /opt/ignite/data/INDEX to the front of /var/../INDEX
    # The contents of the var index had precedence and must come last.
    #
    if [ -f $VAR_INDEX ]
    then
        cp $VAR_INDEX /tmp/idx.$$
        cat $OPT_INDEX /tmp/idx.$$ > $VAR_INDEX
        rm -f /tmp/idx.$$
    else
        cp -p $OPT_INDEX $VAR_INDEX
    fi

    echo "\
# NOTE:    The file: $OPT_INDEX has been obsoleted, the contents
#          of this file have been moved to $VAR_INDEX" > $OPT_INDEX

    echo "\
NOTE:    The file: $OPT_INDEX has been obsoleted, the contents
         of this file have been moved to $VAR_INDEX"
fi

#
# Obsolete the 10.30 HP-UX release.  This involves removing the
# fileset and bundle, and INDEX entries that reference 10.30.
#
if [ -f /opt/ignite/data/Rel_B.10.30/SYSCMDS ] ; then
    #
    # determine the architecture we are running on and set the location of
    # native binaries to link to /opt/ignite/bin and /opt/ignite/lbin
    ############
    case $(uname -m) in
        ia64*) bindir=/opt/ignite/binia
               lbindir=/opt/ignite/lbinia
               ;;
            *) bindir=/opt/ignite/binpa
               lbindir=/opt/ignite/lbinpa
               ;;
    esac
    echo "\
       * Removing support for HP-UX 10.30.  The Ignite-UX-10-30 bundle and
         the Ignite-UX.FILE-SRV-10-30 fileset will be removed."

    if swlist -l fileset Ignite-UX.FILE-SRV-10-30 2>/dev/null |
        grep -q 'Ignite-UX.FILE-SRV-10-30[[:space:]]'
    then
        swremove Ignite-UX.FILE-SRV-10-30
    fi
    if swlist -l bundle Ignite-UX-10-30 2>/dev/null |
        grep -q 'Ignite-UX-10-30[[:space:]]'
    then
        swremove Ignite-UX-10-30.
    fi
    
    if fgrep -q B.10.30 $VAR_INDEX
    then
        echo "\
NOTE:    All 10.30 related entries in $VAR_INDEX will be removed
         because as of this release of Ignite-UX, 10.30 is no longer
         supported.  A backup copy of $VAR_INDEX will be saved
         as ${VAR_INDEX}_B.10.30"
        cp -p ${VAR_INDEX} ${VAR_INDEX}_B.10.30
        # Note this assume manage_index from the prior rev of IUX
        # is on the system and operational, which it should be.
        if [[ -x /opt/ignite/bin/manage_index ]]; then
            /opt/ignite/bin/manage_index -d -r B.10.30
        elif [[ -x ${bindir}/manage_index ]]; then
           ${bindir}/manage_index -d -r B.10.30
        else
           echo "\
NOTE:    manage_index command not found, 10.30 INDEX entries not removed."
        fi
    fi
fi

# Check for and remove any obsolete IUX patches.
for prod in $IUX_PATCHES
do
    swlist $prod >/tmp/$$.swlist 2>&1
    if $(grep -q "not found" /tmp/$$.swlist) ; then
        : # product is not on the system.
    else
        echo "\
       * Removing the obsoleted $prod patch."
             
        swremove $prod > /dev/null
    fi
    rm -f /tmp/$$.swlist
done

# Finally, remove Ignite-UX-SUPPL, since it was temporary for
# 9804 ACE/HWE.  Support is now in the mainstream for these
# patch releases.

if swlist -l product Ignite-UX-SUPPL 2>/dev/null |
   grep -q 'Ignite-UX-SUPPL[[:space:]]'
then
   swremove Ignite-UX-SUPPL
fi

#
# If this isn't the "A" version then remove the obsoleted 10.01 and 10.10
# bundles as well as the "A" version CPL bundle
#
iuxRevLtr=$(GetRevLtr $SW_SOFTWARE_SPEC)
iuxRevMjr=$(GetRev $SW_SOFTWARE_SPEC | awk '{FS=".";print $1"."$2}')
                      

if [[ ${iuxRevLtr} != "A" ]];then
   typeset rmList=""
   typeset rmBlist=""
   typeset there=""

   #
   # remove bundles and filesets of obsoleted 10.01 and 10.10
   # if this is B.3 or later
   #
   if [[ ${iuxRevMjr} > "B.3" || ${iuxRevMjr} = "B.3" ]]; then
       for i in Ignite-UX-10-01 Ignite-UX-10-10 \
                Ignite-UX.FILE-SRV-10-01 Ignite-UX.FILE-SRV-10-10
       {
          there=$(get_sw_spec ${i})
          [[ -n ${there} ]] && rmList="${rmList} ${i}"
          there=""
       }
       [[ -n ${rmList} ]] && swremove ${rmList}
   fi
   #
   # determine if the A version CPL bundle is there
   # if so swmodify it off
   #
   rmBlist=$(swlist -lbundle|grep B572[45]AA_APZ|awk '{print $1}')
   [[ -n ${rmBlist} ]] && swmodify -u ${rmBlist} 2>/dev/null

fi # end if not rev A
exit 0
