set export

# The display/package name used by scripts (override with: just package ios PACKAGE_NAME=mylib)
PACKAGE_NAME := "mylib"

# The location where the completed Android library will be placed.
ANDROID_OUT := justfile_directory() + "/bindings/android"

# The location where the completed iOS bindings will be placed.
IOS_OUT := justfile_directory() + "/bindings/ios"

# The Rust project directory (defaults to current directory - set this when using the tool in another project)
RUST_PROJECT := "."

# Android API level
ANDROID_API_LEVEL := "21"

# Lists the available recipes, omitting itself from the list.
default:
  @just --color always --justfile {{justfile()}} --list | grep -E -v "^\s+ default "

# Validate config file
validate:
  @just _ensure-cli-built
  ./target/release/kuniffi-build validate

# Show current config
show-config:
  @just _ensure-cli-built
  ./target/release/kuniffi-build config

# Removes intermediate build files and binaries
clean:
  cargo clean

# Build the CLI tool
build-cli:
  cargo build --release
  @echo "✓ CLI built: target/release/kuniffi-build"

# Install the CLI tool locally
install-cli:
  cargo install --path . --force
  @echo "✓ CLI installed: kuniffi-build"

# Check if CLI is built, build if not
_ensure-cli-built:
  #!/usr/bin/env bash
  if [ ! -f "target/release/kuniffi-build" ]; then
    echo "Building CLI tool..."
    cargo build --release
  fi

# Initialize config file
init package_name="":
  @just _ensure-cli-built
  #!/usr/bin/env bash
  if [ -n "{{package_name}}" ]; then
    ./target/release/kuniffi-build init --package-name "{{package_name}}"
  else
    ./target/release/kuniffi-build init
  fi

# Packages the library using CLI (recommended)
[no-exit-message]
package platform profile="release":
  @just _ensure-cli-built
  #!/usr/bin/env bash
  case '{{platform}}' in
    And | Android | Droid | Google | and | android | droid | google)
      ./target/release/kuniffi-build {{profile}} --platform android
      ;;
    Apple | IOS | apple | iOS | ios)
      ./target/release/kuniffi-build {{profile}} --platform ios
      ;;
    both | Both | BOTH | all | All | ALL)
      ./target/release/kuniffi-build {{profile}} --platform both
      ;;
    *)
      printf "{{RED}}error:{{NORMAL}} "
      printf "{{BOLD}}platform \"{{platform}}\" not in list of supported platforms: "
      printf "android, ios, both\n{{NORMAL}}"
      exit 1
      ;;
  esac

# Build debug version
debug platform="both":
  @just _ensure-cli-built
  ./target/release/kuniffi-build debug --platform {{platform}}

# Build release version (optimized) - replaces old release recipe
release platform="both":
  @just _ensure-cli-built
  ./target/release/kuniffi-build release --platform {{platform}}

# Packages the library and moves it into place (legacy - uses scripts directly)
[no-exit-message]
package-legacy platform:
  #!/usr/bin/env bash

  case '{{platform}}' in
    And | Android | Droid | Google | and | android | droid | google)
      ./scripts/package_android.sh
      ;;
    Apple | IOS | apple | iOS | ios)
      ./scripts/package_ios.sh
      ;;
    both | Both | BOTH | all | All | ALL)
      ./scripts/package_ios.sh
      ./scripts/package_android.sh
      ;;
    *)
      printf "{{RED}}error:{{NORMAL}} "
      printf "{{BOLD}}platform \"{{platform}}\" not in list of supported platforms: "
      printf "android, ios, both\n{{NORMAL}}"
      exit 1
      ;;
  esac

# Lints the project using Cargo's Clippy tool
lint:
  cargo clippy

# Run the project's tests
test:
  cargo test

# Setup uniffi-bindgen binary in the Rust project
setup RUST_PROJECT:
  #!/usr/bin/env bash
  cd {{RUST_PROJECT}}
  
  # Create uniffi-bindgen.rs if it doesn't exist
  if [ ! -f "uniffi-bindgen.rs" ]; then
    echo 'fn main() {' > uniffi-bindgen.rs
    echo '    uniffi::uniffi_bindgen_main();' >> uniffi-bindgen.rs
    echo '}' >> uniffi-bindgen.rs
    echo "✓ Created uniffi-bindgen.rs"
  else
    echo "uniffi-bindgen.rs already exists"
  fi
  
  # Check if Cargo.toml needs updates
  if ! grep -q "uniffi-bindgen" Cargo.toml; then
    echo "⚠️  Please add this to your Cargo.toml:"
    echo ""
    echo "[[bin]]"
    echo "name = \"uniffi-bindgen\""
    echo "path = \"uniffi-bindgen.rs\""
    echo "required-features = [\"bindgen-cli\"]"
    echo ""
    echo "[features]"
    echo "bindgen-cli = [\"uniffi/cli\"]"
    echo ""
    echo "[dependencies]"
    echo "uniffi = { version = \"0.30.0\" }"
    echo ""
    echo "[build-dependencies]"
    echo "uniffi = { version = \"0.30.0\", features = [\"build\"] }"
  else
    echo "✓ Cargo.toml already configured"
  fi
