#!/bin/bash
# $Id: ucarp.init,v 1.1 2007/02/05 13:06:40 thias Exp $
#
# chkconfig: - 91 09
# description: Starts and stops the common address redundancy protocol daemon

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

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

get_files() {
    FILES=`find ${CONFDIR} -maxdepth 1 -type f -name 'vip-*.conf' \
        -printf "%f\n" | egrep '^vip-[[:digit:]]+\.conf$' | LC_COLLATE="C" sort`
}

prog=$"common address redundancy protocol daemon"
OUR_INITLOG_ARGS="-n ucarp -e 2 -s"
OUR_INITLOG="initlog ${OUR_INITLOG_ARGS}"
CONFDIR=/etc/ucarp
UPSCRIPT=${CONFDIR}/vip-up
DOWNSCRIPT=${CONFDIR}/vip-down

start() {
    RETVAL=-1
    VIP_RETVAL=0

    echo -n $"Starting ${prog}: "

    get_files

    if [ -z "${FILES}" ]; then
        failure $"no virtual addresses are configured in ${CONFDIR}:"
        RETVAL=1
    else
        for FILE in ${FILES}; do
            # Check that the file name gives us an ID between 1 and 255
            ID=`echo ${FILE}| sed 's/^vip-\(.*\).conf/\1/'`
            if [ ${ID} -lt 1 -o ${ID} -gt 255 ]; then
                ${OUR_INITLOG} $"ID out of range (1-255) for ${FILE}, skipped VIP ID ${ID}:"
                continue
            fi

            unset PASSWORD BIND_INTERFACE BIND_ADDRESS VIP OPTIONS
            # Source ucarp settings
            . ${CONFDIR}/${FILE}
            TMP_RETVAL=0

            if [ -z "${PASSWORD}" ]; then
                ${OUR_INITLOG} $"no PASSWORD found in ${FILE}, skipped VIP ID ${ID}:"
                TMP_RETVAL=1
            fi
            if [ -z "${BIND_ADDRESS}" ]; then
                ${OUR_INITLOG} $"no BIND_ADDRESS found in ${FILE}, skipped VIP ID ${ID}:"
                TMP_RETVAL=1
            fi
            if [ -z "${VIP_ADDRESS}" ]; then
                ${OUR_INITLOG} $"no VIP_ADDRESS found in ${FILE}, skipped VIP ID ${ID}:"
                TMP_RETVAL=1
            fi

            # If one of more of the above failed, skip the daemon launch
            if [ ${TMP_RETVAL} -ne 0 ]; then
                VIP_RETVAL=1
                continue
            fi

            [ ${RETVAL} -eq -1 ] && RETVAL=0
            daemon /usr/sbin/ucarp -v ${ID} -m ${ID} -p ${PASSWORD} -s ${BIND_ADDRESS} -a ${VIP_ADDRESS} -i ${BIND_INTERFACE} ${OPTIONS} -B --upscript=$UPSCRIPT --downscript=$DOWNSCRIPT >/dev/null
            LAUNCH_RETVAL=$?
            [ ${LAUNCH_RETVAL} -ne 0 ] && RETVAL=1
        done

        # failure/success or warning if launch worked with some vip errors
        if [ ${RETVAL} -eq 0 -a ${VIP_RETVAL} -eq 0 ]; then
            success $"all ucarp configurations were applied successfully:"
            touch /var/lock/subsys/ucarp
        elif [ ${RETVAL} -eq 0 -a ${VIP_RETVAL} -eq 1 ]; then
            warning $"error in one or more of the ucarp configurations:"
        else
            failure $"error running one or more of the ucarp daemon instances:"
        fi
    fi
    echo
}

stop() {
    echo -n $"Stopping $prog: "
    killproc /usr/sbin/ucarp >/dev/null
    RETVAL=$?

    # We unassign all UCARP-managed VIPs when stopping the service
    # to avoid conflicting "leftovers".

    get_files

    [ ! -z "${FILES}" ] && \
    for FILE in ${FILES}; do
        ID=`echo ${FILE}| sed 's/^vip-\(.*\).conf/\1/'`
        unset PASSWORD BIND_INTERFACE BIND_ADDRESS VIP OPTIONS
        # Source ucarp settings
        . ${CONFDIR}/${FILE}
        $DOWNSCRIPT $BIND_INTERFACE $VIP_ADDRESS ${ID} &>/dev/null
        #TMP_RETVAL=$?
        #[ ${TMP_RETVAL} -ne 0 ] && VIP_RETVAL=1
    done

    # failure/success (no warning, too complicated to handle properly)
    if [ ${RETVAL} -eq 1 ]; then
        failure $"it seems like no ucarp daemon were running:"
    else
        success $"all ucarp daemons stopped and IP addresses unassigned:"
        rm -f /var/lock/subsys/ucarp
    fi
    echo
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/ucarp ]; then
            stop
            start
        fi
        ;;
    status)
        status /usr/sbin/ucarp
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL

