#!/bin/sh

default_base_dir() {
	local main_dir conf_dir

	main_dir="$(uci -q get quickstart.main.main_dir 2>/dev/null || true)"
	conf_dir="$(uci -q get quickstart.main.conf_dir 2>/dev/null || true)"

	[ -n "$main_dir" ] || main_dir="/root"
	[ -n "$conf_dir" ] || conf_dir="${main_dir%/}/Configs"

	printf '%s/OpenClawMgr\n' "${conf_dir%/}"
}

# Ensure section exists
if ! uci -q get openclawmgr.main >/dev/null 2>&1; then
	uci -q set openclawmgr.main=openclawmgr >/dev/null 2>&1 || true
fi

# Fill defaults (only if missing)
[ -n "$(uci -q get openclawmgr.main.enabled 2>/dev/null)" ] || uci -q set openclawmgr.main.enabled='0'
[ -n "$(uci -q get openclawmgr.main.base_dir 2>/dev/null)" ] || uci -q set openclawmgr.main.base_dir="$(default_base_dir)"
[ -n "$(uci -q get openclawmgr.main.install_accelerated 2>/dev/null)" ] || uci -q set openclawmgr.main.install_accelerated='1'

uci -q commit openclawmgr >/dev/null 2>&1 || true

# best-effort: create an unprivileged user for procd
if ! id openclawmgr >/dev/null 2>&1; then
	if command -v adduser >/dev/null 2>&1; then
		adduser -D -H -s /bin/false openclawmgr >/dev/null 2>&1 || true
	elif command -v useradd >/dev/null 2>&1; then
		useradd -r -M -s /bin/false openclawmgr >/dev/null 2>&1 || true
	fi
fi

exit 0
