#!/bin/sh
#
# Startup script for the DNS caching server
#
# chkconfig: - 49 50
# description: This script starts your DNS caching server
# processname: dnsmasq
# pidfile: /run/dnsmasq.pid

# Setting this env variable to skip systemctl
SYSTEMCTL_SKIP_REDIRECT=1

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

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

nsName=""
if [[ -n "$2" ]]; then
    nsName="$2"
fi

hostsfile="/etc/hosts${nsName}"
configfile="/etc/dnsmasq${nsName}.conf"
pidfile=${PIDFILE-/var/run/dnsmasq${nsName}.pid}
lockfile=${LOCKFILE-/var/lock/subsys/dnsmasq${nsName}}

RETVAL=0

# See how we were called.
case "$1" in
  start)
        echo -n "Starting dnsmasq: "
        if ! status -p $pidfile -l $lockfile $dnsmasq > /dev/null; then
           $dnsmasq $OPTIONS -C $configfile -h -H $hostsfile --pid-file=$pidfile
	   RETVAL=$?
           echo
           [ $RETVAL -eq 0 ] && touch ${lockfile}
        fi
        ;;
  stop)
        echo -n "Shutting down dnsmasq: "
        killproc -p ${pidfile} ${dnsmasq}
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f ${lockfile} ${pidfile}
        ;;
  status)
	status -p $pidfile -l $lockfile $dnsmasq
        RETVAL=$?
	;;
  reload)
	echo -n "Reloading dnsmasq: "
	killproc -p ${pidfile} ${dnsmasq} -HUP
	RETVAL=$?
	echo
	;;
  restart)
	$0 stop $nsName
	$0 start $nsName
	RETVAL=$?
	;;
  condrestart)
	    if test "x`pidfileofproc dnsmasq`" != x; then
		$0 stop $nsName
		$0 start $nsName
		RETVAL=$?
	    fi
	    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
        exit 1
esac

exit $RETVAL

