#!/bin/sh

# (c) 2011 Rafał Frühling <rafamiga@gmail.com>

# This script will check bring up, bring down and check existence of an
# alias on a interface.  It's meant to be executed at startup/shutdown by an
# init.d script passing along it's argument ("start", "stop", "reload" etc).

# It gets IP address from it's name, like "loalias-192.168.55.121".

# Managing interfaces using this script is useful if a strong service-alias
# dependence is required, as in case of an alias on lo interface used for
# serving web content in LVS or multi-route OSPF environment, and/or when
# traditional network scripts cannot be used.

# Currently, it's restricted to operations on lo interface but it can be
# easily changed to make it parse $0 to get the interface name

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

. /etc/init.d/functions

ipsrc=$0

ip_exe="/sbin/ip"
scriptname=${ipsrc%%-*}
ipalias=${ipsrc#*-}
cmd=$1
intdev=lo

check_addr() {
    is_up_somewhere=`${ip_exe} addr list to ${ipalias} | wc -l`
    is_up_atint=`${ip_exe} addr list to ${ipalias} dev ${intdev} | wc -l`
    }

start() {
    echo -n "Bringing up ${intdev} alias ${ipalias}/32: "

    check_addr
    
    if [ ${is_up_somewhere} -eq 0 ]; then
    	${ip_exe} addr add ${ipalias} dev ${intdev} 2>/dev/null
	retval=$?

    	if [ ${retval} -eq 0 ]; then
    		success; echo #"brought up"
	    	return 0
	fi

	echo -n "${ip_exe} error=${retval}"; failure; echo
	return 1
    fi

    # it's up already, but it it at ${intdev}?

    if [ ${is_up_atint} -ne 0 ]; then
    	echo -n "is up already"; success; echo
    	return 0
    fi

    echo -n "up not on ${intdev}!!"; failure; echo
    return 1
    }

reload() {
    start
    }

restart() {
    start
    }

stop() {
    echo -n "Stopping ${intdev} alias ${ipalias}/32: "

    check_addr

    is_up_somewhere=`${ip_exe} addr list to ${ipalias} | wc -l`

    if [ ${is_up_somewhere} -eq 0 ]; then
	success; echo #-n "is down already"; success; echo
        return 0
    fi

    if [ ${is_up_atint} -ne 0 ]; then
        ${ip_exe} addr del ${ipalias} dev ${intdev} 2>/dev/null
	retval=$?

    	if [ ${retval} -eq 0 ]; then
    		success; echo #"brought down"
	    	return 0
	fi

	echo "${ip_exe} error=${retval}"; failure; echo
	return 1
    fi

    echo -n "up not on ${intdev}!!"; failure; echo
    return 1
    }

status()
    {
    check_addr
    
    # if up somewhere
    if [ ${is_up_somewhere} -eq 0 ]; then
	return 3
    fi

    # is up and on interface we want to have it
    if [ ${is_up_atint} -eq 0 ]; then
       return 3
    fi
    
    return 0        
    }

if [ $UID != 0 ]; then
    echo "This script can be run only by the root user."
    exit 4
fi

if [ -z "${ipalias}" ]; then
   echo "IP address missing. This script's name should contain an IP address, for ex.: loalias-192.168.19.74"
   exit 2
fi

case "$1" in
    start|stop|status|reload|restart)
        $1
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|status}"
        exit 2
esac
