#!/bin/bash
set -euo pipefail

# Build Pixstrip AppImage
# Requires: cargo, linuxdeploy (with gtk plugin), appimagetool
# Usage: ./build-appimage.sh [--release]

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_TYPE="release"
CARGO_FLAGS="--release"

if [[ "${1:-}" == "--debug" ]]; then
    BUILD_TYPE="debug"
    CARGO_FLAGS=""
fi

APP_ID="live.lashman.Pixstrip"
APP_DIR="$SCRIPT_DIR/AppDir"
TOOLS_DIR="$SCRIPT_DIR/build-tools"

echo "=== Building Pixstrip AppImage ($BUILD_TYPE) ==="

# Step 1: Build binaries
echo "--- Building binaries ---"
cargo build $CARGO_FLAGS --workspace

# Step 2: Create AppDir structure
echo "--- Creating AppDir ---"
rm -rf "$APP_DIR"
mkdir -p "$APP_DIR/usr/bin"
mkdir -p "$APP_DIR/usr/share/applications"
mkdir -p "$APP_DIR/usr/share/icons/hicolor/scalable/apps"
mkdir -p "$APP_DIR/usr/share/metainfo"

# Step 3: Copy binaries
cp "target/$BUILD_TYPE/pixstrip-gtk" "$APP_DIR/usr/bin/pixstrip-gtk"
cp "target/$BUILD_TYPE/pixstrip-cli" "$APP_DIR/usr/bin/pixstrip"

# Step 4: Copy desktop file, icons, metainfo
cp "data/$APP_ID.desktop" "$APP_DIR/usr/share/applications/"
cp "data/$APP_ID.desktop" "$APP_DIR/"
cp "data/$APP_ID.metainfo.xml" "$APP_DIR/usr/share/metainfo/"

# Copy all hicolor icon sizes
for size_dir in data/icons/hicolor/*/apps; do
    size=$(basename "$(dirname "$size_dir")")
    mkdir -p "$APP_DIR/usr/share/icons/hicolor/$size/apps"
    cp "$size_dir"/$APP_ID.* "$APP_DIR/usr/share/icons/hicolor/$size/apps/" 2>/dev/null || true
done

# Top-level icon for AppImage spec
cp "data/icons/hicolor/256x256/apps/$APP_ID.png" "$APP_DIR/$APP_ID.png"

# Step 5: Create AppRun
cat > "$APP_DIR/AppRun" << 'APPRUN'
#!/bin/bash
SELF="$(readlink -f "$0")"
HERE="${SELF%/*}"
export PATH="${HERE}/usr/bin:${PATH}"
export LD_LIBRARY_PATH="${HERE}/usr/lib:${HERE}/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH:-}"
export XDG_DATA_DIRS="${HERE}/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
export GI_TYPELIB_PATH="${HERE}/usr/lib/x86_64-linux-gnu/girepository-1.0:${GI_TYPELIB_PATH:-}"
exec "${HERE}/usr/bin/pixstrip-gtk" "$@"
APPRUN
chmod +x "$APP_DIR/AppRun"

# Step 6: Bundle shared libraries
echo "--- Bundling libraries ---"
mkdir -p "$TOOLS_DIR"

# Download linuxdeploy if not present
if [[ ! -x "$TOOLS_DIR/linuxdeploy" ]]; then
    echo "Downloading linuxdeploy..."
    wget -q -O "$TOOLS_DIR/linuxdeploy" \
        "https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-x86_64.AppImage"
    chmod +x "$TOOLS_DIR/linuxdeploy"
fi

# Download linuxdeploy GTK plugin if not present
if [[ ! -x "$TOOLS_DIR/linuxdeploy-plugin-gtk.sh" ]]; then
    echo "Downloading linuxdeploy GTK plugin..."
    wget -q -O "$TOOLS_DIR/linuxdeploy-plugin-gtk.sh" \
        "https://raw.githubusercontent.com/linuxdeploy/linuxdeploy-plugin-gtk/master/linuxdeploy-plugin-gtk.sh"
    chmod +x "$TOOLS_DIR/linuxdeploy-plugin-gtk.sh"
fi

# Use linuxdeploy to bundle dependencies and create AppImage
export DEPLOY_GTK_VERSION=4
"$TOOLS_DIR/linuxdeploy" \
    --appdir "$APP_DIR" \
    --desktop-file "$APP_DIR/usr/share/applications/$APP_ID.desktop" \
    --icon-file "$APP_DIR/usr/share/icons/hicolor/256x256/apps/$APP_ID.png" \
    --plugin gtk \
    --output appimage

echo ""
echo "=== AppImage built ==="
ls -lh Pixstrip-*.AppImage 2>/dev/null || echo "Note: output filename may vary."
