#!/bin/sh

# Exit on error
set -e

# Service configuration
SERVICE_NAME="guest-agent.service"

# Function to handle systemd service cleanup
cleanup_systemd_service() {
    if [ -d /run/systemd/system ]; then
        echo "Stopping and cleaning up ${SERVICE_NAME}"
        
        # Stop the service
        if ! systemctl stop "${SERVICE_NAME}" >/dev/null; then
            echo "Warning: Failed to stop ${SERVICE_NAME}" >&2
        fi
        
        # Disable the service
        if ! systemctl disable "${SERVICE_NAME}" >/dev/null; then
            echo "Warning: Failed to disable ${SERVICE_NAME}" >&2
        fi
        
        # Reload systemd to apply changes
        if ! systemctl daemon-reload >/dev/null; then
            echo "Warning: Failed to reload systemd" >&2
        fi
    else
        echo "Warning: Systemd not detected, skipping service cleanup" >&2
    fi
}

# Main removal logic
case "$1" in
    remove)
        cleanup_systemd_service
        ;;
    upgrade|deconfigure|failed-upgrade)
        # No action needed for these cases
        ;;
    *)
        echo "Error: prerm called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#
exit 0