#!/bin/sh
#
PROGNAME="ku-djbdns"
DAEMON="/usr/bin/dnscache"
DAEMONNAME="dnscache"
DESC="[ku] DJB DNS"
DEFAULTFILE="/etc/default/$PROGNAME"

# init options
#
SERVICE_ENABLE=false
SERVICE_SERVICEDIR='/etc/service'
SERVICE_OPTS=''
SERVICE_NICE_PARM=''
SERVICE_SERVICES="dnslocal dnscachex"

# internals
#
ignore_nice=false

test -x $DAEMON || exit 0

. /lib/lsb/init-functions
. /etc/default/rcS


do_start()
{
	$SERVICE_ENABLE || return 0

	# pre-start custom addons
	local svc=
	local desc=
	local svcdir=
	local djbdir=

	for svc in $SERVICE_SERVICES
	do
		[ -d /etc/$svc ] || {
			$manual_run && {
				echo "error, service dir /etc/$svc not exists!" >&2
			}
			continue
		}

		svcdir=$SERVICE_SERVICEDIR/$svc
		djbdir=/etc/$svc

		[ -f "$djbdir/desc" ] && desc=`cat "$djbdir/desc"` || desc="$svc"

		if [ -d $svcdir ]
		then
        		log_daemon_msg "Checking $DESC: $desc ($svc)"
			if svok $svcdir
			then
				log_end_msg 0
         			svc -u $svcdir
			else
				log_failure_msg "not running!"
				log_end_msg 1
			fi
		else
        		log_daemon_msg "Starting $DESC: $desc ($svc)"
			ln -s $djbdir $SERVICE_SERVICEDIR/.
			log_end_msg 0
		fi
	done
}


do_stop()
{
	$SERVICE_ENABLE || return 0

	# pre-start custom addons
	local services=`(cd $SERVICE_DJBDIR ; ls)`
	local svc=
	local desc=
	local svcdir=
	local djbdir=

	for svc in $SERVICE_SERVICES
	do
		svcdir=$SERVICE_SERVICEDIR/$svc
		djbdir=/etc/$svc

		[ -f "$djbdir/desc" ] && desc=`cat "$djbdir/desc"` || desc="$svc"

		if [ -d $svcdir ]
		then
        		log_daemon_msg "Stopping $DESC: $desc ($svc)"
			rm -f $svcdir
			svc -dx $djbdir
			svc -dx $djbdir/log
			log_end_msg 0
		else
			$manual_mode && echo "warning, service $svcdir not exists" >&2
    		fi
	done
}


do_stat()
{
	local svc=

	for svc in `ls -d $SERVICE_SERVICEDIR/* 2>/dev/null`
	do
		[ -L "$svc" ] && {
			svstat "$svc"
			svstat "$svc/log"
		}
	done
}


# (MAIN)

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"


[ -s $DEFAULTFILE ] && {
    . $DEFAULTFILE
    case "x$SERVICE_ENABLE" in
        xtrue|xfalse)
		;;
        #xinetd)
		#exit 0
		#;;
        *)	log_failure_msg "$DEFAULTFILE: SERVICE_ENABLE must be 'true' or 'false';"
		log_failure_msg "daemon not started"
		exit 1
		;;
    esac
    $ignore_nice || {
      case "x$SERVICE_NICE" in
        x[0-9])
		SERVICE_NICE_PARM="--nicelevel $SERVICE_NICE";;
        x[1-9][0-9])
		SERVICE_NICE_PARM="--nicelevel $SERVICE_NICE";;
        x)	;;
        *)
		log_warning_msg "$DEFAULTFILE: SERVICE_NICE_PARM need a value between 0 and 19"
		log_warning_msg "ignoring SERVICE_NICE now"
		;;
      esac
   }
}

[ -d $SERVICE_DJBDIR ] || {
	mkdir -p $SERVICE_DJBDIR || exit $?
}



action=$1
manual_mode=false

[ $# -gt 1] && {
	shift
	manual_mode=true
	SERVICE_SERVICES=$*
}

case "$action" in
  start)	do_start ;;
  stop)		do_stop ;;
  restart)
  	do_stop
	do_start
	;;
  reload|force-reload)
	do_stop
	do_start
	;;
  status)
	do_stat
	;;
  *)
	echo "usage: /etc/init.d/$PROGNAME {start|stop|reload|force-reload|restart|status}"
	exit 1
esac

exit 0
