#!/bin/sh
# prerm script for apt-ostree
#
# This script is executed before the package is removed.
# It handles pre-removal tasks such as stopping services,
# backing up configuration files, and checking dependencies.

set -e

# Package name
PACKAGE="apt-ostree"

# Configuration directories
CONFIG_DIR="/etc/apt-ostree"
DAEMON_CONFIG_DIR="/etc/apt-ostreed"
DATA_DIR="/var/lib/apt-ostree"
LOG_DIR="/var/log/apt-ostree"

case "$1" in
    remove|purge)
        echo "Removing $PACKAGE..."
        
        # Check if there are any active OSTree deployments
        if command -v ostree >/dev/null 2>&1; then
            if [ -d "/ostree" ]; then
                echo "Checking for active OSTree deployments..."
                
                # Check if there are any deployments
                if ostree admin status 2>/dev/null | grep -q "deployments"; then
                    echo "Warning: Active OSTree deployments detected"
                    echo "Removing apt-ostree may affect system stability"
                    echo "Consider switching to a different deployment before removal"
                    
                    # Ask for confirmation if interactive
                    if [ -t 0 ]; then
                        echo -n "Do you want to continue with removal? [y/N]: "
                        read -r response
                        case "$response" in
                            [yY]|[yY][eE][sS])
                                echo "Continuing with removal..."
                                ;;
                            *)
                                echo "Removal cancelled by user"
                                exit 1
                                ;;
                        esac
                    fi
                fi
            fi
        fi
        
        # Stop the apt-ostreed daemon service if running
        if command -v systemctl >/dev/null 2>&1; then
            if systemctl is-active --quiet apt-ostreed.service; then
                echo "Stopping apt-ostreed service..."
                systemctl stop apt-ostreed.service || true
            fi
        fi
        
        # Stop any running apt-ostree processes
        if pgrep -f "apt-ostree" >/dev/null 2>&1; then
            echo "Stopping running apt-ostree processes..."
            pkill -f "apt-ostree" || true
            sleep 2
            # Force kill if still running
            pkill -9 -f "apt-ostree" || true
        fi
        
        # Backup configuration files if removing (not purging)
        if [ "$1" = "remove" ]; then
            echo "Backing up configuration files..."
            if [ -d "$CONFIG_DIR" ]; then
                mkdir -p "/tmp/apt-ostree-backup-$(date +%Y%m%d-%H%M%S)"
                cp -r "$CONFIG_DIR" "/tmp/apt-ostree-backup-$(date +%Y%m%d-%H%M%S)/" || true
                echo "Configuration backed up to /tmp/apt-ostree-backup-*"
            fi
            if [ -d "$DAEMON_CONFIG_DIR" ]; then
                mkdir -p "/tmp/apt-ostree-backup-$(date +%Y%m%d-%H%M%S)"
                cp -r "$DAEMON_CONFIG_DIR" "/tmp/apt-ostree-backup-$(date +%Y%m%d-%H%M%S)/" || true
                echo "Daemon configuration backed up to /tmp/apt-ostree-backup-*"
            fi
        fi
        
        echo "$PACKAGE pre-removal completed"
        ;;
        
    upgrade)
        echo "Upgrading $PACKAGE..."
        # Nothing special needed for upgrades
        ;;
        
    failed-upgrade|abort-install|abort-upgrade|abort-remove|abort-deconfigure)
        echo "Aborting $PACKAGE operation..."
        ;;
        
    *)
        echo "prerm called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

# Exit successfully
exit 0
