#!/bin/sh
# Satisfy LuCI menu "depends.uci { <confname>: true }" for apps whose package
# ships an incomplete config.
#
# LuCI hides a menu whose referenced uci config exists but has NO section
# (see luci.dispatcher check_uci_depends: it requires uci.foreach to find at
# least one section). Some packages (e.g. luci-app-ddns-go) declare the
# dependency but never create the config, so the route is never registered and
# the operator sees "No page is registered at '/admin/services/<app>'".
#
# We add a minimal placeholder section so the app becomes reachable; the app
# writes its own real config on first save. Only genuine config names are
# touched -- "@section" forms are a different dependency syntax and are skipped.
changed=0
for f in /usr/share/luci/menu.d/*.json; do
	[ -f "$f" ] || continue
	names=$(tr -d ' \t\n' < "$f" | grep -o '"uci":{[^}]*}' | grep -o '"[^"]*":true' | sed 's/"//g; s/:true//')
	[ -n "$names" ] || continue
	for n in $names; do
		case "$n" in
			''|@*|*[!A-Za-z0-9_.-]*) continue ;;
		esac
		if [ -e "/etc/config/$n" ] && [ -n "$(uci -q show "$n" 2>/dev/null)" ]; then
			continue
		fi
		printf 'config compat\n' > "/etc/config/$n"
		changed=1
	done
done
exit 0
