#! /sbin/sh
########
# Product: Ignite-UX
# Fileset: BOOT-SERVICES
# configure
# @(#) $Revision: 10.26 $
########
#
# (c) Copyright Hewlett-Packard Company 1994
#
########################################################################

##########################################################################
# This script does the following:
#
# - sets up the system to allow tftp access to the directory
#   containing the cold-install boot-lif, kernel and cmds.hppa files.
#
# - installs the new /opt/ignite/etc/instl_boottab
#
# - Adds the instl_bootd line to inetd.conf
# 
# - Adds the example of entries for IPF clients to /etc/bootptab
#
# - Warns if inetd.sec looks like it may not allow 0.0.0.0
#
# - If /usr/sbin/rbootd is found on the system, configures rbootd to
#   start at boot time, and starts it now. This command was deleted in
#   11.23.
#

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

INST_DIR=/opt/ignite
VAR_DIR=/var/opt/ignite

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."

    if grep -q instl_bootd /etc/inetd.conf 2>/dev/null
    then
        echo "\
NOTE:    Removing instl_bootd services from /etc/inetd.conf (not supported
         on diskless client)."
        grep -v instl_bootd /etc/inetd.conf > /tmp/inetd.$$
        cp /tmp/inetd.$$ /etc/inetd.conf
        chmog 444 bin bin /etc/inetd.conf
        rm -f /tmp/inetd.$$
    fi
    exit $WARNING
fi

retval=$SUCCESS

if [ ! -f /etc/inetd.conf ] ; then
    # This configure script must be running before the INETD script,
    # so just go ahead and copy in the inetd.conf file so that we
    # don't end up creating it without the default stuff.
    if [ ! -f /usr/newconfig/etc/inetd.conf ] ; then
        echo "\
ERROR:   Cannot find /usr/newconfig/etc/inetd.conf to copy into place.
         Ignite-UX cannot configure booting services"
        exit $FAILURE
    else
        echo "\
       * Installing /etc/inetd.conf"
        cp_retain /usr/newconfig/etc/inetd.conf /etc/inetd.conf
    fi
fi

for dir in $INST_DIR $VAR_DIR
do
    setup_tftp $dir
    if [ "$?" != 0 ]
    then
        echo "\
ERROR:   The setup_tftp command returned a failure.  It may not be possible
         to use this system as an install server until this problem is fixed
         and tftp access is allowed to the directory: $dir"
        retval=$FAILURE
    fi

    # JAGae74224 fix. Give inetd some time to handle the change between
    # the calls to setup_tftp.
    if [ "$dir" = "$INST_DIR" ]
    then
        sleep 5
    fi
done

#
# Determine which version of instl_bootd should be enabled on the system.
# We ship a version built on 10.01 and on build on the current release.
# If the system is running 10.01/10.10/10.20 then use the .01 (10.01) version
# The .nw (new) version only works on 10.30 (and later?)
#
old_instl_bootd=instl_bootd.01
cur_instl_bootd=instl_bootd.nw
case $(uname -r) in
    *10.10)
        to_use="$old_instl_bootd";;
    *10.01)
        to_use="$old_instl_bootd";;
    *10.20)
        to_use="$old_instl_bootd";;
    *)
        to_use="$cur_instl_bootd";;
esac
#
# 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

rm -f ${lbindir}/instl_bootd
if [ -f ${lbindir}/instl_bootd ] ; then
    # file must be busy - move it aside
    mv ${lbindir}/instl_bootd ${lbindir}/#instl_bootd
fi
ln -s $to_use ${lbindir}/instl_bootd
IPD_addfile -N ${lbindir}/instl_bootd

#
# Copy in the new instl_boottab unless one already exists
# The location has moved for 10.30, so move the old file to
# the new location before the cp newconfig.
# Not using newconfig_cp here because I don't believe it
# can deal with the "opt" situation.
#
if [ -f /etc/instl_boottab -a ! -f /etc/opt/ignite/instl_boottab ] ; then
    cp_retain /etc/instl_boottab /etc/opt/ignite/instl_boottab
    rm -f /etc/instl_boottab
fi
if [ ! -f /etc/opt/ignite/instl_boottab ] ; then
    cp_retain /opt/ignite/newconfig/etc/opt/ignite/instl_boottab /etc/opt/ignite/instl_boottab
fi

#
# Add (or modify) the line to inetd to enable the instl_bootd service
#
old=/etc/instl_bootd
old2=/usr/lbin/instl_bootd
new=/opt/ignite/lbin/instl_bootd

inetd_line="instl_boots  dgram  udp wait   root $new   instl_bootd"

if grep -q $new /etc/inetd.conf 2>/dev/null
then
    :
else
    if [ ! -f /etc/inetd.conf -a -f /usr/newconfig/etc/inetd.conf ]
    then
        # We could not declare a prerequisite on INETD filest (due to
        # integration problems with fileset revs), so just copy in
        # inetd.conf now.
        cp_retain /usr/newconfig/etc/inetd.conf /etc/inetd.conf
    fi
    if [ -f /etc/inetd.conf ]
    then
        if grep -q $old /etc/inetd.conf 2>/dev/null
        then
            sed '\X'$old'XsX'$old'X'$new'X' /etc/inetd.conf > /tmp/inetd.$$
            cp /tmp/inetd.$$ /etc/inetd.conf
            chmog 444 bin bin /etc/inetd.conf
            rm -f /tmp/inetd.$$
        elif grep -q $old2 /etc/inetd.conf 2>/dev/null
        then
            sed '\X'$old2'XsX'$old2'X'$new'X' /etc/inetd.conf > /tmp/inetd.$$
            cp /tmp/inetd.$$ /etc/inetd.conf
            chmog 444 bin bin /etc/inetd.conf
            rm -f /tmp/inetd.$$
        else
            echo $inetd_line >> /etc/inetd.conf
        fi

        inetd -c > /dev/null 2>&1
    else
        echo "\
WARNING: The file /etc/inetd.conf could not be found.  The line:
          $inetd_line
          Could not be appended to the file"
        [ "$retval" != $FAILURE ] && retval=$WARNING
    fi
