#!/bin/sh
#
# Script:	S31route_order
#
# Description:	Re-orders the network routing table to make the specified interface the prefered route
#
# Platform:	Solaris
#
# Author:	High-Availability.Com Ltd
#
# assumes that $RSF_IPDEV has been set by rsfexec, for running by hand you will need to set the env variable
#

. /opt/HAC/bin/rsf.sh

service=${RSF_SERVICE:-"service"}	# short service name

# EDIT
net="192.168.1.0"	# network id (which is interface ip BITWISE ANDed with the network mask)
						# so 192.168.1.20 & ffffff00 = 192.168.1.0

script="`basename $0`"
state="$1"
attempts=${2:-'1'}

if [ ${attempts} -gt 3 ]; then
	dated_echo "Too many retries, aborting..."
	exit 3
fi

case "${rsf_OS}" in
'sunos')
	netmask='netmask +'
	;;
'linux'|'aix')
	netmask=`getmask ${floatname}`
	;;
*)
	netmask=''
	;;
esac

case "${state}" in

'start')
	dated_echo "Making interface ${RSF_IPDEV} the prefered route"
	while [ 1 ]; do
		list=`/usr/bin/netstat -rn | /usr/bin/grep $net | /usr/bin/grep -v 224.0.0.0 | /usr/bin/head -1`
		now=`/usr/bin/echo $list | /usr/bin/awk '{print $6}'`
		if [ $RSF_IPDEV != $now ]; then
			dated_echo 'Lowering the priority of '$now
			ip=`/usr/bin/echo $list | /usr/bin/awk '{print $2}'`
			dated_echo `/usr/sbin/route delete net $net $ip`
			dated_echo `/usr/sbin/route add net -iface net $net $ip`
		else
			break
		fi
	done
	exit 0
	;;


'stop')
	exit 0
	;;


*)
	/usr/bin/echo "usage: ${script} <start|stop>"
	exit 1
	;;

esac

