#!/bin/sh
set -- remove
# initMAX post-remove cleanup for navigatormax. The postinstall deploys a
# REAL module dir to the active frontend's modules/ (not a symlink), so on a
# genuine uninstall remove that deployed dir. Only OUR module (guard on
# manifest id), only the deployed copy - never the package-owned staging
# /usr/share/initmax/zabbix-modules/<id> (the package manager owns it).
case "$1" in
  remove|purge|0|"") ;;   # real uninstall
  *) exit 0 ;;            # upgrade etc - keep it
esac
# RPM has already removed the unit files at %postun. disable --now
# can fail to load that missing file before stopping the cached unit.
# Stop the loaded units explicitly, then remove enablement and reload.
if command -v systemctl >/dev/null 2>&1; then
  systemctl stop "initmax-widget-deploy-navigatormax.path" "initmax-widget-deploy-navigatormax.service" >/dev/null 2>&1 || true
  systemctl disable "initmax-widget-deploy-navigatormax.path" >/dev/null 2>&1 || true
  systemctl daemon-reload >/dev/null 2>&1 || true
fi
for r in /usr/share/zabbix/ui /usr/share/zabbix /usr/share/zabbix/php ${ZABBIX_FRONTEND_DIR:-}; do
  M="$r/modules"
  [ -L "$M/navigatormax" ] && rm -f "$M/navigatormax"
  [ -d "$M/navigatormax" ] && grep -q '"id"' "$M/navigatormax/manifest.json" 2>/dev/null && rm -rf "$M/navigatormax"
done
exit 0