fi

#
# Add the example of entries for IPF clients to /etc/bootptab
#
if [ -f /opt/ignite/boot/nbp.efi ]; then
    bootfile_size=`ls -l /opt/ignite/boot/nbp.efi | awk '{print $5/512}'`

    common_entry="ignite-defaults:\\
	ht=ethernet:\\
	hn:\\
	bf=/opt/ignite/boot/nbp.efi:\\
	bs=$bootfile_size"
    example_entry="System-IPF:\\
	tc=ignite-defaults:\\
	ha=00d009000000:\\
	ip=190.40.101.22:\\
	sm=255.255.248.0:\\
	gw=190.1.48.1:\\
	ds=190.1.48.11"
    first_entry="ignite-defaults:"

    if [ -f /etc/bootptab ]; then
        if grep -q $first_entry /etc/bootptab 2>/dev/null
        then
	    #Get the previous bootfile size to compare
	    prev_bs=$(cat /etc/bootptab | awk 'BEGIN {found=0}
	                                       /^'$first_entry'/{found=1}
					       {
					          if (found == 1 && $1 ~ "bs=[0-9]*")
                                                  {
                                                     split ($1, size, "[=:\\]")
                                                     print size[2]
                                                     found=0
						  }
                                                }')
	    if [ $prev_bs -ne $bootfile_size ]
	    then
	        #The previous bootfile size is different,so replaced by new one.
	        sed 's/bs\='$prev_bs'/bs\='$bootfile_size'/' /etc/bootptab > /tmp/bootptab.$$
		cp /tmp/bootptab.$$ /etc/bootptab
		chmog 444 bin bin /etc/bootptab
	    fi
	else
	    echo "\n"             >> /etc/bootptab
            echo "$common_entry"  >> /etc/bootptab
	    echo "$example_entry" >> /etc/bootptab
        fi
    else
        echo "\
WARNING: The /etc/bootptab could not be found. The Ignite-UX example entries
         cannot be added."
        [ "$retval" != $FAILURE ] && retval=$WARNING
    fi
fi

   
#
# Check for instl_boots lines in inetd.sec that appear to not allow
# 0.0.0.0 (the broadcast address)
#
if [ -f /var/adm/inetd.sec ] ; then
    #
    # Check for lines that have instl_boots but are not lines allowing
    # the broadcast. The awk cmd is used to collapse continued lines
    # that end in \, so that grep can work on the whole line.
    #
    if awk '{ if (sub(/\\$/, "")) printf "%s", $0; else print $0}' /var/adm/inetd.sec | \
         grep -v "^instl_boots.*allow.*0\.0\.0\.0" | \
         grep -q -e "^instl_boots.*allow" -e "^instl_boots.*deny.*0\.0\.0\.0"
    then
        echo "\
WARNING: The file /var/adm/inetd.sec appears to have an entry for the
         "instl_boots" service that may deny (or not allow) access
         to the broadcast IP address: \"0.0.0.0\".  If this is the
         case, then clients will not be able to boot from this Ignite-UX
         server until inetd.sec is modifed.  Consider removing any 
         \"deny 0.0.0.0\" lines and adding:
             instl_boots allow 0.0.0.0"
        [ "$retval" != $FAILURE ] && retval=$WARNING
    fi
fi

#
# JAGae51330 fix. Early exit if /usr/sbin/rbootd not found.
#
if [ ! -x /usr/sbin/rbootd ]
then
    exit $retval
fi

#
# Modify the netdaemons file so that rbootd is started at system bootup.
#
NETDAEMONS=/etc/rc.config.d/netdaemons
if [ ! -f $NETDAEMONS -a -f /usr/newconfig$NETDAEMONS ]
then
    cp_retain /usr/newconfig$NETDAEMONS $NETDAEMONS
fi

if [ -f $NETDAEMONS ]
then
    ch_rc -a -pSTART_RBOOTD=1 $NETDAEMONS

    if [ "$?" != 0 ]
    then
        echo "\
WARNING: Was unable to change the /etc/rc.conf.d/netdaemons file in order
         to set START_RBOOTD=1.  The rbootd services may not be restarted
         at the next system boot."
        [ "$retval" != $FAILURE ] && retval=$WARNING
    fi
else
    echo "\
WARNING: The file $NETDAEMONS could not be found.  The
         variable START_RBOOTD could not be set to 1.  Network boot
         services may not be restarted the next time the system reboots."
    [ "$retval" != $FAILURE ] && retval=$WARNING
fi

#
# If rbootd is currently not started, then start it now.
#
if ps -e | grep -q rbootd
then
    :
else
    # The init.d script runs it in real-time priority.  Not sure
    # that is really needed for non-diskless clients...
    /usr/sbin/rbootd    
fi

exit $retval
