#!/bin/sh

# PROVIDE: lidarr
# REQUIRE: LOGIN network
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf or use sysrc to enable lidarr
# ${lidarr_enable="YES"}
# Optionaly there are some other parameters
# ${lidarr_user="lidarr"}
# ${lidarr_group="lidarr"}
# This stores lidarr data (e.g., databases, configs, logs)
# ${lidarr_data_dir="/usr/local/lidarr"}
# This stores the PID files that daemon will use
# ${lidarr_pid_dir:="/var/run/lidarr"}

. /etc/rc.subr
name=lidarr
rcvar=lidarr_enable
load_rc_config $name

: ${lidarr_enable:=NO}
: ${lidarr_user:="lidarr"}
: ${lidarr_group:="lidarr"}
: ${lidarr_exec_dir:="/usr/local/share/lidarr/bin"}
: ${lidarr_data_dir:="/usr/local/lidarr"}
: ${lidarr_pid_dir:="/var/run/lidarr"}

pidfile="${lidarr_pid_dir}/${name}_daemon.pid"
pidfile_child="${lidarr_pid_dir}/${name}_child.pid"
command="/usr/sbin/daemon"


start_precmd=${name}_precmd
lidarr_precmd() {
    export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

    if [ ! -d ${lidarr_data_dir} ]; then
        install -d -o ${lidarr_user} -g ${lidarr_group} ${lidarr_data_dir}
    fi
    if [ ! -d ${lidarr_pid_dir} ]; then
        install -d -o ${lidarr_user} -g ${lidarr_group} ${lidarr_pid_dir}
    fi

    # .NET 6+ use dual mode sockets to avoid the separate AF handling.
    # disable .NET use of V6 if no ipv6 is configured.
    # See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=259194#c17
    ifconfig -a -u -G lo | grep -q inet6
    if [ $? == 1 ]; then
        export DOTNET_SYSTEM_NET_DISABLEIPV6=1
    fi

    if [ `uname -K` -ge 1400092 ]; then
        export CLR_OPENSSL_VERSION_OVERRIDE=30
    fi

    rc_flags="-r -f -p ${pidfile_child} -P ${pidfile} ${lidarr_exec_dir}/Lidarr --data=${lidarr_data_dir} --nobrowser >> /dev/null 2>&1 ${rc_flags}"
}

run_rc_command "$1"
