#!/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="/usr/local/sbin/unitd"
start_precmd=start_precmd
_tmpprefix="/var/tmp/unit"

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

start_precmd()
{
    unitd_checktmpdir
    rm -f /var/run/unit/control.unit.sock
}

load_rc_config $name

# Default value
: ${unitd_sockfile=""}

pidfile=${unit_pidfile:-"/var/run/unit/unit.pid"}
unitd_enable=${unitd_enable:-"NO"}
sockfile=${unitd_sockfile}

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

run_rc_command "$1"
