#!/bin/bash
# prerm script for linux-patch-api
# Created by package build system

set -e

# Stop the service before removal/upgrade
if [ "$1" = "remove" ] || [ "$1" = "upgrade" ]; then
    echo "Stopping linux-patch-api service..."
    
    if systemctl is-active --quiet linux-patch-api.service; then
        systemctl stop linux-patch-api.service
        echo "Service stopped successfully"
    else
        echo "Service was not running"
    fi
    
    # Disable the service
    if systemctl is-enabled --quiet linux-patch-api.service 2>/dev/null; then
        systemctl disable linux-patch-api.service
        echo "Service disabled"
    fi
fi

# Handle failed upgrade
if [ "$1" = "failed-upgrade" ]; then
    echo "Upgrade failed - attempting to restore previous state"
    # Previous version should handle restoration
fi

echo "Pre-removal script completed"

exit 0
