#!/sbin/sh
#  ident "@(#)nhcgtp 1.18     04/01/19 SMI"
#
# Copyright 2002-2004 Sun Microsystems, Inc. All rights reserved.
#

# the script exists immediately if CMM.UseCGTP!=True

PROG=`basename $0`

NHCONFDIR=/etc/opt/SUNWcgha
NHAS_NOT_CONF_FILE=$NHCONFDIR/not_configured
USER_FILE=/etc/opt/SUNWcgha/nhfs.conf

AWK=/usr/bin/awk
IFCONFIG=/usr/sbin/ifconfig
ROUTE=/usr/sbin/route
GREP=/usr/bin/grep
PGREP=/usr/bin/pgrep
ECHO=/usr/bin/echo
NHGETCONF=/opt/SUNWcgha/sbin/nhgetconf
NHNETCFG=/opt/SUNWcgha/sbin/nhnetcfg

wait_for_dhcp () {

    # the code of this function is also present in nhcgtps
    # (CGTP for diskless standalone). Do not forget to make change in both
    # scripts
 
    # Get name of the network interfaces
    nic0=`$NHGETCONF -u file://$USER_FILE -s Node.Nic0`	\
    || error "Property \"Node.Nic0\" not found in $USER_FILE"

    nic1=`$NHGETCONF -u file://$USER_FILE -s Node.Nic1`	\
    || error "Property \"Node.Nic1\" not found in $USER_FILE"

    $ECHO "Waiting for DHCP interfaces to be configured\c"
    # wait until both interfaces has been configured
    nic0_done=0
    nic1_done=0
    loop=60
    while [ ${nic0_done} -eq 0 ] || [ ${nic1_done} -eq 0 ]
    do
	$ECHO ".\c"

	# check if the interfaces have been configured
	if [ ${nic0_done} -eq 0 ]
	then
	    result=`$IFCONFIG ${nic0} | $GREP -c 0.0.0.0`
	    if [ ${result} -eq 0 ]
	    then
		nic0_done=1
	    fi
	fi
	if [ ${nic1_done} -eq 0 ]
	then
	    result=`$IFCONFIG ${nic1} | $GREP -c 0.0.0.0`
	    if [ ${result} -eq 0 ]
	    then
		nic1_done=1
	    fi
	fi

	# if still not done, make some actions
	if [ ${nic0_done} -eq 0 ] || [ ${nic1_done} -eq 0 ]
	then
	    # check if dhcpagent is still there
	    result=`$PGREP -x dhcpagent`
	    if [ -z "${result}" ]
	    then
		    error "dhcpagent not present"
		    exit 1
	    fi

	    # do not loop forever
	    loop=`expr ${loop} - 1`
	    if [ ${loop} -le 0 ]
	    then
		error "Timeout: Interfaces for CGTP not initialized by DHCP"
	    fi

	    # wait before looping
	    sleep 1
	 else
	    $ECHO "Done."
	 fi
    done
}

diskless_conf () 
{ 
# For diskless nodes, get information from DHCP.
# BootSrvA symbol contains cgtp master dynamic address
# NhCgtpAddr symbol contains : local cgtp addr
# Subnet symbol contains cgtp netmask (assumption is made
# that cgtp netmask is identifical to primary nics one.

    # wait for the interface to be configured
    wait_for_dhcp

    local_cgtp_addr=`/sbin/dhcpinfo NhCgtpAddr 2> /dev/null`
    cgtp_netmask=`/sbin/dhcpinfo Subnet 2> /dev/null`
    master_cgtp=`/sbin/dhcpinfo BootSrvA 2> /dev/null`
    router=`/sbin/dhcpinfo Router 2> /dev/null`
    
    master_nic0=`$NHNETCFG nic0 masterdyn`				\
	|| error "Cannot retrieve master's dynamic NIC0 address"
    master_nic0=`echo ${master_nic0} | $AWK '{print$2}'`

    master_nic1=`$NHNETCFG nic1 masterdyn`				\
	|| error "Can't retrieve master's dynamic NIC1 address"
    master_nic1=`echo ${master_nic1} | $AWK '{print$2}'`

    # add a host route to the IP master address, cannot use setsrc
    # as soon as cgtp0 will be plumbed, we need a proper route (not
    # the default one)
    if [ "${router}" = "${master_nic0}" ]
    then
	#we used nic0 to boot
	$ROUTE add ${master_cgtp} ${master_nic0} -multirt
	cgtpintf_plumb ${local_cgtp_addr} 
	# now the CGTP address is configured, use setsrc
	cgtp_route_add ${master_nic1} 
	#this route is already partialy configured
	cgtp_route_update ${master_nic0}
    else
	#we used nic1 to boot
	$ROUTE add ${master_cgtp} ${master_nic1} -multirt
	cgtpintf_plumb ${local_cgtp_addr} 
	# now the CGTP address is configured, use setsrc
	cgtp_route_add ${master_nic0} 
	#this route is already partialy configured
	cgtp_route_update ${master_nic1}
    
    fi


    # disconnect TCP connections to the master
    # so that NFS re-opens it using new CGTP routing
    /opt/SUNWcgha/sbin/nhtcpdisc ${master_cgtp}
    
HOSTNAME=`/usr/bin/cat /etc/nodename`
/usr/bin/hostname $HOSTNAME

}

