#!/bin/sh
# Startup script for the DNS caching server under ALTLinux
#
# chkconfig: - 20 55
# description: This script starts your DNS caching server
# processname: dnsmasq
# config: /etc/dnsmasq.conf
# pidfile: /var/run/dnsmasq.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

if [ "$1" = "--trace-script" ]; then
    shift
    set -x
fi

# Source networking configuration and check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network
is_yes "${NETWORKING}" || exit 0

dnsmasq=/usr/sbin/dnsmasq
[ -x $dnsmasq ] || exit 0

# Internal variables
PIDFILE=/var/run/dnsmasq.pid
LOCKFILE=/var/lock/subsys/dnsmasq
RETVAL=0

#---------------------------------------------------------------
#
#	Status helpers
#

function print_all_ifaces()
{
    local linenum=0
    netstat -vai | while read iface moredata; do
	[ $[++linenum] -le 2 ] && continue
	case "$iface" in
	    lo )  ;;  # skip loopback
    #       *:* ) ;;  # skip aliases?
	    * ) echo $iface ;;
	esac
    done
}

function print_listening_ifaces()
{
    local line words dev
    netstat -ltnp | egrep '[0-9]*/dnsmasq' | egrep ':53 ' | while read line; do
	words=($line)
	dev="${words[3]%:*}"
	if [ "$dev" = "0.0.0.0" ]; then
	    print_all_ifaces
	    break
	fi
	echo "$dev"
    done | uniq
}

#---------------------------------------------------------------
#
#	Main routines
#

function start()
{
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname dnsmasq --expect-user nobody -- /usr/sbin/dnsmasq-helper start
    RETVAL=$?
    return $RETVAL
}

function start_debug()
{
    "$RESOLVCONF" -u
    # don't adds/removes routing!
    $dnsmasq -d -q $OPTIONS
}

function stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user nobody -- $dnsmasq
    RETVAL=$?
    return $RETVAL
}

function my_status() {
    status --pidfile "$PIDFILE" --expect-user nobody -- $dnsmasq
    RETVAL=$?
    if [ $RETVAL = 0 ]; then
	echo "Listening interfaces:"
	print_listening_ifaces
    fi
    return $RETVAL
}

function restart() {
    stop
    start
    RETVAL=$?
    return $RETVAL
}

function reload() {
    msg_reloading dnsmasq
    stop_daemon --pidfile "$PIDFILE" --expect-user nobody -HUP -- $dnsmasq
    RETVAL=$?
    return $RETVAL
}

function clean() {
    stop
    $RM -f $LOCKFILE $PID_FILE
    /usr/sbin/dnsmasq-helper cleanup_routing
    RETVAL=0
}

case "$1" in
    start)
		start
		/usr/sbin/dnsmasq-helper poststart "$?"
		;;
    stop)
		stop
		/usr/sbin/dnsmasq-helper poststop
		;;
    status)  my_status ;;
    restart) restart ;;
    reload)  reload ;;
    clean)   clean ;;
    startdebug)  start_debug ;;
    condrestart) [ -e "$LOCKFILE" ] && restart ;;
    condreload)  [ -e "$LOCKFILE" ] && reload ;;
    condstop)    [ -e "$LOCKFILE" ] && stop ;;
    *)
        echo "Usage: ${0##*/} {start|stop|restart|reload|condrestart|condreload|condstop|clean|status}"
        exit 1
esac

exit $RETVAL

## EOF ##
