#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
IMAGE_NAME="zeroclock-appimage-builder"
OUTPUT_DIR="$SCRIPT_DIR/dist-appimage"

echo "Building Docker image (Ubuntu 24.04 / glibc 2.39)..."
docker build -f Dockerfile.appimage -t "$IMAGE_NAME" "$SCRIPT_DIR"

echo "Extracting AppImage..."
rm -rf "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"

CONTAINER_ID=$(docker create "$IMAGE_NAME")
docker cp "$CONTAINER_ID:/app/src-tauri/target/release/bundle/appimage/." "$OUTPUT_DIR/"
docker rm "$CONTAINER_ID" > /dev/null

echo ""
echo "Done! AppImage(s) in: $OUTPUT_DIR/"
ls -lh "$OUTPUT_DIR/"*.AppImage 2>/dev/null || echo "(no .AppImage files found  - check build output above)"
