#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
NOSTR_CORE_DIR="$REPO_ROOT/includes/nostr_core_lib"
NOSTR_CORE_LIB="$NOSTR_CORE_DIR/libnostr_core_x64.a"
BUILD_DIR="$SCRIPT_DIR/build-host"
BINARY="$BUILD_DIR/nostr-id-tui"

if [[ ! -f "$NOSTR_CORE_LIB" ]]; then
    echo "[dev.sh] missing $NOSTR_CORE_LIB; building nostr_core_lib first"
    (
        cd "$NOSTR_CORE_DIR"
        ./build.sh x64
    )
fi

echo "[dev.sh] configuring host build"
cmake -S "$SCRIPT_DIR" -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE=Debug

echo "[dev.sh] building host binary"
cmake --build "$BUILD_DIR" -j"$(nproc)"

echo "[dev.sh] launching TUI"
exec env TERM="${TERM:-xterm-256color}" "$BINARY"
