#!/usr/bin/env bash
# Build DFX_inspire_service on the G1 onboard PC (PC2 / Jetson, aarch64).
#
# Why the extra flags: the factory unitree_sdk2 installed in /usr/local is stale
# and lacks the go2 IDL headers (MotorCmds_/MotorStates_) + matching symbols.
# We point the compiler/linker at the newer unitree_sdk2 source tree instead,
# without touching /usr/local (no `sudo make install`).
#
# Override the SDK location with: UNITREE_SDK2_DIR=/path/to/unitree_sdk2 ./build.sh
set -euo pipefail

SDK="${UNITREE_SDK2_DIR:-$HOME/unitree_sdk2}"
ARCH="$(uname -m)"   # aarch64 on the G1 Jetson

if [ ! -f "${SDK}/lib/${ARCH}/libunitree_sdk2.a" ]; then
  echo "ERROR: unitree_sdk2 not found at ${SDK} (expected ${SDK}/lib/${ARCH}/libunitree_sdk2.a)" >&2
  echo "       Set UNITREE_SDK2_DIR to the unitree_sdk2 source tree." >&2
  exit 1
fi

cd "$(dirname "$0")"
mkdir -p build && cd build
cmake .. \
  -DCMAKE_CXX_FLAGS="-I${SDK}/include" \
  -DCMAKE_EXE_LINKER_FLAGS="-L${SDK}/lib/${ARCH}"
make -j"$(nproc)"

echo
echo "Built: $(pwd)/inspire_g1  inspire_h1  hand_example"
