#!/bin/sh
#
# avahi-dnsconfd:	Starts the Avahi dns configuration daemon
#
# chkconfig:		345 35 65
#
# description:	avahi-dnsconfd connects to a running avahi-daemon and runs the script \
#		/etc/avahi/dnsconf.action for each unicast DNS server that is announced \
#		on the local LAN. This is useful for configuring unicast DNS servers in \
#		a DHCP-like fashion with mDNS.
#
# $Id$


# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network ]; then
		msg_network_down "avahi-dnsconfd"
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/avahi-dnsconfd ]; then
		msg_already_running "avahi-dnsconfd"
		return
	fi

	msg_starting "avahi-dnsconfd"
	daemon /usr/sbin/avahi-dnsconfd -D
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/avahi-dnsconfd
}

stop() {
	if [ ! -f /var/lock/subsys/avahi-dnsconfd ]; then
		msg_not_running "avahi-dnsconfd"
		return
	fi

	# Stop daemons.
	msg_stopping "avahi-dnsconfd"
	avahi-dnsconfd -k && ok || fail
	rm -f /var/lock/subsys/avahi-dnsconfd
}

reload() {
	if [ ! -f /var/lock/subsys/avahi-dnsconfd ]; then
		msg_not_running "avahi-dnsconfd"
		RETVAL=7
		return
	fi

	msg_reloading "avahi-dnsconfd"
	avahi-dnsconfd -r
	RETVAL=$?
}

condrestart() {
	if [ ! -f /var/lock/subsys/avahi-dnsconfd ]; then
		msg_not_running "avahi-dnsconfd"
		RETVAL=$1
		return
	fi

	stop
	start
}

# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  reload|force-reload)
  	reload
	;;
  try-restart)
	condrestart 0
	;;
  status)
	status avahi-dnsconfd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|try-restart|status}"
	exit 3
esac

exit $RETVAL
