#!/sbin/openrc-run
#
# OpenRC init script for Avahi mDNS/DNS-SD daemon.
#

description="Avahi mDNS/DNS-SD Daemon"

DAEMON=/usr/sbin/avahi-daemon
DISABLE_TAG="/var/run/avahi-daemon/disabled-for-unicast-local"

depend() {
	need localmount
	use dbus logger
	after bootmisc
}

load_defaults() {
	[ -f /etc/default/avahi-daemon ] && . /etc/default/avahi-daemon
}

disabled_for_unicast_local() {
	[ -e "$DISABLE_TAG" ] && [ "$AVAHI_DAEMON_DETECT_LOCAL" != "0" ]
}

start() {
	[ -x "$DAEMON" ] || return 0
	load_defaults
	$DAEMON -c && return 0
	if disabled_for_unicast_local; then
		ewarn "avahi-daemon disabled because there is a unicast .local domain"
		return 0
	fi

	ebegin "Starting avahi-daemon"
	$DAEMON -D
	eend $?
}

stop() {
	[ -x "$DAEMON" ] || return 0

	ebegin "Stopping avahi-daemon"
	if $DAEMON -c; then
		$DAEMON -k
	fi
	eend 0
}

reload() {
	[ -x "$DAEMON" ] || return 0

	ebegin "Reloading avahi-daemon"
	$DAEMON -c && $DAEMON -r
	eend $?
}

status() {
	$DAEMON -c && { einfo "$description is running"; return 0; }
	einfo "$description is not running"
	return 3
}
