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

# Build Driftwood as an AppImage using linuxdeploy
#
# Prerequisites:
#   - Rust toolchain (cargo)
#   - linuxdeploy (https://github.com/linuxdeploy/linuxdeploy)
#   - linuxdeploy-plugin-gtk (for GTK4/libadwaita bundling)
#
# Usage:
#   ./packaging/build-appimage.sh
#
# The resulting .AppImage will be in the project root.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
APP_ID="app.driftwood.Driftwood"

cd "$PROJECT_DIR"

echo "=== Building Driftwood (release) ==="
cargo build --release

echo "=== Preparing AppDir ==="
APPDIR="$PROJECT_DIR/AppDir"
rm -rf "$APPDIR"
mkdir -p "$APPDIR/usr/bin"
mkdir -p "$APPDIR/usr/share/applications"
mkdir -p "$APPDIR/usr/share/glib-2.0/schemas"
mkdir -p "$APPDIR/usr/share/icons/hicolor/scalable/apps"
mkdir -p "$APPDIR/usr/share/icons/hicolor/symbolic/apps"
mkdir -p "$APPDIR/usr/share/metainfo"

# Binary
cp "target/release/driftwood" "$APPDIR/usr/bin/driftwood"

# Desktop file
cp "data/$APP_ID.desktop" "$APPDIR/usr/share/applications/$APP_ID.desktop"

# GSettings schema
cp "data/$APP_ID.gschema.xml" "$APPDIR/usr/share/glib-2.0/schemas/$APP_ID.gschema.xml"
glib-compile-schemas "$APPDIR/usr/share/glib-2.0/schemas/"

# Icon
ICON_FILE="data/icons/hicolor/scalable/apps/$APP_ID.svg"
if [ -f "$ICON_FILE" ]; then
    cp "$ICON_FILE" "$APPDIR/usr/share/icons/hicolor/scalable/apps/$APP_ID.svg"
else
    echo "Warning: No app icon found at $ICON_FILE"
    echo "Creating a minimal placeholder icon..."
    cat > "$APPDIR/usr/share/icons/hicolor/scalable/apps/$APP_ID.svg" << 'SVGEOF'
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128">
  <rect width="128" height="128" rx="16" fill="#3584e4"/>
  <text x="64" y="78" font-family="sans-serif" font-size="64" font-weight="bold"
        fill="white" text-anchor="middle">D</text>
</svg>
SVGEOF
fi

# Symbolic icon
SYMBOLIC_FILE="data/icons/hicolor/symbolic/apps/$APP_ID-symbolic.svg"
if [ -f "$SYMBOLIC_FILE" ]; then
    cp "$SYMBOLIC_FILE" "$APPDIR/usr/share/icons/hicolor/symbolic/apps/$APP_ID-symbolic.svg"
fi

# AppStream metainfo
METAINFO_FILE="data/$APP_ID.metainfo.xml"
if [ -f "$METAINFO_FILE" ]; then
    cp "$METAINFO_FILE" "$APPDIR/usr/share/metainfo/$APP_ID.metainfo.xml"
fi

# Check for linuxdeploy
LINUXDEPLOY="${LINUXDEPLOY:-linuxdeploy}"
if ! command -v "$LINUXDEPLOY" &>/dev/null; then
    # Try to find it in the current directory
    if [ -x "./linuxdeploy-x86_64.AppImage" ]; then
        LINUXDEPLOY="./linuxdeploy-x86_64.AppImage"
    else
        echo ""
        echo "Error: linuxdeploy not found."
        echo ""
        echo "Download it from:"
        echo "  https://github.com/linuxdeploy/linuxdeploy/releases"
        echo ""
        echo "Then either:"
        echo "  1. Place linuxdeploy-x86_64.AppImage in the project root, or"
        echo "  2. Set LINUXDEPLOY=/path/to/linuxdeploy"
        exit 1
    fi
fi

# Check for GTK plugin
GTK_PLUGIN_ARG=""
if [ -x "./linuxdeploy-plugin-gtk.sh" ]; then
    export DEPLOY_GTK_VERSION=4
    GTK_PLUGIN_ARG="--plugin gtk"
    echo "GTK4 plugin found - libraries will be bundled."
else
    echo ""
    echo "Warning: linuxdeploy-plugin-gtk not found."
    echo "GTK4 libraries will not be bundled."
    echo "The AppImage may only work on systems with GTK4 and libadwaita installed."
    echo ""
    echo "Download the plugin from:"
    echo "  https://github.com/linuxdeploy/linuxdeploy-plugin-gtk"
    echo ""
fi

echo "=== Building AppImage ==="
export ARCH="x86_64"
export OUTPUT="Driftwood-x86_64.AppImage"
export GSETTINGS_SCHEMA_DIR="$APPDIR/usr/share/glib-2.0/schemas"

"$LINUXDEPLOY" \
    --appdir "$APPDIR" \
    --desktop-file "$APPDIR/usr/share/applications/$APP_ID.desktop" \
    --icon-file "$APPDIR/usr/share/icons/hicolor/scalable/apps/$APP_ID.svg" \
    $GTK_PLUGIN_ARG \
    --output appimage

echo ""
echo "=== Done ==="
echo "AppImage created: $OUTPUT"
echo "You can run it with: ./$OUTPUT"
