set shell := ["bash", "-cu"]
set dotenv-load := true

sanderling := env_var_or_default("SANDERLING", "sanderling")
avd        := env_var_or_default("AVD", "")
duration   := env_var_or_default("DURATION", "1m")
seed       := env_var_or_default("SEED", "0")
output     := env_var_or_default("OUTPUT", justfile_directory() / "sanderling" / "runs")
ios_device := env_var_or_default("IOS_DEVICE", "iPhone 17 Pro")

default:
    @just --list

# Resolve ANDROID_HOME from the env or a short list of canonical install
# locations, so Gradle can find the Android SDK without the user having to
# export anything.
_android-home:
    #!/usr/bin/env bash
    set -euo pipefail
    if [[ -n "${ANDROID_HOME:-}" && -d "$ANDROID_HOME" ]]; then
      echo "$ANDROID_HOME"; exit 0
    fi
    if [[ -n "${ANDROID_SDK_ROOT:-}" && -d "$ANDROID_SDK_ROOT" ]]; then
      echo "$ANDROID_SDK_ROOT"; exit 0
    fi
    for candidate in \
      "$HOME/Library/Android/sdk" \
      "$HOME/Android/Sdk" \
      "/opt/homebrew/share/android-commandlinetools" \
      "/usr/local/share/android-commandlinetools"; do
      if [[ -d "$candidate" ]]; then
        echo "$candidate"; exit 0
      fi
    done
    echo "could not locate Android SDK (set ANDROID_HOME)" >&2
    exit 1

# Build and install the folio APK on a running emulator/device.
install:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    ./gradlew :app:androidApp:installDebug

# Remove the folio APK from the connected device.
uninstall:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    ./gradlew :app:androidApp:uninstallDebug

# Remove gradle + iOS build directories.
clean:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    ./gradlew clean
    rm -rf app/iosApp/build

# Regenerate iosApp.xcodeproj from project.yml.
ios-gen:
    #!/usr/bin/env bash
    set -euo pipefail
    cd app/iosApp && xcodegen generate

# Build + install + launch on a booted iOS simulator (boots IOS_DEVICE if none).
ios:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    if ! xcrun simctl list devices booted | grep -q Booted; then
      xcrun simctl boot "{{ios_device}}"
      open -a Simulator
      sleep 3
    fi
    just ios-gen
    xcodebuild -project app/iosApp/iosApp.xcodeproj -scheme iosApp \
      -destination 'platform=iOS Simulator,name={{ios_device}}' \
      -derivedDataPath app/iosApp/build \
      build | tail -5
    xcrun simctl install booted app/iosApp/build/Build/Products/Debug-iphonesimulator/iosApp.app
    xcrun simctl launch booted app.folio

# Run 'sanderling test' against the folio app. Uses a connected device if one is
# online, otherwise boots AVD=<name> if provided.
test:
    #!/usr/bin/env bash
    set -euo pipefail
    avd_flag=()
    if [[ -n "{{avd}}" ]]; then
      avd_flag=(--avd "{{avd}}")
    fi
    "{{sanderling}}" test \
      --spec "{{justfile_directory()}}/sanderling/spec.ts" \
      --bundle-id app.folio \
      "${avd_flag[@]}" \
      --duration "{{duration}}" \
      --seed "{{seed}}" \
      --output "{{output}}"

# Serve the wasmJs web app from a webpack dev server with COOP/COEP headers.
web:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    ./gradlew :app:webApp:wasmJsBrowserDevelopmentRun --continuous

# Produce a webpack distributable bundle for the web app.
web-build:
    #!/usr/bin/env bash
    set -euo pipefail
    export ANDROID_HOME="$(just _android-home)"
    ./gradlew :app:webApp:wasmJsBrowserDevelopmentExecutableDistribution

# Build + install + run sanderling spec on iOS simulator.
test-ios:
    #!/usr/bin/env bash
    set -euo pipefail
    just ios
    ios_device_flag=()
    if [[ -n "{{ios_device}}" ]]; then
      ios_device_flag=(--ios-device "{{ios_device}}")
    fi
    "{{sanderling}}" test \
      --platform ios \
      --spec "{{justfile_directory()}}/sanderling/spec.ts" \
      --bundle-id app.folio \
      "${ios_device_flag[@]}" \
      --duration "{{duration}}" \
      --seed "{{seed}}" \
      --output "{{output}}"
