#!/bin/sh
#
# Mediamtx FreeBSD Service Script
#
# PROVIDE: mediamtx
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Optional settings:
# mediamtx_enable (YES/NO): Enable or disable the service.
# mediamtx_user (system user): User to run the service as.
# mediamtx_group (system group): Group to run the service as.
# mediamtx_config (file): Path to application configuration file.

. /etc/rc.subr

name=mediamtx
desc="MediaMTX Media Streaming Server"
rcvar=mediamtx_enable

load_rc_config $name

: ${mediamtx_enable:="NO"}
: ${mediamtx_config:="/usr/local/www/${name}/${name}.yml"}
: ${mediamtx_user:="mediamtx"}
: ${mediamtx_group:="mediamtx"}

pidfile="/var/run/${name}/${name}.pid"
required_files="${mediamtx_config}"
command=/usr/sbin/daemon
command_args="-P ${pidfile} -H -o /var/log/${name}/${name}.log /usr/local/bin/${name} ${mediamtx_config}"

start_precmd="mediamtx_startprecmd"

mediamtx_startprecmd()
{
    mkdir -p /var/run/${name}
    mkdir -p /var/log/${name}
    chown -R ${mediamtx_user}:${mediamtx_group} /var/run/${name}
    chown -R ${mediamtx_user}:${mediamtx_group} /var/log/${name}
}

run_rc_command "$1"
