#!/bin/bash
# preremove script for archivebox .deb package
set -e

# Only clean up on full removal, not during upgrade.
# dpkg passes "$1" as "remove", "purge", or "upgrade" — we skip cleanup on
# upgrade so the venv and service persist across package version bumps.
if [ "$1" = "remove" ] || [ "$1" = "purge" ]; then
    # Stop the service if running
    if command -v systemctl >/dev/null 2>&1 && [ -d /run/systemd/system ]; then
        systemctl stop archivebox 2>/dev/null || true
        systemctl disable archivebox 2>/dev/null || true
    fi

    echo "[+] Removing ArchiveBox virtualenv..."
    rm -rf /opt/archivebox/venv

    echo "[i] Your ArchiveBox data in /var/lib/archivebox has NOT been removed."
    echo "    The 'archivebox' system user has NOT been removed."
    echo "    Remove them manually if you no longer need them:"
    echo "      sudo rm -rf /var/lib/archivebox"
    echo "      sudo userdel archivebox"
fi
