#!/bin/sh

# Exit on error
set -e

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

# Function to handle systemd service management
manage_systemd_service() {
    # Check if systemd is running
    if [ -d /run/systemd/system ]; then
        echo "Configuring systemd service: ${SERVICE_NAME}"
        
        # Reload systemd to pick up new service
        if ! systemctl --system daemon-reload >/dev/null; then
            echo "Warning: Failed to reload systemd" >&2
        fi
        
        # Enable service
        if ! systemctl enable "${SERVICE_NAME}" >/dev/null; then
            echo "Warning: Failed to enable ${SERVICE_NAME}" >&2
        fi
        
        # Start service
        if ! systemctl start "${SERVICE_NAME}" >/dev/null; then
            echo "Warning: Failed to start ${SERVICE_NAME}" >&2
        fi
    else
        echo "Warning: Systemd not detected, skipping service configuration" >&2
    fi
}

# Main installation logic
case "$1" in
    configure)
        manage_systemd_service
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        # No action needed for these cases
        ;;
    *)
        echo "Error: postinst called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#
exit 0