#!/bin/bash
# Common setup for Android dev scripts
# Source this file, don't execute it directly

set -eou pipefail

# Resolve repo root. Prefer git, fall back to jj for non-colocated jj
# workspaces (e.g. workspaces created via `jj workspace add`).
if ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"; then
    :
elif ROOT="$(jj root 2>/dev/null)"; then
    :
else
    echo "error: not in a git or jj working tree" >&2
    exit 1
fi
SDK_ROOT="${ROOT}/sdks/android"

# Re-exec in the android Nix shell if not already there.
# Also accepts the "local" (default) shell which has all necessary tools.
ensure_nix_shell() {
    if [[ "${XMTP_DEV_SHELL:-}" != "android" && "${XMTP_DEV_SHELL:-}" != "local" ]]; then
        exec nix develop "${ROOT}#android" --command "$0" "$@"
    fi
}
