#!/bin/sh
# pve-nag-patch: stop the Proxmox web UI's "No valid subscription" popup (Prime, 2026-10-03: every host).
#
# The popup lives in proxmox-widget-toolkit's proxmoxlib.js, in Proxmox.Utils.checked_command: when the
# subscription status is not 'active' it shows Ext.Msg.show(...) and runs the guarded command only after OK.
# This replaces that one status test with `false`, so the else-branch runs the command directly. The popup
# is skipped, and buttons guarded by checked_command (login, apt Refresh, ...) still work. The common
# "void(Ext.Msg.show" trick swallows the command instead. The second status test in the file (the
# subscription panel's state) is deliberately left alone.
#
# Idempotent; safe to run any time. The apt hook /etc/apt/apt.conf.d/86pve-nag-patch re-runs it after every
# dpkg run, because each proxmox-widget-toolkit update restores the original file.
# Undo: apt-get install --reinstall proxmox-widget-toolkit, after removing the hook.
# Always exits 0 so it can never fail an apt transaction.
[ -f /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js ] || exit 0
grep -q 'pve-nag-patch' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js && exit 0
# Already de-nagged by another tool (pve-nag-buster / the community `res.false` or NoMoreNagging seds): leave it.
grep -q -E "res\.false|NoMoreNagging|void\(\{ //Ext\.Msg\.show" /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js && exit 0
if ! grep -q 'No valid subscription' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js; then
  logger -t pve-nag-patch "popup not found: proxmoxlib.js layout changed, NOT patched"
  exit 0
fi
# Anchored on the popup itself: the status test must be IMMEDIATELY followed by `) { Ext.Msg.show({ title:
# gettext('No valid subscription')`, so nothing else in the file can match. Older toolkits (4.3.6/4.3.11) wrap
# the test across lines (`res\n .data.status...`), hence the \s*. Found in the 2026-10-03 rollout's offline test,
# where an unanchored first version would have patched the subscription PANEL on nh3-pve and pfi-pve.
perl -0pi -e 's/res\s*\.data\.status\.toLowerCase\(\)\s*!==\s*.active.(\s*\)\s*\{\s*Ext\.Msg\.show\(\{\s*title:\s*gettext\(.No valid subscription.\))/false \/* pve-nag-patch *\/$1/' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js
if grep -q 'pve-nag-patch' /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js; then
  logger -t pve-nag-patch "patched proxmoxlib.js"
else
  logger -t pve-nag-patch "status test before the popup not found: NOT patched"
fi
exit 0
