#! /bin/sh
# 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/dnsmasq
NAME="dnsmasq"
DESC="caching dns forwarder"

# change these lines if you want dnsmasq to serve an MX record.
# only one need be set, the other defaults to the name of the host 
# running dnsmasq
#MAILHOSTNAME=""
#MAILTARGET=""
# change this line if you want dns to get its upstream servers from
# somewhere other that /etc/resolv.conf 
#RESOLV_CONF=""
# You might want to set this to "root" if RESOLV.CONF is set 
# to /etc/ppp/resolv.conf since /etc/ppp/* are not world readable
#DNSMASQ_USER="root"
# If you want dnsmasq to listen for requests on only one interface
# (and the loopback) give the name of the interface (eg eth0) here
#DNSMASQ_INTERFACE="eth0"
# change these if you want dnsmasq to cache any "hostname" 
# or "client-hostname" from a dhcpd's lease file
#DHCP_LEASE="/var/lib/dhcp/dhcpd.leases"
#DOMAIN_SUFFIX=`dnsdomainname`

test -f $DAEMON || exit 0

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --pidfile \
                /var/run/$NAME.pid --exec $DAEMON -- \
                ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
		${MAILTARGET:+ -t $MAILTARGET} \
                ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
                ${DNSMASQ_INTERFACE:+ -i $DNSMASQ_INTERFACE} \
                ${DHCP_LEASE:+ -l $DHCP_LEASE} \
                ${DOMAIN_SUFFIX:+ $DOMAIN_SUFFIX} \
		${RESOLV_CONF:+ -r $RESOLV_CONF}
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON
	echo "$NAME."
	;;
  reload|force-reload)
        echo "Reloading $DESC configuration files."
        start-stop-daemon --stop --signal 1 --quiet --pidfile \
               /var/run/$NAME.pid --exec $DAEMON
        ;;
  restart)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON -- \
                ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
		${MAILTARGET:+ -t $MAILTARGET} \
                ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
                ${DNSMASQ_INTERFACE:+ -i $DNSMASQ_INTERFACE} \
                ${DHCP_LEASE:+ -l $DHCP_LEASE} \
                ${DOMAIN_SUFFIX:+ $DOMAIN_SUFFIX} \
                ${RESOLV_CONF:+ -r $RESOLV_CONF}
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
