#!/bin/bash

SYS_CONF="/etc/dnsmasq.conf"
BOOT_CONF="/boot/dnsmasq.conf"
CONF="/data/etc/dnsmasq.conf"

LOG="/var/log/dnsmasq.log"
PROG="/usr/sbin/dnsmasq"


test -x ${PROG} || exit 0

test -n "${OS_VERSION}" || source /etc/init.d/base

prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
test -s ${CONF} || exit 0


start() {
    msg_begin "Starting dnsmasq"
    
    iface=$(cat ${CONF} | grep interface | cut -d '=' -f 2)
    ip=$(cat ${CONF} | grep range | cut -d '=' -f 2 | cut -d '.' -f 1,2,3).1
    
    ifconfig ${iface} ${ip}

    ${PROG} -C ${CONF} --log-facility=${LOG}

    test $? == 0 && msg_done || msg_fail
}

stop() {
    msg_begin "Stopping dnsmasq"
    killall -q $(basename ${PROG})
    test $? == 0 && msg_done || msg_fail
}

case "$1" in
    start)
        start
        ;;
        
    stop)
        stop
        ;;
        
    restart)
        stop
        start
        ;;
    
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

