#!/bin/sh

# PROVIDE: rpi4b_temp_humidity
# REQUIRE: DAEMON
# BEFORE:
# KEYWORD: shutdown

. /etc/rc.subr

name=rpi4b_temp_humidity
rcvar=rpi4b_temp_humidity_enable 

command="/usr/sbin/daemon"
procname="/usr/local/bin/rpi4b-temp-humidity" 

start_precmd="${name}_prestart"

load_rc_config ${name}
: ${rpi4b_temp_humidity_enable:=NO} 
: ${rpi4b_temp_humidity_config_file:=""} 
: ${rpi4b_temp_humidity_strict_config:=NO} 
: ${rpi4b_temp_humidity_verbose:=YES} 
: ${rpi4b_temp_humidity_log_file:="/var/log/rpi4b-temp-humidity.log"} 

rpi4b_temp_humidity_prestart()
{
    if checkyesno rpi4b_temp_humidity_strict_config; then
        rc_flags="-s ${rc_flags}"
    fi
    if ! [ -z "${rpi4b_temp_humidity_config_file}" ]; then
        rc_flags="-f \"${rpi4b_temp_humidity_config_file}\" ${rc_flags}"
    fi
    if checkyesno rpi4b_temp_humidity_verbose; then
        rc_flags="-v ${rc_flags}"
    fi
    if ! [ -z "${rpi4b_temp_humidity_log_file}" ]; then
        # simple log rotation
        mv "${rpi4b_temp_humidity_log_file}" "${rpi4b_temp_humidity_log_file}.old"
        rpi4b_temp_humidity_daemon_flags="-o \"${rpi4b_temp_humidity_log_file}\""
        if ! mkdir -p "$(dirname "${rpi4b_temp_humidity_log_file}")"; then
            return 1
        fi
    else
        rpi4b_temp_humidity_daemon_flags="-f"
    fi
    rc_flags="${rpi4b_temp_humidity_daemon_flags} ${procname} ${rc_flags}"
}

run_rc_command "$1"
