#!/bin/bash

webuiContainerSetup()
{
    local app_name=$1
    local type=$2 # install or uninstall

    if [[ "$app_name" == "libreportal" && "$type" == "uninstall" ]]; then
        isNotice "Skipping WebUI config refresh — LibrePortal (the WebUI) is being removed."
        return 0
    fi

    webuiUpdateAppStatus $app_name $type;
    webuiUpdateAppLog $app_name $type;
    webuiGenerateAppsServicesConfig;
    # Targeted patch (~0.5s) instead of full regen (~3.5s) for the
    # single app being installed/uninstalled.
    if declare -F webuiPatchAppConfigJson >/dev/null 2>&1; then
        webuiPatchAppConfigJson "$app_name" >/dev/null 2>&1 \
            || webuiGenerateLibrePortalConfig
    else
        webuiGenerateLibrePortalConfig
    fi
    # The patch path rewrites apps.json (which references
    # /icons/apps/<app>.svg) but doesn't place the icon file — sync it here
    # so the apps page never points at a missing icon. The full regen
    # copies icons itself; this is cheap and idempotent regardless.
    if declare -F webuiSyncAppIcon >/dev/null 2>&1; then
        webuiSyncAppIcon "$app_name" >/dev/null 2>&1 || true
    fi
}