#!/bin/bash
#
# License: Copyright 2015 SpinetiX. This file is licensed under the
#          terms of the GNU General Public License version 2.  This
#          program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starting/stopping avahi-autoipd for zeroconf IP address
# Description: Starting/stopping avahi-autoipd for zeroconf IP address
### END INIT INFO
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=avahi-autoipd
DESC="Zeroconf IP"
DAEMON=/usr/sbin/$NAME
ARGS="-wD"
STATE_FILE=/var/run/avahi-autoipd.state

# config
RCMAIN=/usr/share/resources/default/init/main

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

# Load init script configuration
AVAHI_AUTOIPD_START=1
AVAHI_AUTOIPD_IFACES=auto
[ -f /etc/default/$NAME ] && . /etc/default/$NAME

# Load resources
[ -f "$RCMAIN" ] && . "$RCMAIN"

# Source the init script functions
. /etc/init.d/functions

if [ "$AVAHI_AUTOIPD_START" != "1" -a "$1" != "stop" ]; then
    echo -n "Not starting $DESC $NAME, disabled via /etc/default/$NAME "
    warning
    echo
    exit 0
fi

start_iface() {
    $DAEMON $ARGS "$1"
}

stop_iface() {
    $DAEMON -c "$1" && $DAEMON -k "$1"
}

reload_iface() {
    $DAEMON -c "$1" && $DAEMON -r "$1"
}

status_iface() {
    $DAEMON -c "$1"
}

get_start_ifaces_auto() {
    local iface

    AVAHI_AUTOIPD_IFACES=
    if ! type udevadm 2>/dev/null >/dev/null; then
        echo -n "Starting $DESC: missing udevadm, cannot autodetect interfaces "
        warning
        echo
        return 1
    fi

    for iface in /sys/class/net/*; do
        [ -e "$iface"/type -a -e "$iface"/address -a -e "$iface"/uevent ] || continue

        # accept only Ethernet type interfaces (includes wlan)
        [ "$(< "$iface"/type)" == 1 ] || continue

        # get interface info from udev
        UDEV_ID_BUS=
        UDEV_DEVPATH=
        UDEV_DEVTYPE=
        UDEV_INTERFACE=
        eval `udevadm info --query=property --export-prefix=UDEV_ --export --path="$iface"`

        # only interfaces on PCI or platform, no pluggable USB
        [ "$UDEV_ID_BUS" = 'pci' -o "$UDEV_DEVPATH" != "${UDEV_DEVPATH#/devices/platform/}" ] || continue

        # only Ethernet or WLAN, no WWAN
        [ -z "$UDEV_DEVTYPE" -o "$UDEV_DEVTYPE" = 'wlan' ] || continue

        # skip WLAN if model has no WLAN support
        [ "$UDEV_DEVTYPE" = 'wlan' -a "$rc_has_wlan" != 'yes' ] && continue

        # OK, use avahi-autoipd on this interface
        AVAHI_AUTOIPD_IFACES="${AVAHI_AUTOIPD_IFACES:+$AVAHI_AUTOIPD_IFACES }$UDEV_INTERFACE"
    done

}

get_start_ifaces() {

    [ "$AVAHI_AUTOIPD_IFACES" != 'auto' ] || get_start_ifaces_auto

    echo "AVAHI_AUTOIPD_IFACES='$AVAHI_AUTOIPD_IFACES'" >> "$STATE_FILE"
}

load_state() {
    AVAHI_AUTOIPD_IFACES=
    [ -f "$STATE_FILE" ] && . "$STATE_FILE"
}

init_state() {
    : > "$STATE_FILE"
    get_start_ifaces
}

start() {
    local iface
    local ret=0
    local found=0

    init_state

    for iface in $AVAHI_AUTOIPD_IFACES; do
	echo -n "Starting $DESC: $NAME on $iface: "
	start_iface "$iface" && success || failure 1
	[ $? -eq 0 ] || ret=1
	echo
	found=1
    done

    if [ $found = 0 ]; then
	echo -n "Starting $DESC: $NAME, no interfaces: "
	warning
	echo
    fi

    return $ret
}

stop() {
    local iface
    local ret=0

    load_state
    for iface in $AVAHI_AUTOIPD_IFACES; do
	echo -n "Stopping $DESC: $NAME on $iface: "
	stop_iface "$iface" && success || failure 1
	[ $? -eq 0 ] || ret=1
	echo
    done
    rm -f "$STATE_FILE"

    return $ret
}

restart() {
    local iface
    local ret=0

    load_state
    for iface in $AVAHI_AUTOIPD_IFACES; do
	echo -n "Restarting $DESC: $NAME on $iface: "
	stop_iface "$iface"
	start_iface "$iface" && success || failure 1
	[ $? -eq 0 ] || ret=1
	echo
    done

    return $ret
}

condrestart() {
    local iface
    local ret=0

    load_state
    for iface in $AVAHI_AUTOIPD_IFACES; do
	status_iface "$iface" || continue
	echo -n "Restarting $DESC: $NAME on $iface: "
	stop_iface "$iface"
	start_iface "$iface" && success || failure 1
	[ $? -eq 0 ] || ret=1
	echo
    done

    return $ret
}

reload() {
    local iface
    local ret=0

    load_state
    for iface in $AVAHI_AUTOIPD_IFACES; do
	echo -n "Refreshing $DESC: $NAME on $iface: "
	reload_iface "$iface" && success || failure 1
	[ $? -eq 0 ] || ret=1
	echo
    done

    return $ret
}

status() {
    local iface
    local pid
    local ret=0

    load_state
    for iface in $AVAHI_AUTOIPD_IFACES; do
	pid=
	[ -s /var/run/$NAME.$iface.pid ] && pid="$(< /var/run/$NAME.$iface.pid)"
	if [ -n "$pid" ] && kill -0 $pid; then
	    echo "$NAME for $iface (pid $pid) is running..."
	else
	    echo "$NAME for $iface is stopped"
	    ret=1
	fi
    done

    return $ret
}

parse() {
    case "$1" in
	start)
	    start
	    return $?
	    ;;
	stop)
	    stop
	    return $?
	    ;;
	restart)
	    restart
	    return $?
	    ;;
	condrestart|try-restart)
	    condrestart
	    return $?
	    ;;
	reload)
	    reload
	    return $?
	    ;;
	force-reload)
	    restart
	    return $?
	    ;;
	status)
	    status
	    return $?
	    ;;
	*)
	    echo "Usage: $NAME " \
		"{start|stop|restart|condrestart|reload|force-reload|status}" >&2
	    ;;
    esac
	
    return 1
}

parse $@

