#!/bin/sh

# PROVIDE: adguardhome
# REQUIRE: DAEMON NETWORKING
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable adguardhome:
# adguardhome_enable : set to "YES" to enable the daemon, default is "NO"

. /etc/rc.subr

name="adguardhome"
rcvar="${name}_enable"

load_rc_config "$name"

: "${adguardhome_enable:=NO}"
: "${adguardhome_config:=%%PREFIX%%/etc/AdGuardHome.yaml}"
: "${adguardhome_user:=%%USER%%}"
: "${adguardhome_group:=%%GROUP%%}"
: "${adguardhome_work_dir:=/var/db/adguardhome}"
: "${adguardhome_svcj_options:=net_basic}"
: "${adguardhome_extra:=}"

# old values
: "${adguardhome_config_old:=%%PREFIX%%/bin/AdGuardHome.yaml}"
: "${adguardhome_work_dir_old:=%%PREFIX%%/bin}"

logfile="/var/log/${name}.log"
pidfile="/var/run/${name}.pid"

command="/usr/sbin/daemon"
command_args="-H -o $logfile -t $name -P $pidfile %%PREFIX%%/bin/adguardhome -c $adguardhome_config -w $adguardhome_work_dir $adguardhome_extra"

start_precmd="${name}_prestart"
extra_commands="reload"

adguardhome_prestart()
{
    # check if the old config exists
    if [ -f "$adguardhome_config_old" ] || [ -d "${adguardhome_work_dir_old}/data" ]; then
        if ! [ -f "$adguardhome_config" ] && ! [ -d "${adguardhome_work_dir}/data" ]; then
            # old config exists, new config doesn't exist
            # move old config to new locations
            if [ -f "$adguardhome_config_old" ]; then
                echo "moving old config $adguardhome_config_old to the new location $adguardhome_config"
                mv "$adguardhome_config_old" "$adguardhome_config"
            fi
            if [ -d "${adguardhome_work_dir_old}/data" ]; then
                install -d -m 0750 -o "$adguardhome_user" \
                        -g "$adguardhome_group" "$adguardhome_work_dir"
                echo "moving old config ${adguardhome_work_dir_old}/data to the new location ${adguardhome_work_dir}/data"
                mv "${adguardhome_work_dir_old}/data" "${adguardhome_work_dir}/data"
            fi
        else # both old and new config exist
            echo "(!!!) WARNING =================================================================="
            echo "(!!!) WARNING Both old and new configs exist"
            echo "(!!!) WARNING Please choose which configs do you need and remove the old configs"
            echo "(!!!) WARNING OLD config locations: $adguardhome_config_old ${adguardhome_work_dir_old}/data"
            echo "(!!!) WARNING NEW config locations: $adguardhome_config ${adguardhome_work_dir}/data"
            echo "(!!!) WARNING =================================================================="
        fi
    fi

    # install dir
    if [ ! -d "$adguardhome_work_dir" ]; then
        install -d -m 0750 -o "$adguardhome_user" -g "$adguardhome_group" \
                "$adguardhome_work_dir"
    fi
}

run_rc_command "$1"
