#!/bin/sh

start() {
	if [ ! -f /var/run/dbus/avahi_flag ]; then
		#dont force restart dbus, as that can kill other processes already attached to dbus.
		if dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ListNames >/dev/null 2>&1; then
			/usr/local/etc/init.d/dbus reload
		else
			/usr/local/etc/init.d/dbus restart
		fi
		touch /var/run/dbus/avahi_flag
	fi
	if ! pidof avahi-daemon >/dev/null; then
		/usr/local/sbin/avahi-daemon -D
	fi
}

stop() {
	/usr/local/sbin/avahi-daemon -k
}

status() {
	if pidof avahi-daemon >/dev/null; then
		echo -e "\navahi is running.\n"
		exit 0
	else
		echo -e "\navahi is not running.\n"
		exit 1
	fi	
}

case $1 in
	start) start
		;;
	stop) stop
		;;
	status) status
		;;
	restart) stop; start
		;;
	*) echo -e "\n$0 [start|stop|restart|status]\n"
		;;
esac
