#!/bin/ksh
set -x

##################################
# Parameter to set up
##################################

DBNAME=SATEST
SVC_IP_ADDRESS=192.168.0.10

HOSTA=nameofa
HOSTB=nameofb

HOSTA_BASE_ADDR=192.168.0.1				# Interface must be running prior to vcs startup i.e The file
HOSTB_BASE_ADDR=192.168.0.2				# /etc/hostname.??? must exist..

NETMASK=255.255.255.0

PING_HOSTS="192.168.0.5 192.168.0.6 192.168.0.7"	# IP addresses which are pinged to ensure 
							# that the interface is OK - if at lease 1 responds
							# were still alive...

DEFAULT_GATEWAY="192.168.0.250"

HOSTA_PRIMARY_INTERFACE=hme0
HOSTA_SECONDARY_INTERFACE=hme1

HOSTB_PRIMARY_INTERFACE=hme0
HOSTB_SECONDARY_INTERFACE=hme1
 
MAIN="/etc/VRTSvcs/conf/config/main.cf"

##################################
# Make config writeable
##################################

haconf -makerw
if [ $? != 0 ]
 then
    echo "ERROR: Unable to set the config read/wright.."
    exit 1
fi

##################################
# Add MultiNIC resource
##################################

if [ "$(hares -list | grep -c \"^nic${DBNAME} \")" -lt 1 ]
 then
    MULTINICA=`cat ${MAIN} | grep -v "\/\/" | grep MultiNICA | awk '{print $2}'`
    if [ ! "${MULTINICA}" ]
     then
        hares -add nic${DBNAME} MultiNICA ${DBNAME}
        hares -local nic${DBNAME} Device
        hares -modify nic${DBNAME} Device -add ${HOSTA_PRIMARY_INTERFACE} ${HOSTA_BASE_ADDR} -sys ${HOSTA}
        hares -modify nic${DBNAME} Device -add ${HOSTA_SECONDARY_INTERFACE} ${HOSTA_BASE_ADDR} -sys ${HOSTA}
        hares -modify nic${DBNAME} Device -add ${HOSTB_PRIMARY_INTERFACE} ${HOSTB_BASE_ADDR} -sys ${HOSTB}
        hares -modify nic${DBNAME} Device -add ${HOSTB_SECONDARY_INTERFACE} ${HOSTB_BASE_ADDR} -sys ${HOSTB}
        hares -modify nic${DBNAME} NetMask ${NETMASK}
        hares -modify nic${DBNAME} NetworkHosts ${PING_HOSTS}
        hares -modify nic${DBNAME} RouteOptions "default ${DEFAULT_GATEWAY} 1"
        hares -modify nic${DBNAME} ArpDelay 5
        hares -modify nic${DBNAME} IfconfigTwice 1
        hares -modify nic${DBNAME} Enabled 1

    fi
fi

#####################################
###  Add IP resource
###  If we already have a MultiNIC defined 
###  set up a proxy to the resource
#####################################

if [ "$(hares -list | grep -c \"^ip${DBNAME} \")" -lt 1 ]
 then
    if [ "${MULTINICA}" ]
     then
        hares -add ip${DBNAME} IPMultiNIC ${DBNAME}
        hares -modify ip${DBNAME} Address ${SVC_IP_ADDRESS} 
        hares -modify ip${DBNAME} NetMask ${NETMASK}
        hares -modify ip${DBNAME} MultiNICResName ${MULTINICA}
        hares -modify ip${DBNAME} IfconfigTwice 1
        hares -modify ip${DBNAME} Enabled 1
    
        hares -add proxy${DBNAME} Proxy ${DBNAME}
        hares -modify proxy${DBNAME} TargetResName ${MULTINICA}
        hares -modify proxy${DBNAME} Enabled 1
    
        hares -link ip${DBNAME} proxy${DBNAME}
    else
        hares -add ip${DBNAME} IPMultiNIC ${DBNAME}
        hares -modify ip${DBNAME} Address ${SVC_IP_ADDRESS} 
        hares -modify ip${DBNAME} NetMask ${NETMASK}
        hares -modify ip${DBNAME} MultiNICResName nic${DBNAME}
        hares -modify ip${DBNAME} IfconfigTwice 1
        hares -modify ip${DBNAME} Enabled 1
    
        hares -link ip${DBNAME} nic${DBNAME}
    fi
fi

haconf -dump -makero
if [ $? != 0 ]
 then
    echo "ERROR: Unable to set the config read/only.."
    exit 1
fi
