#!/sbin/sh
#  ident "@(#)nhcgtps 1.0     03/01/06 SMI"
#
# Copyright 2003-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

# 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

wait_for_dhcp () {

    # the code of this function is also present in nhcgtp
    # (CGTP for NHAS diskless). 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 
    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

    # create brodcast CGTP routing
    cgtp_broadcast=`$NHNETCFG cgtp broadcast | awk '{print$2}'`  
    nic0_broadcast=`$NHNETCFG nic0 broadcast | awk '{print$2}'`  
    nic1_broadcast=`$NHNETCFG nic1 broadcast | awk '{print$2}'`  
    $ROUTE add ${cgtp_broadcast} ${nic0_broadcast} -multirt -setsrc ${local_cgtp_addr}
    $ROUTE add ${cgtp_broadcast} ${nic1_broadcast} -multirt -setsrc ${local_cgtp_addr}

    # disconnect TCP connections to the master
    # so that NFS re-open it using new CGTP routing
    /opt/SUNWcgha/sbin/tcpdiscs ${master_cgtp}
}


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
    $IFCONFIG ${cgtp_ifname} $1 netmask ${cgtp_netmask} broadcast + up
}


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

# Check the configuration has an entry for Node Type.
Node_type=`$NHGETCONF -u file://$USER_FILE -s Node.Type`
if [ $? != 0 ]
then
    echo "Property \"Node.type\" not found in $USER_FILE"
    echo "Can't configure CGTP routes"
    exit 1 
fi

# Get information about type of node and master node floating address
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`
if [ $? != 0 ]
then
    echo "Property \"Node.NICCGTP\" not found in $USER_FILE"
    echo "Can't configure CGTP routes"
    exit 1 
fi


case "${Node_type}" in
'DISKLESS')
        diskless_conf
        ;;
*)
        echo "Wrong value for property \"Node.type\", should be \"Diskless\""
        exit 1
        ;;
esac
exit 0

