#!/bin/sh

# PROVIDE: rustdesk_hbbr
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# rustdesk_hbbr_enable (bool):            Set to NO by default.
#               Set it to YES to enable rustdesk_hbbr.
# rustdesk_hbbr_args (string):            Set extra arguments to pass to rustdesk_hbbr
#               Default is "-k _".
# rustdesk_hbbr_user (string):            Set user that rustdesk_hbbr will run under
#               Default is "root".
# rustdesk_hbbr_group (string):           Set group that rustdesk_hbbr will run under
#               Default is "wheel".

. /etc/rc.subr

name=rustdesk_hbbr
desc="Rustdesk Relay Server"
rcvar=rustdesk_hbbr_enable

load_rc_config $name

: ${rustdesk_hbbr_enable:=NO}
: ${rustdesk_hbbr_args="-k _"}
: ${rustdesk_hbbr_user:=rustdesk}
: ${rustdesk_hbbr_group:=rustdesk}

pidfile=/var/run/rustdesk_hbbr.pid
command=/usr/sbin/daemon
procname=/usr/local/sbin/hbbr
rustdesk_hbbr_chdir=/var/db/rustdesk-server
command_args="-p ${pidfile} -o /var/log/rustdesk-hbbr.log ${procname} ${rustdesk_hbbr_args}"
## If you want the daemon do its log over syslog comment out the above line and remove the comment from the below replacement
#command_args="-p ${pidfile} -T ${name} ${procname} ${rustdesk_hbbr_args}"

start_precmd=rustdesk_hbbr_startprecmd

rustdesk_hbbr_startprecmd()
{
    if [ -e ${pidfile} ]; then
        chown ${rustdesk_hbbr_user}:${rustdesk_hbbr_group} ${pidfile};
    else
        install -o ${rustdesk_hbbr_user} -g ${rustdesk_hbbr_group} /dev/null ${pidfile};
    fi
    if [ -e ${rustdesk_hbbr_chdir} ]; then
        chown -R ${rustdesk_hbbr_user}:${rustdesk_hbbr_group} ${rustdesk_hbbr_chdir};
        chmod -R 770 ${rustdesk_hbbr_chdir};
    else
        mkdir -m 770 ${rustdesk_hbbr_chdir};
        chown ${rustdesk_hbbr_user}:${rustdesk_hbbr_group} ${rustdesk_hbbr_chdir};
    fi
    if [ -e /var/log/rustdesk-hbbr.log ]; then
        chown -R ${rustdesk_hbbr_user}:${rustdesk_hbbr_group} /var/log/rustdesk-hbbr.log;
        chmod 660 /var/log/rustdesk-hbbr.log;
    else
        install -o ${rustdesk_hbbr_user} -g ${rustdesk_hbbr_group} /dev/null /var/log/rustdesk-hbbr.log;
        chmod 660 /var/log/rustdesk-hbbr.log;
    fi
}

run_rc_command "$1"
