#!/bin/sh

. alterator-net-functions
. /etc/ha.d/shellfuncs

IPTABLES_HELPER=/usr/bin/iptables_helper
IFACESDIR=/etc/net/ifaces

onboot_option()
{
    for iface in $1; do
        write_iface_option "$IFACESDIR/$iface" ONBOOT "$2"
    done
}

updown()
{
    local ret=0 e=
    for iface in $1; do
        # Skip ifaces without configuration
        [ -s "$IFACESDIR/$iface/options" ] || continue

        /sbin/if$2 "$iface"
        e="$?"
        # If iface already up - it's OK
        if [ "$e" -ne 2 ]; then
            ret="$(( $ret + $e ))"
        fi
    done
    return "$ret"
}

case "$1" in
    start)
    ha_pseudo_resource ahs_ifupdown_externals start
    eifaces="$("$IPTABLES_HELPER" show -e 2>/dev/null)"
    [ -n "$eifaces" ] || exit 0
    onboot_option "$eifaces" yes
    updown "$eifaces" up
    ;;
    stop)
    eifaces="$("$IPTABLES_HELPER" show -e 2/dev/null)"
    if [ -n "$eifaces" ]; then
        onboot_option "$eifaces" no
        updown "$eifaces" down
    fi
    ha_pseudo_resource ahs_ifupdown_externals stop
    ;;
    *)
    ha_pseudo_resource ahs_ifupdown_externals "$1"
    ;;
esac

