#!/sbin/sh
#  ident "@(#)nhcgtp 1.5     02/03/21 SMI"
#
# Copyright 2001 Sun Microsystems, Inc. All rights reserved.
#

# the script exists immediately if CMM_USE_CGTP is not set to YES

USER_FILE=/etc/opt/SUNWcgha/nhcmm.conf
# inline the user modifiable config file
.  $USER_FILE
if [ "$CMM_USE_CGTP" != YES ]; then
    exit 0
fi


# Discover what is our NHAS Node ID , looking at already
# defined interfaces that have address like "10.x.1.nodeId"
# or like  "10.x.2.nodeId"
# so that we can deduce the corresponding CGTP adress

PROG=`basename $0`

itf=`netstat -in | egrep "10\.[0-9][0-9]*\.[12]\."`

set -- ${itf}

if [ "$4" = "" ]
then
    echo "$PROG: This machine is not configured for a NHAS cluster"
    exit 1
else
    nodeId=`echo $4  | cut -d. -f4`
    clusterId=`echo $4  | cut -d. -f2`
    local_cgtp_addr=10.${clusterId}.3.${nodeId}
fi

# Call DHCP info to see if we have booted through network
# using dhcp: get the "Router" inof
router=`/sbin/dhcpinfo Router 2> /dev/null`

if [ $? = 0 ]
then
# we were booted from network/dhcp
    network_boot=1 
else
    network_boot=0
fi


# if system has booted through the network, 
# need to add a host route before configuring cgtp0
if [ ${network_boot} = 1 ]
then
    prefix=`echo $router | cut -d. -f1-2`

    # boot_ip_net either 1 or 2
    boot_ip_net=`echo $router | cut -d. -f3`

    if [ ${boot_ip_net} = 1 ]
    then
	alt_ip_net=2
    else
	alt_ip_net=1
    fi

    # CGTP master
    master=${prefix}.3.1
    
    master1=${prefix}.${boot_ip_net}.1
    master2=${prefix}.${alt_ip_net}.1

    # add a host route to the IP master address, cannot use setsrc
    route add  ${master} ${master1} -multirt

fi

# On all nodes, either booted from disk or from network
#  configure cgtp0 interface 
ifconfig cgtp0 plumb
ifconfig cgtp0  ${local_cgtp_addr} netmask 255.255.255.0 broadcast + up


# continue what is needed for a diskless to have
# a correct CGTP routing for the NFS mount of /
if [ ${network_boot} = 1 ]
then
    route add  ${master} ${master2} -multirt -setsrc ${local_cgtp_addr}
    # now the CGTP address is configured, use setsrc
    route change  ${master} ${master1} -multirt -setsrc ${local_cgtp_addr}

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

exit 0

