#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

PROG=/usr/bin/restic-rest-server

start_instance() {
	local cfg="$1"
	local var
	local val

	config_get_bool val "$cfg" 'enabled' '0'
	[ "$val" = 0 ] && return 1

	procd_open_instance "$cfg"
	procd_set_param command "$PROG"
	for var in append_only debug no_auth no_verify_upload private_repos prometheus prometheus_no_auth tls; do
		config_get_bool val "$cfg" "$var" 0
		[ "$val" = 0 ] || procd_append_param command "--${var//_/-}"
	done
	for var in cpuprofile htpasswd_file listen log max_size path tls_cert tls_key; do
		config_get val "$cfg" "$var"
		[ -z "$val" ] || procd_append_param command "--${var//_/-}" "$val"
	done
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger restic-rest-server
}

start_service() {
	config_load restic-rest-server
	config_foreach start_instance rest-server
}
