#!/sbin/sh
#  ident "@(#)nhcgtp 1.13     02/11/13 SMI"
#
# Copyright 2002 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

# 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


USER_FILE=/etc/opt/SUNWcgha/nhfs.conf


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.
    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`
    
    # 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-open it using new CGTP routing
    /opt/SUNWcgha/sbin/nhtcpdisc ${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 [ "`/opt/SUNWcgha/sbin/nhgetconf -u file://$USER_FILE -s Node.UseCGTP`" != "True" ]
then
    exit 0
fi

# Get information about type of node and master node floating address
Node_type=`/opt/SUNWcgha/sbin/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 CGTP interface name
cgtp_ifname=`/opt/SUNWcgha/sbin/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

# Get local cgtp address.
local_cgtp_addr=`/opt/SUNWcgha/sbin/nhnetcfg cgtp local | awk '{print$2}'`

# Get CGTP network mask
cgtp_netmask=`/opt/SUNWcgha/sbin/nhnetcfg cgtp netmask | awk '{print$2}'`

# Get master dynamic cgtp address.
master_cgtp=`/opt/SUNWcgha/sbin/nhnetcfg cgtp masterdyn | awk '{print$2}'`
if [ $? != 0 ]
then
    echo "Can not get the CGTP address of my master node"
    exit 1 
fi

master_nic0=`/opt/SUNWcgha/sbin/nhnetcfg nic0 masterdyn | awk '{print$2}'`
if [ $? != 0 ]
then
    echo "Can not get the NIC0  address of my master node"
    exit 1 
fi

master_nic1=`/opt/SUNWcgha/sbin/nhnetcfg nic1 masterdyn | awk '{print$2}'`
if [ $? != 0 ]
then
    echo "Can not get the NIC1  address of my master node"
    exit 1 
fi


case "${Node_type}" in
'Dataless')
	cgtpintf_plumb ${local_cgtp_addr}
	cgtp_route_add ${master_nic0}
	cgtp_route_add ${master_nic1}
	;;
'Diskless')
	diskless_conf
	;;
'Diskfull')
	cgtpintf_plumb ${local_cgtp_addr}
	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

