#!/bin/sh
#
# $Header$
#

# PROVIDE: zoracl
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name="zoracl"
rcvar="${name}_enable"

load_rc_config $name

# Defaults
: ${zoracl_enable="NO"}
: ${zoracl_user="polyglot"}
: ${zoracl_root="/Berkanavt/polyglot"}
: ${zoracl_logdir="${zoracl_root}/log"}
: ${zoracl_rundir="${zoracl_root}/run"}
: ${zoracl_tmpdir="${zoracl_root}/temp"}
: ${zoracl_checkfile="${zoracl_rundir}/${name}_disable_check"}
: ${zoracl_start_log="${zoracl_logdir}/${name}.start"}
: ${zoracl_flags="subscribe -Fl -o+${zoracl_logdir}/subs --of-limit-records=100000 --tempdir=${zoracl_tmpdir}"}

command="${zoracl_root}/bin/zoracl"
pidfile="${zoracl_rundir}/${name}.pid"

# Custom commands
extra_commands="check recovery"
start_precmd="${name}_prestart"
start_cmd="${name}_start"
stop_postcmd="${name}_poststop"
check_cmd="${name}_check"
recovery_cmd="${name}_recovery"

zoracl_prestart()
{
	if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
		echo 1>&2 "${name} already running? (pid=$rc_pid)."
		return 1
	fi

	if [ -f $zoracl_checkfile ]; then
		rm -f $zoracl_checkfile
	fi

	if [ "$(eval $IDCMD)" = "root" ]; then
		if ! newsyslog -sR "${name} rc script" ${zoracl_start_log}; then
			return 1
		fi
	fi

	return 0
}

zoracl_start()
{
	echo "Starting ${name}."

	if [ -n "${zoracl_user}" ]; then
		if [ "${zoracl_user}" = "$(eval $IDCMD)" ]; then
			# unset $zoracl_user if running as that user
			unset zoracl_user
		fi
	fi           

	if ! daemon -p $pidfile ${zoracl_user:+-u$zoracl_user} $command \
		$rc_flags $command_args >/dev/null 2>${zoracl_start_log}
	then
		return 1
	fi

	return 0
}

zoracl_poststop()
{
	if ! touch $zoracl_checkfile; then
		return 1
	fi

	if [ -f "${pidfile}" ]; then
		rm -f $pidfile || true
	fi

	return 0
}

zoracl_check()
{
	if [ -n "$rc_pid" ]; then
		return 0
	else
		if [ -f "$zoracl_checkfile" ]; then
			return 0
		else
			echo "${name} is not running."
			return 1
		fi
	fi
}

zoracl_recovery()
{
	if ! zoracl_check; then
		zoracl_start
	fi
}

run_rc_command "$1"

