#!/bin/bash

MODE=${1}
IPADDRESS=${2}
# figure out which network device to use
if [ -n "`ip addr | grep inet | grep bond0`" ]; then
    # DL3/XL8 use bonded eth0/1
    NETWORKDEVICE=bond0
else
    # DL1/2 use two separate ports
    NETWORKDEVICE=eth1
fi

APPDIR=/opt/midas/bin
APPNAME=set-marmaserver-mode

function usage
{
    echo "usage: ${APPNAME} {dhcp|static} [static ip address]"
    echo "       ${APPNAME} standby"
}

echo "${APPNAME} ${MODE} ${IPADDRESS}"

# arg checking
if [ "${MODE}" != "standby" ] && [ "${MODE}" != "dhcp" ] && [ "${MODE}" != "static" ]; then
    echo "invalid arg"
    usage
    exit 1
fi

# TODO: add DHCP support for DL3/XL8
if [ ${MODE} == "dhcp" ] && [ ${NETWORKDEVICE} == "bond0" ]; then
    echo "dhcp not yet supported on XL8/DL3"
    exit 1
fi

if [ "${MODE}" == "static" ] && [ -z "${IPADDRESS}" ]; then
    echo "ip address arg is required for static mode"
    usage
    exit 1
fi

# simple format check of IP address, and append default subnet if not supplied
if [ "${MODE}" == "static" ]; then
    if [ -n "`echo ${IPADDRESS} | egrep '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$'`" ]; then
        IPADDRESS=${IPADDRESS}/24
    elif [ -z "`echo ${IPADDRESS} | egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$'`" ]; then
        echo "invalid ip address arg format, should look something like this: 10.101.1.2 or 10.101.1.2/24"
        usage
        exit 1
    fi
fi

# remove existing server addresses from network device
function clearAddresses
{ 
    # note exclusion of MC addresses
    EXISTING_ADDRESSES=(`ip addr show dev ${NETWORKDEVICE} | grep " inet " | sed 's/[a-z ]*\([0-9./]*\).*$/\1/' | egrep -v "192.168.20.[1-2]"`)
    for i in ${EXISTING_ADDRESSES[@]}; do
        ip addr del ${i} dev ${NETWORKDEVICE}
    done
}

# if we are in standby mode, just remove the address
if [ $MODE == "standby" ]; then
    echo "killing dhcpcd"
    pkill dhcpcd
    echo "removing previous server address(es) from ${NETWORKDEVICE}"
    # if this call fails, we want to return failure!
    clearAddresses
    exit 0
fi

# for telling other MC to standby
case "`hostname`" in
    MC-1)
        OTHERMC=MC-2
    ;;
    MC-2)
        OTHERMC=MC-1
    ;;
esac

# attempt to set up ip address on this MC
if [ "${MODE}" == "static" ]; then
    # do nothing if address is already set up
    if [ -z "`ip addr show dev ${NETWORKDEVICE} | grep inet | grep ${IPADDRESS}`" ]; then
        # XL8/DL3 - force standby on other MC
        if [ "${NETWORKDEVICE}" == "bond0" ];
        then
            echo "forcing ${OTHERMC} to standby"
            # don't block in case the other MC is dead
            ssh ${OTHERMC} "${APPDIR}/${APPNAME} standby" &
        fi
        # clear unwanted addresses
        echo "removing address from ${NETWORKDEVICE}"
        clearAddresses
        echo "adding static address: ${IPADDRESS} to device ${NETWORKDEVICE}"
        ip addr add ${IPADDRESS} dev ${NETWORKDEVICE}
    fi
fi

if [ "${MODE}" == "dhcp" ]; then
    # if dhcpcd not running, force other server down (XL8/DL3) and start ours    
    if [ -z "`ps -e -o comm | egrep '^dhcpcd$'`" ]; then
		# make sure any static addresses get dumped
		clearAddresses
        if [ "${NETWORKDEVICE}" == "bond0" ]; then
            echo "forcing ${OTHERMC} to standby"
            ssh ${OTHERMC} "${APPDIR}/${APPNAME} standby"
        fi
        echo "setting ${NETWORKDEVICE} to dhcp" 
        dhcpcd ${NETWORKDEVICE} &
    fi
fi