cgtp_route_update () 
{ 
    $ROUTE change ${master_cgtp} $1 -multirt -setsrc ${local_cgtp_addr}
}

cgtp_route_add ()
{
    $ROUTE add ${master_cgtp} $1 -multirt -setsrc ${local_cgtp_addr}
}

cgtpintf_plumb () 
{ 
    $IFCONFIG ${cgtp_ifname} plumb $1 netmask ${cgtp_netmask} broadcast + up
}

error ()
{
	echo "$PROG: ERROR: $1"
	echo "$PROG: Aborting. Cannot configure CGTP routes"
	exit 1
}


#
## SCRIPT STARTS HERE.
###


# Installation in progress test
if [ -f $NHAS_NOT_CONF_FILE ]; then
    exit 0
fi

# Sets TCP maximum retransmission delay to 1 second.
TCP_VALUE=1000
/usr/sbin/ndd -set /dev/tcp tcp_rexmit_interval_max $TCP_VALUE
if [ $? -eq 0 ]; then
    echo "$PROG: TCP maximum retransmission delay set to ${TCP_VALUE}ms"
fi

if [ "`$NHGETCONF -u file://$USER_FILE -s Node.UseCGTP | tr '[:lower:]' '[:upper:]'`" != "TRUE" ]; then
    exit 0
fi

# Get information about type of node and master node floating address
Node_type=`$NHGETCONF -u file://$USER_FILE -s Node.Type`	\
    || error "Property \"Node.type\" not found in $USER_FILE"

Node_type=`$NHGETCONF -u file://$USER_FILE -s Node.Type | tr '[:lower:]' '[:upper:]'`

# Get CGTP interface name
cgtp_ifname=`$NHGETCONF -u file://$USER_FILE -s Node.NICCGTP`		\
    || error "Property \"Node.NICCGTP\" not found in $USER_FILE"

case "${Node_type}" in
'DISKLESS')
    diskless_conf
    ;;

'DATALESS' | 'DISKFULL')
    # Check that CGTP interface exists
    $IFCONFIG ${cgtp_ifname} > /dev/null 2>&1				\
	|| error "Interface ${cgtp_ifname} not plumbed"

    # Get local cgtp address
    local_cgtp_addr=`$NHNETCFG cgtp local`				\
	|| error "Cannot retrieve local CGTP address"
    local_cgtp_addr=`echo ${local_cgtp_addr} | $AWK '{print$2}'`

    # Get CGTP network mask
    cgtp_netmask=`$NHNETCFG cgtp netmask`				\
	|| error "Cannot retrieve local CGTP address's netmask"
    cgtp_netmask=`echo ${cgtp_netmask} | $AWK '{print$2}'`

    # Get master dynamic cgtp address
    master_cgtp=`$NHNETCFG cgtp masterdyn`				\
	|| error "Cannot retrieve master's dynamic CGTP address"
    master_cgtp=`echo ${master_cgtp} | $AWK '{print$2}'`

    master_nic0=`$NHNETCFG nic0 masterdyn`				\
	|| error "Cannot retrieve master's dynamic NIC0 address"
    master_nic0=`echo ${master_nic0} | $AWK '{print$2}'`

    master_nic1=`$NHNETCFG nic1 masterdyn`				\
	|| error "Can't retrieve master's dynamic NIC1 address"
    master_nic1=`echo ${master_nic1} | $AWK '{print$2}'`

    cgtp_route_add ${master_nic0}
    cgtp_route_add ${master_nic1}
    ;;
*)
    echo "Wrong value for property \"Node.type\", should be \"Dataless\", \"Diskless\" or \"Diskfull\""
    exit 1
    ;;
esac

exit 0
