#!/bin/sh
#
# PROVIDE: chess_server
# REQUIRE: LOGIN postgresql
# KEYWORD: shutdown
#
# rc.conf variables (setup-jail.sh sets them with sysrc):
#   chess_server_enable   YES to start at boot
#   chess_server_flags    chess-server arguments (-dsn, -jwt-secret-file, ...)
#   chess_server_env      environment, including PATH=... for stockfish
#   chess_server_account  unprivileged account (default: chess)
#   chess_server_binary   default: /usr/local/chess/chess-server
#   chess_server_logfile  default: /var/log/chess/chess-server.log
#
# daemon(8) starts as root, writes the supervisor pidfile, drops to the
# account, restarts the server if it exits (-r, 5 s delay), and reopens the
# log file on SIGHUP (-H) for newsyslog. Do not set chess_server_user: rc.subr
# would switch users before daemon runs and daemon could not drop privileges.

. /etc/rc.subr

name=chess_server
rcvar=chess_server_enable
load_rc_config $name

: ${chess_server_enable:="NO"}
: ${chess_server_account:="chess"}
: ${chess_server_binary:="/usr/local/chess/chess-server"}
: ${chess_server_logfile:="/var/log/chess/chess-server.log"}

pidfile="/var/run/${name}.pid"
procname="daemon"
command="/usr/sbin/daemon"
command_args="-f -r -R 5 -H -P ${pidfile} -o ${chess_server_logfile} -u ${chess_server_account} ${chess_server_binary} ${chess_server_flags}"

run_rc_command "$1"
