#!/bin/sh

# PROVIDE: unitd
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable nginx:
# unitd_enable (bool):          Set to "NO" by default.
#                               Set it to "YES" to enable unit
# unitd_sockfile (str):         Set to "" by default.
#                               Set it to "127.0.0.1:8443" to enable the unit control
#                               socket on the network address

. /etc/rc.subr

name="unitd"
rcvar=unitd_enable

command="%%PREFIX%%/sbin/unitd"
start_precmd=start_precmd
_tmpprefix="%%UNIT_TMPDIR%%"

unitd_checktmpdir()
{
    if [ ! -d ${_tmpprefix} ] ; then
        install -d -o %%WWWOWN%% -g %%WWWGRP%% -m 755 ${_tmpprefix}
    else
        chown -R %%WWWOWN%%:%%WWWGRP%% ${_tmpprefix}
    fi
}

start_precmd()
{
    unitd_checktmpdir
    rm -f %%UNIT_SOCK%%
}

load_rc_config $name

# Default value
: ${unitd_sockfile=""}

pidfile=${unit_pidfile:-"%%UNIT_PIDFILE%%"}
unitd_enable=${unitd_enable:-"NO"}
sockfile=${unitd_sockfile}

if [ x${sockfile} != "x" ]
then
    command_args="--control ${sockfile}"
fi

run_rc_command "$1"
