#!/bin/bash
set -e

# Post-install script for Debian Atomic Desktop
# This script is called by Calamares after partitioning and user setup

echo "Starting Debian Atomic Desktop installation..."

# Get the target device from Calamares
TARGET_DEVICE="${1:-/dev/sda}"
ATOMIC_IMAGE="${2:-ghcr.io/particle-os/debian-atomic:latest}"

echo "Target device: $TARGET_DEVICE"
echo "Atomic image: $ATOMIC_IMAGE"

# Verify the target device exists
if [ ! -b "$TARGET_DEVICE" ]; then
    echo "Error: Target device $TARGET_DEVICE does not exist"
    exit 1
fi

# Check if bootc is available
if ! command -v bootc &> /dev/null; then
    echo "Error: bootc is not installed"
    exit 1
fi

# Install the atomic image using bootc
echo "Installing atomic image to $TARGET_DEVICE..."
bootc install to-disk \
    --device "$TARGET_DEVICE" \
    --replace-os \
    --image "$ATOMIC_IMAGE"

if [ $? -eq 0 ]; then
    echo "Atomic image installation completed successfully!"
else
    echo "Error: Failed to install atomic image"
    exit 1
fi

# Additional post-install tasks can be added here
# For example, copying user data, configuring bootloader, etc.

echo "Debian Atomic Desktop installation completed!"
exit 0 