#!/bin/sh
# shellcheck disable=SC2317  # Don't warn about unreachable commands in this file

DAEMON=mdnsd
MDNSD=/usr/sbin/$DAEMON
PIDFILE=/var/run/$DAEMON.pid
CFGFILE=/etc/default/$DAEMON

MDNSD_ARGS=""

# Read configuration variable file if it is present
# shellcheck source=/dev/null
[ -r "$CFGFILE" ] && . "$CFGFILE"

# shellcheck disable=SC2086
start() {
	[ -n "$1" ] || printf 'Starting %s: ' "$DAEMON"
	start-stop-daemon -S -q -p "$PIDFILE" -x "$MDNSD" -- $MDNSD_ARGS
}

stop() {
	[ -n "$1" ] || printf 'Stopping %s: ' "$DAEMON"
	start-stop-daemon -K -q -p "$PIDFILE" -x "$MDNSD"
}

restart() {
	printf 'Restarting %s: ' "$DAEMON"
	stop  silent
	start silent
}

# SIGHUP reloads /etc/mdns.d/*.service
reload() {
	printf 'Reloading %s: ' "$DAEMON"
	start-stop-daemon -K -s HUP -q -p "$PIDFILE" -x "$MDNSD"
}

case "$1" in
	start|stop|restart|reload)
		"$1"
		status=$?
		if [ "$status" -eq 0 ]; then
			echo "OK"
		else
			echo "FAIL"
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|reload}"
		exit 1
		;;
esac

exit "$status"
