#!/bin/sh

# PROVIDE: act_runner
# REQUIRE: NETWORKING SYSLOG
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable act_runner:
#
# act_runner_enable (bool):   Set to "NO" by default.
#                       Set it to "YES" to enable act_runner.

. /etc/rc.subr

name="act_runner"
rcvar="act_runner_enable"

load_rc_config "${name}"

: ${act_runner_user:="act_runner"}
: ${act_runner_enable:="NO"}
: ${act_runner_facility:="daemon"}
: ${act_runner_priority:="debug"}
: ${act_runner_config:="/usr/local/etc/act_runner/act_runner.conf"}
: ${act_runner_cache_dir:="/var/db/act_runner"}
: ${act_runner_log_dir:="/var/log/act_runner"}
: ${act_runner_run_dir:="/var/run/act_runner"}
: ${act_runner_log_file:="${act_runner_log_dir}/act_runner.log"}
: ${act_runner_args:="-c ${act_runner_config} daemon"}

exec_name="/usr/local/bin/act_runner"
command="/usr/sbin/daemon"

pidfile="${act_runner_run_dir}/${name}.pid"

required_files=${act_runner_config}

if [ -n "$act_runner_flags" ]; then
  echo "act_runner_flags has been removed. Adjust the variable to be act_runner_args."
  exit 1
fi

command_args=" -r -S -l ${act_runner_facility} -s ${act_runner_priority} \
    -T ${name} \
    -u ${act_runner_user} -P ${pidfile} \
    -o ${act_runner_log_file} ${exec_name} ${act_runner_args}"

start_precmd="${name}_prestart"
sig_stop=TERM

act_runner_prestart() {
  #  ensure the log directories are owned by the unprivileged user
  for d in "${act_runner_log_dir} ${act_runner_run_dir}"; do
    if [ ! -e "$d" ]; then
      install -d -o ${act_runner_user} -g ${act_runner_user} -m 755 ${d}
    fi
  done
}

run_rc_command "$1"
