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

USE_PROCD=1
START=99

NAME="naiveproxy"

start_service() {
	config_load "$NAME"
	config_get_bool "enable" "config" "enable" "0"
	[ "$enable" -eq "1" ] || return 1

	config_get "listen_addr" "config" "listen_addr"
	config_get "server_addr" "config" "server_addr"
	config_get "extra_argument" "config" "extra_argument"

	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
	procd_set_param limits core="unlimited"
	procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

service_triggers() {
        procd_add_reload_trigger "$NAME"
}
