#!/bin/sh /etc/rc.common
# Copyright (C) 2023 ImmortalWrt.org

START=99
USE_PROCD=1

CONF="ddns-go"
YAML="/etc/$CONF/config.yaml"
PROG="/usr/bin/$CONF"

start_service() {
	config_load "$CONF"

	local enabled
	config_get_bool enabled "config" "enabled" "0"
	[ "$enabled" -eq "1" ] || return 1

	local listen ttl dns noweb insecure
	config_get listen "config" "listen" "[::]:9876"
	config_get ttl "config" "ttl" "300"
	config_get dns "config" "dns"
	config_get_bool noweb "config" "noweb" "0"
	config_get_bool insecure "config" "insecure" "0"

	[ -d "${YAML%/*}" ] || mkdir -p "${YAML%/*}"
	touch "$YAML"
	chown ddns-go "$YAML"

	procd_open_instance "$CONF"
	procd_set_param command "$PROG"
	procd_append_param command -c "$YAML"
	procd_append_param command -l "$listen"
	procd_append_param command -f "$ttl"
	[ -z "$dns" ] || procd_append_param command -dns "$dns"
	[ "$noweb" -eq "0" ] || procd_append_param command -noweb
	[ "$insecure" -eq "0" ] || procd_append_param command -skipVerify

	procd_set_param respawn
	procd_set_param stderr 1
	procd_set_param user ddns-go

	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}
