#!/bin/sh
#
# $Header$

# PROVIDE: zora
# REQUIRE: mountsafe DAEMON
# KEYWORD: shutdown

# Define zora_* variables in /etc/rc.conf.d/zora
# Add the following line to this file to enable ZORA:
# zora_enable="YES" 
# 

. /etc/rc.subr

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

load_rc_config $name

: ${zora_enable="NO"}
: ${zora_user="webbase"}
: ${zora_dir="/Berkanavt/zora"}
: ${zora_config="${zora_dir}/config/config.zora"}
: ${zora_flags="-c ${zora_config}"}
: ${zora_start_log="${zora_dir}/logs/${name}.start"}

zora_spiderlookup="/Berkanavt/bin/scripts/spiderlookup"

zora_cluster_spider="${zora_dir}/config/cluster.spider"
zora_cluster_spider_perm=0664
zora_cluster_spider_baksfx=".PREV"

zora_cluster_zora="${zora_dir}/config/cluster.zora"
zora_cluster_zora_perm=0664
zora_cluster_zora_baksfx=".PREV"

command="${zora_dir}/bin/${name}"
command_args="${zora_flags}"

pidfile="${zora_dir}/run/${name}.pid"
checkfile="${zora_dir}/run/${name}.stoped"

required_files="${zora_config} ${zora_spiderlookup}"

extra_commands="checkup gen_cluster_conf"
start_precmd="${name}_prestart"
start_cmd="${name}_start"
stop_postcmd="${name}_poststop"
checkup_cmd="${name}_checkup"
gen_cluster_conf_cmd="${name}_gen_cluster_conf"

check_running ()
{
	if [ -n "$rc_pid" -a -z "$rc_fast" ] ; then
		return 0
	else
		return 1
	fi
}

zora_gen_cluster_conf()
{
	local _tmp_cluster_conf
	local _spiderlookup_args
	local _fname
	local _fname_perm
	local _fname_baksfx

	if [ X$1 = X"spider" ] ; then
		_spiderlookup_args="-fz"
		_fname=$zora_cluster_spider
		_fname_perm=$zora_cluster_spider_perm
		_fname_baksfx=$zora_cluster_spider_baksfx
	elif [ X$1 = X"zora" ] ; then
		_spiderlookup_args="-z"
		_fname=$zora_cluster_zora
		_fname_perm=$zora_cluster_zora_perm
		_fname_baksfx=$zora_cluster_zora_baksfx
	else
		echo 2>&1 "internal error!"
		return 1
	fi

	_tmp_cluster_conf=$(mktemp -t ${name})

	if ! $zora_spiderlookup $_spiderlookup_args > $_tmp_cluster_conf
	then
		echo 2>&1 "Cannot generate $_fname conf."
		rm -f $_tmp_cluster_conf 2>/dev/null
		return 1
	fi

	if diff -q $_fname $_tmp_cluster_conf >/dev/null 2>&1
	then
		# Cluster conf not changed
		rm -f $_tmp_cluster_conf 2>/dev/null
		return 0
	fi

	if [ ! -s $_tmp_cluster_conf ]; then
		# Some error during generation
		echo 2>&1 "Generated file $_fname is empty."
		rm -f $_tmp_cluster_conf 2>/dev/null
		return 1
	fi

	if ! install -o $zora_user -g wheel -m $_fname_perm \
		-B $_fname_baksfx $_tmp_cluster_conf $_fname
	then
		echo 2>&1 "Cannot install new $_fname conf."
		rm -f $_tmp_cluster_conf 2>/dev/null
		return 1
	fi

	rm -f $_tmp_cluster_conf 2>/dev/null
	return 0
}

zora_prestart ()
{
	if check_running ; then
		echo 1>&2 "${name} already running? (pid=$rc_pid)."
		return 1
	fi
	if [ -f $checkfile ]; then
		rm -f $checkfile
	fi
	if ! zora_gen_cluster_conf spider; then
		return 1
	fi
	if ! zora_gen_cluster_conf zora; then
		return 1
	fi
	if [ "$(eval $IDCMD)" = "root" ]; then
		if ! newsyslog -sR "${name} rc script" ${zora_start_log}; then
			return 1
		fi
	fi
	return 0
}

zora_start ()
{
	if ! daemon -p $pidfile -u $zora_user $command $command_args \
		>>${zora_start_log} 2>&1
	then
		echo "start ${name} failed"
		return 1
	else
		return 0
	fi 
}

zora_poststop ()
{
	if ! touch $checkfile; then
		echo "failed create ${checkfile}"
		return 1
	else
		return 0
	fi
}

zora_checkup ()
{
	if ! check_running ; then
		if [ -f "$checkfile" ]; then
			return 0
		else
			_run_rc_notrunning
			echo "${name} starting.."
			zora_start
		fi
	fi
}

run_rc_command "$1"

