#!/bin/sh
### BEGIN INIT INFO
# Provides:          avahi-dnsconfd
# Required-Start:    $remote_fs avahi-daemon
# Required-Stop:     $remote_fs avahi-daemon
# Should-Start:	     $syslog
# Should-Stop:       $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Avahi Unicast DNS Configuration Daemon
# Description:       A DNS configuration daemon using mDNS in a
#                    DHCP-like fashion
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="Avahi Unicast DNS Configuration Daemon"
NAME="avahi-dnsconfd"
DAEMON="/usr/sbin/$NAME"
SCRIPTNAME=/etc/init.d/$NAME

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

. /lib/lsb/init-functions

#
#       Function that starts the daemon/service.
#
d_start() {
    $DAEMON -c && return 0

    if [ -s /etc/localtime ]; then
        if [ ! -d /etc/avahi/etc ]; then
            mkdir -p /etc/avahi/etc >/dev/null 2>&1
        fi
        cp -fp /etc/localtime /etc/avahi/etc >/dev/null 2>&1
    fi;

    $DAEMON -D
}

#
#       Function that stops the daemon/service.
#
d_stop() {
    if $DAEMON -c ; then
       $DAEMON -k
    fi
}

#
#       Function that reload the config file for the daemon/service.
#
d_refresh() {
    $DAEMON -c && $DAEMON -r
}

#
#       Function that check the status of the daemon/service.
#
d_status() {
    $DAEMON -c && { echo "$DESC is running"; exit 0; } || { echo "$DESC is not running"; exit 3; }
}

case "$1" in
    start)
        log_daemon_msg "Starting $DESC" "$NAME"
        d_start
        log_end_msg $?
        ;;
    stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        d_stop
        log_end_msg $?
        ;;
    refresh)
        log_daemon_msg "Refreshing $DESC" "$NAME"
        d_refresh
        log_end_msg $?
        ;;
    restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        d_stop
        if [ "$?" -eq 0 ]; then
                d_start
                log_end_msg $?
        else
                log_end_msg 1
        fi
        ;;
    status)
        d_status
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|refresh|status}" >&2
        exit 3
        ;;
esac

exit 0
