#!/bin/bash

# Copyright 2005 Sam Trenholme

# TERMS

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:

# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.

# This software is provided 'as is' with no guarantees of correctness or
# fitness for purpose.

# chkconfig: 345 55 45
# description: This shell script takes care of starting and stopping \
#              the MaraDNS process.
#              A DNS server is a program that performs the DNS \
#              conversion from hostnames in to ipaddresses.


# The following is a pointer to the MaraDNS program
MARADNS="/usr/sbin/maradns"
#MARADNS="/usr/local/sbin/maradns"

# The following is a pointer to the duende daemonizer
DUENDE="/usr/sbin/duende"
#DUENDE="/usr/local/sbin/duende"

# The following is the directory we place MaraDNS log entries in
LOGDIR="/var/log/maradns"

# The following is a list of all mararc files which we will load or
# unload;
# Simple case: Only one MaraDNS process, using the /etc/maradns/mararc.* file
MARARCS="/etc/maradns/mararc.recursive"
# Authorative dns server
#MARARCS="/etc/maradns/mararc.authorative"
# Full dns server
#MARARCS="/etc/maradns/mararc.full"
#
# Case two: Three MaraDNS processes, one using /etc/maradns/mararc.1, the second one
# using /etc/maradns/mararc.2, and the third one using /etc/maradns/mararc.3
#MARARCS="/etc/maradns/mararc.1 /etc/maradns/mararc.2 /etc/maradns/mararc.3"

# Show usage information if this script is invoked with no arguments
if [ $# -lt 1 ] ; then
    echo Usage: $0 \(start\|stop\|restart\)
    exit 1
fi

# If invoked as stop or restart, kill *all* MaraDNS processes
if [ $1 = "stop" -o $1 = "restart" ] ; then
    echo Sending all MaraDNS processes the TERM signal
    ps -ef | awk '{print $2":"$8}' | grep maradns | grep -v $$ | \
      cut -f1 -d: | xargs kill > /dev/null 2>&1
    echo waiting 1 second
    sleep 1
    echo Sending all MaraDNS processes the KILL signal
    ps -e | awk '{print $1":"$NF}' | grep maradns | grep -v $$ | \
      cut -f1 -d: | xargs kill -9 > /dev/null 2>&1
    echo MaraDNS should have been stopped
fi

# If invoked as start or restart, start the MaraDNS processes
if [ $1 = "start" -o $1 = "restart" ] ; then
    echo Starting all maradns processes
    for a in $MARARCS ; do
        echo Starting maradns process which uses Mararc file $a
	# Duende syslogs MaraDNS' output messages and daemonizes MaraDNS
        $DUENDE $MARADNS -f $a
    done
fi

