#!/bin/sh
# $Id$
#
# Script:	S30interface
#
# Platform:	All supported
#
# Author:	High-Availability.Com Ltd
#
# Description:	Bring up/down additional interface (physical or floating)
#
# This script  should be used to add  more  available  interfaces for a service
# once it is brought up by RSF-1.  It should be placed in a  services directory
# and the  appropriate K script  (shutdown)  linked in as well.  It can be used
# to bring up either secondard addressing on an existing interface (i.e. le0:1)
# or to bring up a completely new interface (i.e. le1).
#
# Variables:
#   "INTERFACE" Should be set to the interface name to be plumbed in. This is a
#               REQUIRED variable.
#   "ADDRESS"   Should be set to the IP  address to assign to the interface, it 
#               can be a name form  (phoenix, phoenix.rising.com) or a dot form
#               (192.168.1.1). This is a REQUIRED variable.
#   "DEFROUTE"  Provides an  OPTIONAL  new default route to  be added once this
#               interface is enabled.
# 
# This script  also handles  MAC  address changing for operating  systems  that
# support it.
#
# Variables:
#   "FAKEMAC"   The new MAC address to assign once the interface is brought up.
#   "REALMAC"   The MAC address to assign once the interface is brought down.
#   "PRIM"      The address  of the primary  server,  used for deleting old mac
#               entries in the ARP cache.
#   "SEC"       The address  of the secondary server, used for deleting old mac
#               entries in the ARP cache.
#
#
# *** Edit the following variable for your site. ***
#
INTERFACE="eth0:1"  # network interface device
ADDRESS="192.168.45.6"  # hostname/addr for device
DEFROUTE=""	   # default route, if required
#
# *** For MAC address failover - leave as is if not required ***
#
FAKEMAC=""	   # unique, fictional MAC addr, if required (eg. de:ad:be:ef:1:1)
REALMAC=""	   # usual MAC addr for this interface (put back when FAKEMAC is downed)
PRIM=""	           # primary server nodename
SEC=""	           # secondary server nodename

#
# Do not edit below here.
. /opt/HAC/bin/rsf.sh
script="`basename $0`"
state="$1"
attempts=${2:-'1'}
broadcast=''

#
# Test sanity
if [ ${attempts} -gt 3 ]; then
	dated_echo "too many retries, aborting..."
	exit ${RSF_ABORT}
fi

if [ -z "${INTERFACE}" ] ; then
       dated_echo "interface not set! aborting..."
       exit ${RSF_ABORT}
fi

if [ -z "${ADDRESS}" ] ; then
       dated_echo "address not set! aborting..."
       exit ${RSF_ABORT}
fi

#
# Get the netmask to use.
case "${COMPANY_OS}" in
'sunos')
	netmask='netmask +'
	if [ "${COMPANY_OS_REL}" = "5.9" ] ; then 
	    broadcast='broadcast +'
	fi
	;;
'linux'|'aix'|'hp-ux'|'sco_sv'|'unixware'|'freebsd'|'openbsd')
	netmask=`hac_getmask ${ADDRESS}`
	;;
*)
	netmask=''
	;;
esac

#
# Do the work.
case "${state}" in
'start')
	dated_echo "configuring interface ${INTERFACE}"
	if [ "${COMPANY_OS}" = "sunos" ] ; then
	    ifconfig ${INTERFACE} plumb
	fi

	ifconfig ${INTERFACE} inet ${ADDRESS} ${netmask} ${broadcast} up
	if [ ! -z "${FAKEMAC}" ] ; then
		ifconfig ${INTERFACE} ether ${FAKEMAC}
		# fix our own cache
		arp -d ${PRIM}
		arp -d ${SEC}
		arp -s `uname -n` ${FAKEMAC} pub
	fi
	#
	# 
	if [ "${DEFROUTE}" != "" ]; then
		dated_echo "adding default route."
		case "${COMPANY_OS}" in
		    'sunos')
		    route add default ${DEFROUTE} 1
		    ;;
		    'linux')
		    route add default gw ${DEFROUTE}
		    ;;
		    *)
		    dated_echo "default route not handled - please modify"
		    ;;
	       esac
	fi
	exit ${RSF_OK}
	;;


'stop')
	dated_echo "stopping interface ${INTERFACE}"
	if [ "${DEFROUTE}" != "" ]; then
		dated_echo "removing default route."
		route delete default ${DEFROUTE}
	fi
	if [ "${COMPANY_OS}" = "sunos" -a "${COMPANY_OS_REL}" = "5.9" ] ; then 
	    ifconfig ${INTERFACE} unplumb
	else
	    if [ "${COMPANY_OS}" = "sunos" ] ; then
		ifconfig ${INTERFACE} 0 down
	    else
		ifconfig ${INTERFACE} down
	    fi
	fi

	if [ "${FAKEMAC}" != "" ] ; then
		# restore orig MAC addr
		ifconfig ${INTERFACE} ether ${REALMAC}
		# fix our own cache
		arp -d ${PRIM}
		arp -d ${SEC}
		arp -s `uname -n` ${REALMAC} pub
	fi
	exit ${RSF_OK}
	;;


*)
	echo "usage: ${script} <start|stop>"
	exit ${RSF_WARN}
	;;

esac
