#!/bin/sh /etc/rc.common
# UCI-aware procd init for srunc. Reads /etc/config/srunc, renders each enabled
# `config srunc` section to a tmpfs TOML, and spawns one procd instance per
# section. The password lives in the tmpfs file (mode 0600), NOT in argv —
# argv is world-readable via `ps`.

USE_PROCD=1
START=95
STOP=01

PROG=/usr/sbin/srunc
RUNDIR=/var/run/srunc

render_config() {
	local section="$1"
	local username password host ac_id insecure timeout profile
	config_get      username "$section" username
	config_get      password "$section" password
	config_get      host     "$section" host
	config_get      ac_id    "$section" ac_id
	config_get_bool insecure "$section" insecure 0
	config_get      timeout  "$section" timeout  2
	config_get      profile  "$section" profile

	mkdir -p "$RUNDIR"
	chmod 0700 "$RUNDIR"
	local cfg="$RUNDIR/$section.toml"
	{
		printf 'username = "%s"\n' "$username"
		printf 'password = "%s"\n' "$password"
		[ -n "$host" ]    && printf 'host = "%s"\n'    "$host"
		[ -n "$ac_id" ]   && printf 'ac_id = "%s"\n'   "$ac_id"
		[ -n "$profile" ] && printf 'profile = "%s"\n' "$profile"
		[ "$insecure" = "1" ] && echo 'insecure = true'
		printf 'timeout = %s\n' "$timeout"
		echo 'daemonize = true'
	} > "$cfg"
	chmod 0600 "$cfg"
	echo "$cfg"
}

start_instance() {
	local section="$1"
	local enabled keepalive campus_only interval retry username log_level verbose
	config_get_bool enabled     "$section" enabled     1
	config_get_bool keepalive   "$section" keepalive   1
	config_get_bool campus_only "$section" campus_only 0
	config_get      interval    "$section" interval    3
	config_get      retry       "$section" retry       2
	config_get      log_level   "$section" log_level   info
	config_get      username    "$section" username
	[ "$enabled" = "1" ] || return 0
	[ -n "$username" ] || {
		logger -t srunc "section '$section' has no username, skipping"
		return 0
	}

	# Daemon base level (with -D) is ERROR; each -v raises one step.
	case "$log_level" in
		error) verbose= ;;
		warn)  verbose=-v ;;
		info)  verbose=-vv ;;
		debug) verbose=-vvv ;;
		*)     verbose=-vv ;;
	esac

	local cfg
	cfg=$(render_config "$section")

	# Build argv. -D suppresses stdin and drops the daemon to ERROR; -v (a
	# global flag, before the subcommand) restores higher levels. procd
	# captures stdout/stderr, so logread shows whatever level is picked here.
	set -- -c "$cfg" $verbose login -D
	[ "$keepalive" = "1" ]   && set -- "$@" -k
	[ "$campus_only" = "1" ] && set -- "$@" -C
	set -- "$@" --interval "$interval" --retry "$retry"

	procd_open_instance "$section"
	procd_set_param command "$PROG" "$@"
	procd_set_param respawn 5 5 0
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

start_service() {
	local globals_enabled
	config_load 'srunc'
	config_get_bool globals_enabled globals enabled 1
	[ "$globals_enabled" = "1" ] || return 0
	config_foreach start_instance srunc
}

stop_service() {
	rm -rf "$RUNDIR"
}

service_triggers() {
	procd_add_reload_trigger "srunc"
}

reload_service() {
	stop
	start
}
