# debian/postrm
#!/bin/sh

# Exit on error
set -e

# Service configuration
SERVICE_NAME="vsock_listener.service"
SERVICE_PATH="/etc/systemd/system/${SERVICE_NAME}"

# Function to handle systemd cleanup
cleanup_systemd() {
    if [ -d /run/systemd/system ]; then
        echo "Reloading systemd configuration"
        if ! systemctl daemon-reload >/dev/null; then
            echo "Warning: Failed to reload systemd" >&2
        fi
    fi
}

# Function to handle purge-specific cleanup
handle_purge() {
    echo "Purging ${SERVICE_NAME} configuration"
    if [ -f "${SERVICE_PATH}" ]; then
        if ! rm -f "${SERVICE_PATH}"; then
            echo "Warning: Failed to remove ${SERVICE_PATH}" >&2
        fi
    fi
    cleanup_systemd
}

# Main removal logic
case "$1" in
    purge)
        handle_purge
        ;;
    remove|upgrade|disappear|failed-upgrade|abort-install|abort-upgrade)
        cleanup_systemd
        ;;
    *)
        echo "Error: postrm called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#
exit 0

