#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BUILD_DIR="${PROJECT_DIR}/build"
EDITOR_DIR="${BUILD_DIR}/editor"
APP_NAME="Blogi Editor"
APP_BUNDLE="blogi-editor.app"
DMG_NAME="blogi-editor.dmg"
STAGING_DIR="${BUILD_DIR}/dmg_staging"
QT_DIR="${QT_DIR:-$HOME/Qt/6.11.0/macos}"

# --- Validate environment ---
if [ ! -d "${QT_DIR}/bin" ]; then
    echo "Error: Qt directory not found at ${QT_DIR}"
    echo "Set QT_DIR to your Qt installation, e.g.:"
    echo "  QT_DIR=~/Qt/6.11.0/macos $0"
    exit 1
fi

MACDEPLOYQT="${QT_DIR}/bin/macdeployqt"
if [ ! -x "$MACDEPLOYQT" ]; then
    echo "Error: macdeployqt not found at ${MACDEPLOYQT}"
    exit 1
fi

# --- Configure and build ---
echo "==> Configuring..."
cmake -B "$BUILD_DIR" -S "$PROJECT_DIR" \
    -DBUILD_EDITOR=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_PREFIX_PATH="${QT_DIR}"

echo "==> Building..."
cmake --build "$BUILD_DIR" --config Release

# --- Bundle Qt frameworks and dependencies ---
echo "==> Running macdeployqt..."
"$MACDEPLOYQT" "${EDITOR_DIR}/${APP_BUNDLE}" \
    -qmldir="${PROJECT_DIR}/editor/qml" \
    2>&1 | grep -v "^ERROR:.*QtSerialPort\|^ERROR:.*libmimerapi\|^ERROR:.*libiodbc\|^ERROR:.*libpq" || true

# --- Generate .icns from SVG logo ---
ICON_SVG="${PROJECT_DIR}/data/logo_runes.svg"
ICONSET_DIR="/tmp/blogi_icon.iconset"
ICNS_FILE="/tmp/blogi_editor.icns"

if [ -f "$ICON_SVG" ]; then
    echo "==> Creating app icon..."
    rm -rf "$ICONSET_DIR"
    mkdir -p "$ICONSET_DIR"
    for size in 16 32 64 128 256 512; do
        sips -s format png -z "$size" "$size" "$ICON_SVG" \
            --out "${ICONSET_DIR}/icon_${size}x${size}.png" >/dev/null 2>&1
    done
    for size in 16 32 128 256; do
        double=$((size * 2))
        cp "${ICONSET_DIR}/icon_${double}x${double}.png" \
           "${ICONSET_DIR}/icon_${size}x${size}@2x.png" 2>/dev/null || true
    done
    iconutil -c icns "$ICONSET_DIR" -o "$ICNS_FILE"
    rm -rf "$ICONSET_DIR"
fi

# --- Prepare DMG staging folder ---
echo "==> Preparing DMG contents..."
rm -rf "$STAGING_DIR"
mkdir -p "$STAGING_DIR"
cp -R "${EDITOR_DIR}/${APP_BUNDLE}" "${STAGING_DIR}/${APP_NAME}.app"
ln -s /Applications "${STAGING_DIR}/Applications"

# Add icon to bundle
if [ -f "$ICNS_FILE" ]; then
    cp "$ICNS_FILE" "${STAGING_DIR}/${APP_NAME}.app/Contents/Resources/blogi-editor.icns"
    /usr/libexec/PlistBuddy -c "Add :CFBundleIconFile string blogi-editor" \
        "${STAGING_DIR}/${APP_NAME}.app/Contents/Info.plist" 2>/dev/null \
    || /usr/libexec/PlistBuddy -c "Set :CFBundleIconFile blogi-editor" \
        "${STAGING_DIR}/${APP_NAME}.app/Contents/Info.plist"
fi

# --- Create DMG ---
echo "==> Creating DMG..."
rm -f "${EDITOR_DIR}/${DMG_NAME}"
hdiutil create \
    -volname "${APP_NAME}" \
    -srcfolder "$STAGING_DIR" \
    -ov -format UDZO \
    "${EDITOR_DIR}/${DMG_NAME}"

rm -rf "$STAGING_DIR"

echo ""
echo "==> Done: ${EDITOR_DIR}/${DMG_NAME}"
ls -lh "${EDITOR_DIR}/${DMG_NAME}"
