#!/bin/sh
#
# MeshCentral FreeBSD Service Script
#
# PROVIDE: meshcentral
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# Optional settings:
# meshcentral_enable (YES/NO): Enable or disable the service.
# meshcentral_daemon_user (system user): User to run the service as.
# meshcentral_daemon_group (system group): Group to run the service as.
# meshcentral_args (path): Additional arguements to pass to the application.
# meshcentral_chdir (path): Path to application data directory.
#
# Meshcentral will not work properly when setting "meshcentral_user" and "meshcentral_group" variables
# therefore it is necessary to substitute "meshcentral_user" with "meshcentral_daemon_user"
# and have the daemon invoke the command as the user

. /etc/rc.subr

name=meshcentral
desc="MeshCentral Computer Management"
rcvar=meshcentral_enable

load_rc_config $name

: ${meshcentral_enable:="NO"}
: ${meshcentral_args:=""}
: ${meshcentral_chdir:="/usr/local/meshcentral"}
: ${meshcentral_daemon_user:="meshcentral"}
: ${meshcentral_daemon_group:="meshcentral"}

pidfile="/var/run/${name}/${name}.pid"
node="/usr/local/bin/node"
command="/usr/sbin/daemon"
command_args="-u ${meshcentral_daemon_user} -P ${pidfile} -H -o /var/log/${name}/${name}.log ${node} node_modules/${name} ${meshcentral_args}"

start_precmd="meshcentral_startprecmd"

meshcentral_startprecmd()
{
    mkdir -p /var/run/${name}
    mkdir -p /var/log/${name}
    chown -R ${meshcentral_daemon_user}:${meshcentral_daemon_group} /var/run/${name}
    chown -R ${meshcentral_daemon_user}:${meshcentral_daemon_group} /var/log/${name}
}

run_rc_command "$1"
