#!/bin/sh
#
# Hides menu entries of the web UI from the default admin user (role 2 of req_auth.settings, 0 = hidden).
# RobustOS Pro 2.5 brought the entries WireGuard and Software update (the vendor update page, ours is on the Version page).
#
#   eurogard_hide_menu_entries [WAIT_SECONDS]
#
# A key that does not exist (older firmware, or the router application has not generated the role settings yet) is waited
# for up to WAIT_SECONDS (default 0: the postinst of the web files calls it without waiting, the user management service
# at boot waits for the router application). Always exits 0.

ROLE=${EG_HIDE_ROLE:-2}
KEYS=${EG_HIDE_KEYS:-"wireguard software_update rcms ssh"}
WAIT=${1:-0}

changed=0
for key in ${KEYS}; do
    name="req_auth.settings[${ROLE}].${key}"
    waited=0
    while :; do
        cur=$(uci get "${name}" 2> /dev/null)
        [ -n "${cur}" ] && break
        [ "${waited}" -ge "${WAIT}" ] && break
        sleep 5
        waited=$((waited + 5))
    done
    if [ -z "${cur}" ]; then
        echo "${name}: not there, skipped"
    elif [ "${cur}" != "0" ]; then
        uci set "${name}" 0 && changed=1 && echo "${name}: ${cur} -> 0"
    fi
done
[ "${changed}" = "1" ] && uci commit
exit 0
