#!/bin/sh
# Begin /etc/init.d/
# DNS MASQ is a simple DNS+DHCP server
# The configuration file is : /etc/dnsmasq.conf
# The data files used by dnsmasq include:
#    /etc/hosts
#    /etc/resolv.conf/dnsmasq
#
# (c) Eko M. Budi for Vector Linux
# License : GNU GPL
#
#

# Include the functions declared in the /etc/rc.d/functions file
source /etc/rc.d/functions

# path to binary
ROOT="/usr/sbin"
# name of binary to run
APP="dnsmasq"
#runtime options
#OPT=""

#server name & description
SERVER="DNSMASQ server"

service_start()
{
    # if old resolv.conf contains a nameserver
    if [ -r /etc/resolv.conf ]; then
	line=$(grep "nameserver" /etc/resolv.conf);
	if [ "$line" ]; then
	    if ! echo $line | grep -q "127.0.0.1"; then
		cp /etc/resolv.conf /etc/resolv.conf.dnsmasq
	    fi
	fi
    fi
    HOSTNAME=`hostname`
    echo "search ${HOSTNAME#*.}" > /etc/resolv.conf
    echo "nameserver 127.0.0.1" >> /etc/resolv.conf 
    loadproc $ROOT/$APP $OPT
}

service_stop()
{
    killproc $ROOT/$APP

    # Copy back resolf.conv
    if [ -r /etc/resolv.conf.dnsmasq ]; then
	cp /etc/resolv.conf.dnsmasq /etc/resolv.conf
    fi
}


case "$1" in
        start)
                echon "Starting $SERVER............"
                service_start
                ;;
        reload)
                echon "Reloading $SERVER..........."
                checkloadproc $ROOT/$APP $OPT
                ;;
        stop)
                echon "Stopping $SERVER............"
                service_stop
                ;;
        restart)
                $0 stop
                /bin/sleep 1
                $0 start
                ;;
        status)
                statusproc $ROOT/$APP
                ;;
        *)
                echo "Usage: $0 {start|stop|reload|restart|status}"
                exit 1
        ;;

esac

# End /etc/init.d/
