#!/bin/sh
#
# Called when the monitored IP-address dies or comes alive
#
# Here we take over or release the external IP-address of the
# other frontend machine, and run any administration scripts
# to notify the sysadmins appropriately.

# Ensure ZEUSHOME is setup to point to our installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/zxtm/bin
    OLD_PWD=`pwd`
    cd `dirname $0`/../..
    ZEUSHOME=`pwd`
    export ZEUSHOME
    cd $OLD_PWD
fi

if [ ! -d "$ZEUSHOME/zxtm" ]
then
  echo 'FATAL: $ZEUSHOME is not set, or is set incorrectly'
  echo 'It should point to the Stingray Traffic Manager installation'
  exit 1
fi

# Try to get networking commands into our PATH
PATH=/sbin:/usr/etc/sbin:/usr/etc:/usr/sbin:$PATH
export PATH

# Exit on 1st error
set -e


#----------------------------------------------------------------------------
# Run the necessary operating systems commands
# (ifconfig, route) needed to make the interface
# come up with the IP-address, netmask and broadcast
# addresses specified for this host, and add a route
interface_up()
{
    IP=$1
    INTERFACE=$2
    BROADCAST=$3

    case $PLATFORM in
        Linux)
           if [ "$INET4" = "1" ]; then
              ip addr add $IP broadcast $BROADCAST dev $INTERFACE
           else
              ip addr add $IP dev $INTERFACE
           fi
            ;;
        SunOS)
	   ipversion="inet6"
	   broadcast=""
           if [ "$INET4" = "1" ]; then
	       ipversion=""
	       broadcast="broadcast $BROADCAST"
           fi
	   # SunOS complains if the iface already exists, but we can ignore that
           ifconfig $INTERFACE $ipversion plumb 2>/dev/null
	   # SunOS reports to stdout if it creates a new alias, we're not interested
           ifconfig $INTERFACE $ipversion addif $IP $broadcast up 1>/dev/null
            ;;
        *)
            echo "FATAL\tFlipper doesn't know about platform $PLATFORM yet" >&2
            exit 1
            ;;
    esac
}


# Run the necessary operating systems commands
# (ifconfig, route) needed to make the interface
# and route obsolete
interface_down()
{
    IP=$1
    INTERFACE=$2

    case $PLATFORM in
        Linux)
           ip addr del $IP dev $INTERFACE
            ;;
        HP-UX)
            # on HP-UX we also need to remove the entry from the arp cache.
            ifconfig $INTERFACE 0.0.0.0
            arp -d $IP || true
            ;;
        SunOS)
            RELEASE=`uname -r`
            case $RELEASE in
                5.6|5.7)
                    ifconfig $INTERFACE 0 down
                    ;;
                *)
		   ipversion="inet6"
		   if [ "$INET4" = "1" ]; then
		      ipversion=""
		   fi
                   # solaris doesn't like the /N when taking the address down
                   IPONLY=`echo $IP | cut -d '/' -f 1`
                   ifconfig $INTERFACE $ipversion removeif $IPONLY
                    ;;
            esac
            ;;
        *)
            echo "FATAL\tFlipper doesn't know about platform $PLATFORM yet" >&2
            exit 1
            ;;
    esac
}


#------------------------------------------------------------
# Run the neccessary commands to activate an interface.
#
interface_activate()
{
   INTERFACE=$1;
   ISV4=$2;

   case $PLATFORM in
      Linux)
         ip link set up dev $INTERFACE
         ;;
      SunOS)
        if [ "$ISV4" = "1" ]; then
          ifconfig $INTERFACE plumb up
        else
          ifconfig $INTERFACE inet6 plumb up
        fi
        ;;
       HP-UX|FreeBSD|IRIX64|OSF1|OpenBSD|AIX)
         ifconfig $INTERFACE up
         ;;
       *)
         echo "FATAL\tFlipper doesn't know about platform $PLATFORM yet" >&2
         exit 1
         ;;
   esac
}


##############################################################################

ACTION=$1
INTERFACE=$2
IP=$3
BROADCAST=$4

# Check for IPv6 addresses
INET4=1
if ( echo $IP | grep ':' > /dev/null 2>&1 ) ; then
    INET4=0
fi


if [ "$ACTION" = "takeover" -a "$INET4" = "1" -a "x$BROADCAST" = "x" ]; then
    echo "FATAL\tinterface <takeover|release|activate> <interface> <ip address> <broadcast>" >&2
    exit 1
fi

PLATFORM=`uname`

case $ACTION in
    takeover)
        interface_up $IP $INTERFACE $BROADCAST
        ;;
    release)
        # we should have been passed the correct interface and details
        interface_down $IP $INTERFACE
        ;;
    activate)
        # Activate the parent interface of an alias interface.
        interface_activate $INTERFACE $ISV4
        ;;
    *)
        echo "Usage: interface <takeover|release> <interface> <ip address> <broadcast>" >&2
        echo "Usage: interface activate <interface> <isipv4>" >&2
        exit 1
    ;;
esac
