#!/bin/sh /etc/rc.common
#
# shellcheck disable=SC3043 # ash supports local

START=90
STOP=10

PROG=/usr/bin/strelaysrv
USE_PROCD=1

service_triggers() {
	procd_add_reload_trigger 'strelaysrv'
}

start_service() {
	config_load 'strelaysrv'

	local config_name="strelaysrv"
	local debug enabled ext_address global_rate keys listen memlimit \
		message_timeout nat nat_lease nat_renewal nat_timeout network_timeout \
		nice per_session_rate ping_interval pools pprof protocol provided_by \
		status_srv token user

	local gc maxprocs memlimit

	uci_validate_section 'strelaysrv' 'strelaysrv' "$config_name" \
		'debug:bool:0' \
		'enabled:bool:0' \
		'ext_address:string' \
		'gc:uinteger:0' \
		'global_rate:string' \
		'group:string:syncthing' \
		'keys:string:/etc/strelaysrv' \
		'listen:string::22067' \
		'maxprocs:uinteger:0' \
		'memlimit:uinteger:0' \
		'message_timeout:string' \
		'nat:bool:0' \
		'nat_lease:string' \
		'nat_renewal:string' \
		'nat_timeout:string' \
		'network_timeout:string' \
		'nice:range(-20,19):0' \
		'per_session_rate:string' \
		'ping_interval:string' \
		'pools:string' \
		'pprof:bool:0' \
		'protocol:or("tcp", "tcp4", "tcp6"):tcp' \
		'provided_by:string' \
		'status_srv:string' \
		'token:string' \
		'user:string:syncthing'

	[ "$enabled" = 1 ] || return 0

	[ -z "$group" ] && group=$(id -gn "$user")

	mkdir -p "$keys"
	[ -d "$keys" ] && chown -R "$user:$group" "$keys"

	procd_open_instance strelaysrv

	procd_set_param command "$PROG"

	[ "$gc" -le 0 ] || procd_append_param env GOGC="$gc"
	[ "$maxprocs" -le 0 ] || procd_append_param env GOMAXPROCS="$maxprocs"
	[ "$memlimit" -le 0 ] || procd_append_param env GOMEMLIMIT="$memlimit"

	[ "$debug" -eq 0 ] || procd_append_param command -debug
	[ -z "$ext_address" ] ||
		procd_append_param command -ext-address="$ext_address"
	[ -z "$global_rate" ] ||
		procd_append_param command -global-rate="$global_rate"
	procd_append_param command -keys="$keys"
	[ -z "$listen" ] || procd_append_param command -listen="$listen"
	[ -z "$message_timeout" ] ||
		procd_append_param command -message-timeout="$message_timeout"
	[ "$nat" -eq 0 ] || procd_append_param command -nat
	[ -z "$nat_lease" ] || procd_append_param command -nat-lease="$nat_lease"
	[ -z "$nat_renewal" ] ||
		procd_append_param command -nat-renewal="$nat_renewal"
	[ -z "$nat_timeout" ] ||
		procd_append_param command -nat-timeout="$nat_timeout"
	[ -z "$network_timeout" ] ||
		procd_append_param command -network-timeout="$network_timeout"
	[ -z "$per_session_rate" ] ||
		procd_append_param command -per-session-rate="$per_session_rate"
	[ -z "$ping_interval" ] ||
		procd_append_param command -ping-interval="$ping_interval"
	# pools is set to an empty value by default
	procd_append_param command -pools="$pools"
	[ "$pprof" -eq 0 ] || procd_append_param command -pprof
	[ -z "$protocol" ] || procd_append_param command -protocol="$protocol"
	[ -z "$provided_by" ] ||
		procd_append_param command -provided-by="$provided_by"
	# status-srv is set to an empty value by default
	procd_append_param command -status-srv="$status_srv"
	[ -z "$token" ] || procd_append_param command -token="$token"

	procd_set_param nice "$nice"
	procd_set_param term_timeout 15
	procd_set_param user "$user"
	procd_set_param group "$group"
	procd_set_param respawn
	procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

reload_service() {
	stop
	start
}
