#!/usr/bin/env bash
# bin/kb/search - Go deduction search over the brain (model served by daemon).
# Builds the kbsearch binary on first run / when source changes, then execs it.
set -euo pipefail

KB="$(cd "$(dirname "$0")/../.." && pwd)"
BIN="$KB/var/bin/kbsearch"
SRC="$KB/bin/kbsearch"

mkdir -p "$KB/var/bin"

# Rebuild if binary missing or any .go source newer
need_build=0
if [ ! -x "$BIN" ]; then
    need_build=1
else
    # Check if any .go in kbsearch is newer than binary
    while IFS= read -r -d '' f; do
        if [ "$f" -nt "$BIN" ]; then
            need_build=1
            break
        fi
    done < <(find "$SRC" -name '*.go' -print0 2>/dev/null)
fi

if [ "$need_build" -eq 1 ]; then
    echo "Building kbsearch..." >&2
    (cd "$SRC" && \
        CGO_CFLAGS="-I$KB/lib-ladybug" \
        CGO_LDFLAGS="-L$KB/lib-ladybug -Wl,-rpath,$KB/lib-ladybug" \
        go build -tags system_ladybug -o "$BIN" .) || exit 1
fi

exec "$BIN" "$@"