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

. /lib/functions.sh
. /lib/functions/procd.sh

USE_PROCD=1

START=99
STOP=10

init_conf() {
	config_load "naiveproxy"
	config_get "enable" "config" "enable" "0"
	config_get "listen_addr" "config" "listen_addr"
	config_get "server_addr" "config" "server_addr"
	config_get "extra_argument" "config" "extra_argument"
}

start_service() {
	init_conf
	[ "${enable}" == "1" ] || return 0

	procd_open_instance naiveproxy

	procd_set_param command naive
	procd_append_param command --listen="${listen_addr}"
	procd_append_param command --proxy="${server_addr}"
	[ -n "${extra_argument}" ] && procd_append_param command "${extra_argument}"

	procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
	procd_set_param limits core="unlimited"
	procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

reload_service()
{
	stop
	start
}

service_triggers() {
        procd_add_reload_trigger "naiveproxy"
}
