#!/bin/sh
#
# PROVIDE: skynetd
# REQUIRE: LOGIN
# REQUIRE: sshd mountdb
# KEYWORD: shutdown
#
# Define skynetd_* variables in /etc/rc.conf.d/skynetd
# Add the following line to this file to enable skynetd:
# skynetd_enable="YES"
#
# Documentation about system:
# http://wiki.yandex-team.ru/JandeksPoisk/Sepe/Skynet
#

. /etc/rc.subr

name="skynetd"
rcvar=`set_rcvar`

load_rc_config $name

: ${skynetd_enable="YES"}
: ${skynetd_user="root"}
: ${skynetd_dir="/skynet"}
: ${skynetd_limits="-e -C skynet"}

required_dirs="${skynetd_dir}"

scommand="/skynet/python/bin/python ${skynetd_dir}/startup/up.py"

if [ "$1" = "--no-lock" ]; then
    shift
    scommand="${scommand} --no-lock"
fi

mode="$1"

shift

start_precmd="${name}_precmd"
start_cmd="echo \"$scommand start \"$@\"\" | su -m $skynetd_user"
stop_precmd="${name}_precmd"
stop_cmd="echo \"$scommand stop \"$@\"\" | su -m $skynetd_user"
restart_precmd="${name}_precmd"
restart_cmd="echo \"$scommand restart \"$@\"\" | su -m $skynetd_user"

extra_commands="autostart check"
autostart_precmd="${name}_precmd"
autostart_cmd="echo \"$scommand autostart\" | su -m $skynetd_user > /dev/null 2> /dev/null"

check_precmd="${name}_precmd"
check_cmd="echo \"$scommand check \"$@\"\" | su -m $skynetd_user"

skynetd_precmd()
{
    export PATH=/usr/local/bin:/usr/bin:/bin
    export USER=${skynetd_user}
    export HOME=${skynetd_dir}

    cd ${skynetd_dir}

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

run_rc_command "$mode"
