#!/bin/sh
#
#       $Id: arptables-noarp-addr_takegiveip,v 1.1 2005/02/21 02:44:37 horms Exp $
#
#       Copyright (C) 2005 Horms <horms@verge.net.au>
#
#       This script uses arptables-noarp-addr to manage 
#       the arptables entries to prevent NOARP_ADDR...
#       from being advertised via ARP.
#
#       usage: arptables-noarp-addr_takegiveip NOARP_ADDR[/IGNORED TRAILING ARGUMENTS] {take|give}
#
#       Intended for use heartbeat's local_giveip and local_takeip 
#	scripts as follows. Edit the paths in these samples as 
#       appropriate
#
#       Sample /etc/ha.d/rc.d/local_giveip
#       --- begin ---
#       #!/bin/sh
#	exec  /etc/ha.d/rc.d/arptables-noarp-addr_takegiveip $* give
#       --- end ---
#
#       Sample /etc/ha.d/rc.d/local_takeip
#       --- begin ---
#       #!/bin/sh
#	exec  /etc/ha.d/rc.d/arptables-noarp-addr_takegiveip $* take
#       --- end ---
#

unset LANG
LC_ALL=C
export LC_ALL

prefix=/usr
exec_prefix=/usr
. /etc/ha.d/shellfuncs

HELPERCMD="/usr/sbin/arptables-noarp-addr"

usage()
{
	ha_log "ERROR: Usage: arptables-noarp-addr_takegiveip NOARP_ADDR[/IGNORED TRAILING ARGUMENTS] {take|give}"
	exit 1
}

if [ ! -x "$HELPERCMD" ]; then
	ha_log "ERROR: $HELPERCMD not found"
	exit 5
fi

if [ $# != 2 ]; then
	usage
fi

ADDR="${1%%/*}"
ACTION="$2"

case $ACTION in
	take)
		CMD="$HELPERCMD ${1%%/*} stop"
		;;
	give)
		CMD="$HELPERCMD ${1%%/*} start"
		;;
	*)
		usage
		;;
esac

OUTPUT="$($CMD 2>&1)"
RC=$?
echo "$OUTPUT" | while read line; do
	if [ -z "$line" ]; then
		continue;
	fi
	ha_log info: $line
done
if [ $? = 0 ]; then
	ha_log "info: $HELPERCMD ${1%%/*} stop: success"
else
	ha_log "ERROR: $HELPERCMD ${1%%/*} stop: fail"
fi

exit $RC
