#! /sbin/sh
########
# Product: Ignite-UX
# Fileset: BOOT-KERNEL-PA
# configure
# @(#) $Revision: 10.2 $
########
#
# (c) Copyright Hewlett-Packard Company 2002
#
########################################################################

##########################################################################
# This script checks to see of the preinstall script found any user
# configurable information inside the RAM filesystem and if so, it
# will replace that information inside the new filesystem.
#
# If no user-defined information was in installed, this script will
# determine if this host can be the install server and will configure
# it as the default.  It will also use this host to find out the
# default netmask for the network.
#

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

# Print the max array index for the variable provided as $1
maxindex()
{
    typeset i=$(set | sed -n 's/^'$1'\[\([[:digit:]]\{1,\}\)\]=.*$/\1/p' \
     | tail -1)
    echo $i
}

if [[ -n $SW_DEFERRED_KERNBLD ]]
then
    echo "WARNING: Ignite-UX will not function from an NFS Diskless"
    echo "         client, although swlist(1M) will report the product"
    echo "         as installed."
    exit $WARNING
fi

INST_DIR=/opt/ignite
BOOT_DIR=${INST_DIR}/boot
TMP_CFG=${BOOT_DIR}/fs_cfg.old
DEF_CFG=${BOOT_DIR}/fs_cfg.def
INSTALLFS=${BOOT_DIR}/INSTALLFS
DATA_DIR=${INST_DIR}/data
CMDS=${DATA_DIR}/INSTCMDS

PATH=${INST_DIR}/bin:$PATH

if [ -f /usr/lib/sw/hpux.install/700INSTALL -o -f /usr/lbin/instl_bootd \
     -o -f /usr/sbin/instl_adm ]
then
    # Run Ignite-UX script to remove NetInstall/Patches
    ${SW_CONTROL_DIRECTORY}/../pfiles/preinstall
fi
#
# 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
binlink=/opt/ignite/bin
lbinlink=/opt/ignite/lbin

#
# do we have the instl_adm command? look in linked and native bin
# directories
# if not exit
#
have_instl_adm=0
if [[ -x ${binlink}/instl_adm ]];then
  instladm=${binlink}/instl_adm
  have_instl_adm=1
elif [[ -x ${bindir}/instl_adm ]];then
  instladm=${bindir}/instl_adm
  have_instl_adm=1
fi

#
# if no instl_adm exit
if (( !  have_instl_adm ));then
  echo "\
WARNING: instl_adm not found on the system, configuration information
         not modified."
  exit ${WARNING}
fi

if [ -s $TMP_CFG ]
then
    ${instladm} -F $INSTALLFS -f $TMP_CFG
    if [ "$?" != 0 ]
    then
        echo "\
WARNING: instl_adm failed to restore the old cold-install configuration.
         Old configuration can be found in: $TMP_CFG"
        exit $WARNING
    else
        rm -f $TMP_CFG
    fi
