#!/bin/sh
# Cerynix — postrm
# Copyright (c) 2026 SIA Cerynix. All rights reserved.
# SPDX-License-Identifier: LicenseRef-Cerynix-Proprietary
#
# Deregister the unit. We never delete the tenant's .env, generated certs or
# Docker data volumes — purging the package must not destroy the install's
# secrets or database. Operators remove those explicitly if they mean to.
set -e

UNIT=cerynix.service

case "$1" in
  remove)
    if [ -d /run/systemd/system ]; then
      systemctl --system daemon-reload >/dev/null 2>&1 || true
    fi
    if command -v deb-systemd-helper >/dev/null 2>&1; then
      deb-systemd-helper mask "$UNIT" >/dev/null 2>&1 || true
    fi
    ;;
  purge)
    if command -v deb-systemd-helper >/dev/null 2>&1; then
      deb-systemd-helper purge "$UNIT" >/dev/null 2>&1 || true
      deb-systemd-helper unmask "$UNIT" >/dev/null 2>&1 || true
    fi
    if [ -d /run/systemd/system ]; then
      systemctl --system daemon-reload >/dev/null 2>&1 || true
    fi
    echo "cerynix: package removed. /opt/cerynix/.env and Docker data volumes were left intact."
    ;;
  upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;
esac

exit 0
