
#!/bin/sh
#
# PROVIDE: stored
# REQUIRE: network
# REQUIRE: db
# REQUIRE: daemon
# KEYWORD: shutdown
#
# Documentation about system:
# http://wiki.yandex-team.ru/Robot/PageRank
#

. /etc/rc.subr

name="stored"
rcvar=`set_rcvar`

load_rc_config $name

: ${stored_enable="NO"}
: ${stored_user="pagerankstore"}
: ${stored_dir="/Berkanavt/mesh/store"}
: ${stored_limits="-e -C worm"}
: ${stored_port="35025"}
: ${stored_master="orange2-091.fb.yandex.ru"}
: ${stored_acl="${stored_dir}/acl.cfg"}

stored_host=`hostname -f|sed -e 's/yandex/fb.yandex/g'`

if [ $stored_host = $stored_master ]; then
	pidfile="${stored_dir}/master.pid"
else
        pidfile="${stored_dir}/node.pid"
fi

command="${stored_dir}/bin/stored"

if [ $stored_host = $stored_master ]; then
	command_args="master -D -c ${stored_dir}/store.cfg -l ${stored_dir}/stored_master.log -s ${stored_acl}"
else
	command_args="node -D -m ${stored_master}:${stored_port} -a ${stored_host}:${stored_port} -d ${stored_dir}/node -l ${stored_dir}/stored_node.log"
fi

required_dirs="${stored_dir}"

start_precmd="stored_precmd"

stop_precmd="stored_stop_precmd"

extra_commands="check"
check_cmd="stored_check"

stored_check() {
    if [ -z `check_pidfile ${pidfile} ${command}` ]; then
	if [ ! -r ${stored_dir}/normal_stop.tag ]; then
	        run_rc_command start
	fi
    fi
}

stored_precmd()
{
    export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:${stored_dir}/bin
    export USER=${stored_user}
    export HOME=${stored_dir}

    cd ${stored_dir}

    if [ -r ${stored_dir}/normal_stop.tag ]; then
	rm ${stored_dir}/normal_stop.tag
    fi

    if [ -n "${stored_limits}" ]; then
        eval `/usr/bin/limits ${stored_limits}` 2>/dev/null
    fi
}

stored_stop_precmd()
{
    if [ ! -r ${stored_dir}/normal_stop.tag ]; then
	touch ${stored_dir}/normal_stop.tag
    fi
}

run_rc_command "$1"