else
    # If the machine is IA and BOOT-KERNEL-IA is also loaded in this session
    # then do not do the following here. Just exit with SUCCESS.

    #
    # The ram-fs has no configuration info in it.  So
    # fill in as much as possible given what we know about the
    # server:
    #    - if -f $CMDS, then server=`hostname`
    #    - netmask=our-netmask
    #    - gateway=our-gateway (if gateway != our IP address)

    ia_opts=""
    #
    # Find out which interfaces are configured.  Cannot rely
    # on this script running while they are up however because it
    # can be running after a reboot in single-user state via swconfig
    # (See JAGaa86074).  Source in netconf and pick one that exits
    # in lanscan output so we don't use a bogus netconf entry.
    #
    typeset -i num_ifaces=0
    typeset -i best_iface_idx=-1
    if [ -f /etc/rc.config.d/netconf ] ; then
        . /etc/rc.config.d/netconf

        typeset -i inum=0
        typeset -i maxidx=$(maxindex INTERFACE_NAME)
        while [ $inum -le $maxidx ]
        do
            iface="${INTERFACE_NAME[$inum]}"
            if [ -n "$iface" -a -n "${IP_ADDRESS[$inum]}" ] ; then
                # if the iface does not contain a ":", or if it does
                # it is ":0" then it is the first IP address for that
                # iface, ignore all else for systems that multiple
                # IP's per iface
                if [ "${iface%%*:*}" != "" -o "${iface%%*:0}" = "" ] ; then
                    ifaces[$num_ifaces]="${iface%*:*}"
                    netmasks[$num_ifaces]="${SUBNET_MASK[$inum]}"
                    IPs[$num_ifaces]="${IP_ADDRESS[$inum]}"
                    ((num_ifaces += 1))
                fi
            fi
            ((inum += 1))
        done
        
        for iface in $(lanscan -i 2>/dev/null |  awk '{print $1}') ""
        do
            inum=0
            while [ $inum  -lt $num_ifaces ]
            do
                if [ "${ifaces[$inum]}" = "$iface" ] ; then
                    best_iface_idx=$inum
                fi
                ((inum += 1))
            done
            if [ $best_iface_idx -ge 0 ] ; then
                break;
            fi
        done
    fi

    if [ $num_ifaces = 0 ] ; then
        if [ -f /tmp/install.vars ] ; then
            # Use what is available from IUX, there is a chance that
            # this maybe temporary information, but probably small.
            . /tmp/install.vars
            if [ -n "$INST_HOST_IP" ] ; then
                ifaces[0]="$INST_LAN_DEV"
                netmasks[0]="$INST_NETMASK"
                IPs[0]="$INST_HOST_IP"
                best_iface_idx=0
                num_ifaces=1
            fi
        fi
    fi

    if [ $num_ifaces = 0 -o $best_iface_idx -eq -1 ]
    then
        echo "\
WARNING: There are no network interfaces configured at this time. The
         default networking parameters cannot be configured for this
         Ignite-UX server.  Please use the instl_adm(1M) command to
         set the server's IP address (-t), netmask (-m) and default
         gateway (-g) as desired.  You may instead, re-run this configure
         script after networking is configured using the command:
         \"/usr/sbin/swconfig -xreconfigure=true Ignite-UX.BOOT-KERNEL-PA\""
        exit $WARNING
    fi
    
    if [ -f $CMDS -a  -n "${IPs[$best_iface_idx]}" ]
    then
        ia_opts="-t ${IPs[$best_iface_idx]} $ia_opts"
    fi

    if [ -n "${netmasks[$best_iface_idx]}" ]
    then
        ia_opts="-m ${netmasks[$best_iface_idx]} $ia_opts"
    fi
        
    #
    # Determine the default route.  Again, we cannot rely on
    # the current settings from netstat since we could be running
    # in single user state, so read it out of the netconf that was
    # sourced in.
    #
    # To fix FSDdt23231 we must look at all our IPs to make sure
    # that the default route does not match any of them.  Also
    # only consider routes that are for the interface we picked
    # for the IP and netmask.
    #
    route=""
    inum=0
    maxidx=$(maxindex ROUTE_DESTINATION)
    while [ $inum -le $maxidx ]
    do
        if [ "${ROUTE_DESTINATION[$inum]}" = "default" -a \
            -n "${ROUTE_GATEWAY[$inum]}" ] ; then
            is_local=false
            for ip in ${IPs[*]}
            do
                if [ $ip = "${ROUTE_GATEWAY[$inum]}" ]
                then
                    is_local=true
                fi
            done
            if [ $is_local = false ]
            then
                route="${ROUTE_GATEWAY[$inum]}"
                break; # Keep the first non-local route
            fi
        fi
        ((inum += 1))
    done
    
    if [ -z "$route" -a -n "$INST_DEFAULT_ROUTE" ]
    then
        # Use the INST_DEFAULT_ROUTE that may have been sourced in
        # from /tmp/install.vars
        route="$INST_DEFAULT_ROUTE"
    fi

    if [ -n "$route" ]
    then
        ia_opts="-g $route $ia_opts"
    fi

    if [ -n "$ia_opts" ]
    then
        ${instladm} -F $INSTALLFS $ia_opts && \
        ${instladm} -F $INSTALLFS -d > $DEF_CFG && \
        IPD_addfile $DEF_CFG
        if [ $num_ifaces -gt 1 ]
        then
            echo "\
NOTE:    Because this system has multiple LAN interfaces configured, you
         should verify that the default Ignite-UX server IP address, network
         route, and netmask were chosen correctly by running the
         \"/opt/ignite/bin/instl_adm -d\" command."
        fi
    fi
fi

exit $SUCCESS
