#! /sbin/sh
########
# Product: Ignite-UX
# configure
# @(#) $Revision: 10.15 $
########
#
# (c) Copyright Hewlett-Packard Company 1999
#
########################################################################

#
# This script is used to check that the product was constantly
# installed and that all the fileset revisions are the same, which
# is required for proper operation.
#

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

# Ensure the SD defaults are what we expect.  Note that sd_opts is
# a symlink to a release specfic sd_opts file.  This link is not
# in place yet when this script runs, so just use the 10.01 version
# which has all the basics that we care about here.
SD_OPTS="-X /opt/ignite/lib/sd_opts.10.01"

exit_val=$SUCCESS

# Note this function is similar to the one in check_version cmd, however
# here the SD IPD is in transition so when updating you will see both
# the old and the new rev of the filesets.  So it must accept that,
# and if the new version exists, then ignore the old ones.
check_fileset_revs()
{
    ret=0
    num_bad=0
    num_good=0

    my_vers="$(cat /opt/ignite/Version)"

    swlist $SD_OPTS -a revision -l fileset Ignite-UX | awk '{if ($1 != "#" && $1 != "" ) print $1 " " $2}' |
    while read fst rev
    do
        if [ "$rev" != "$my_vers" ] ; then
            bad_fsts[$num_bad]=$fst
            bad_revs[$num_bad]=$rev
            ((num_bad += 1))
        else
            good_fsts[$num_good]=$fst
            ((num_good += 1))
        fi
        
    done

    idx=0
    while [ $idx -lt $num_bad ]
    do
        # Check to see if the bad fileset is also in the good list, in which
        # case it is okay, since it probably just got updated but is still
        # temporarily in the IPD.
        is_good=false
        for fst in ${good_fsts[*]}
        do
            if [ ${bad_fsts[$idx]} = $fst ] ; then
                is_good=true
            fi
        done
        if [ $is_good = false ] ; then
            echo "\
WARNING: Fileset: \"${bad_fsts[$idx]}\" (rev=${bad_revs[$idx]}) is a different
         version than the rest of Ignite-UX (rev=$my_vers).  You must
         update all filesets of the product to the same version for
         proper behavior."
            ret=1
        fi
        ((idx += 1))
    done
    return $ret
}

# 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 

check_fileset_revs
if [ $? != 0 ] ; then
    # Exit with a warning, exiting with $FAILURE causes the filesets
    # to not be configured.
    exit_val=$WARNING
fi

get_rev()
{
    SWSPEC=$1
    (
       IFS=,
       set -- $SW_SOFTWARE_SPEC
       r=${3:-""}
       IFS="="
       set -- $r
       print ${2:-""}
    )
}
#
# Check to see if the product is a different rev than the
# IUX-Recovery wrapper - in which case we should remove the
# wrapper since the user probably loaded the product from
# the distribution depot and not a recovery_cmds depot.
#

myrev=$(get_rev $SW_SOFTWARE_SPEC)
swlist $SD_OPTS -a revision -l bundle IUX-Recovery 2>/dev/null | \
   awk 'BEGIN { ismatch=0; rev="none" }
        {
          if ($1 == "IUX-Recovery")
          {
              if ( $2 == "'$myrev'")
                  ismatch=1
              else
                  rev=$2
          }
        } END { print rev " " ismatch }' | read bun_rev ismatch

#
# If the IUX-Recovery bundles on the system do not match the version
# we are loading then remove the old one - (note that both the new and
# old bundle maybe on the system at the time this is running until SD
# removes the replaced bundle later).
#
if [ $ismatch -eq 0 ] ; then
    if [ "$bun_rev" != "none" ] ; then
        swremove $SD_OPTS IUX-Recovery.,r=$bun_rev
    fi
fi

exit $exit_val
