#!/usr/sh
#
# Start/stop cego daemon 
#

. /etc/rc.subr(2)

name="cegod"
rcvar=`set_rcvar`
start_cmd="${name}_start"
stop_cmd="${name}_stop"

load_rc_config $name

# are we called with a profile ?
if [ -n "$2" ]; then
	profile="$2"
	if [ "x${named_profiles}" != "x" ]; then
		eval named_enable="\${named_${profile}_enable:-${named_enable}}"
		eval chrootdir="/serv/dns/${profile}/"
		eval flags=\"-t ${chrootdir} -c conf -u bind\"
		pidfile="$chrootdir$pidfile"
		required_files="${chrootdir}${configfile}"
	else
		echo "$0: extra argument ignored"
	fi
else
	# we are called without a profile, but probably as a profile
	if [ "x${named_profiles}" != "x" -a "x$1" != "x" ]; then
		for profile in ${named_profiles}; do
			eval _enable="\${named_${profile}_enable}"
			case "x${_enable:-${named_enable}}" in
			x|x[Nn][Oo]|x[Nn][Oo][Nn][Ee])
				continue
				;;
			x[Yy][Ee][Ss])
				;;
			*)
				if test -z "$_enable"; then
					_var=named_enable
				else
					_var=named_"${profile}"_enable
				fi
				echo "Bad value" \
				    "'${_enable:-${named_enable}}'" \
				    "for ${_var}. " \
				    "Profile ${profile} skipped."
				continue
				;;
			esac
			echo "===> named profile: ${profile}"
			# echo "                    ${flags}"
			/client/rc.d/named $1 ${profile}
			retcode="$?"
			if [ "0${retcode}" -ne 0 ]; then
				failed="${profile} (${retcode}) ${failed:-}"
			else
				success="${profile} ${success:-}"
			fi
		done
		exit 0
	else
		# setup the default cases
		pidfile="$chrootdir$pidfile"
	fi
fi

cegod_start()
{
    echo "Starting cego daemon .."
    $CEGO --mode=daemon --dbxml=$CGXML --logfile=$CGLOG --tableset=$TS &
    rc=$?
}

cegod_stop()
{
    echo "Stopping cego daemon .."
    PID=`cat /usr/local/cegodb/pid`
    /usr/bin/kill -s INT $PID 
}



run_rc_command "$1"


# location of the cego program
CEGO=/usr/local/bin/cego
# location of the cego configuration file
CGXML=/usr/local/cegodb/cegodb.xml
# location of the cego log file
CGLOG=/usr/local/cegodb/cego.log
# name of the tableset, which should be started up
TS=TS1

rc=0
