#!/usr/bin/env bash
# Cross-build Chatter with the reMarkable Paper Pro Move (Chiappa) SDK.
#
# Usage: scripts/build.sh [extra cmake args]
# Override SDK location with CHATTER_SDK=...
set -euo pipefail

SDK="${CHATTER_SDK:-/home/ack/external/remarkable-sdk/chiappa-3.27.0.97}"
ENV="$SDK/environment-setup-cortexa55-remarkable-linux"
[ -f "$ENV" ] || { echo "SDK env-setup not found: $ENV"; exit 1; }

cd "$(dirname "$0")/.."

# Yocto SDKs refuse to operate with LD_LIBRARY_PATH set (Jatke sets it globally).
unset LD_LIBRARY_PATH
# shellcheck disable=SC1090
. "$ENV"

cmake -S . -B build "$@"
cmake --build build -j"$(nproc)"
echo "built: build/chatter"

# The return-to-Chatter launcher is a tiny standalone C daemon (not part of the
# CMake target). Build it here too so install-launcher.sh has its binary.
# $CC carries flags (mcpu, sysroot, …) so it must stay UNquoted.
# shellcheck disable=SC2086
$CC -O2 -o tools/chatter-launcher tools/chatter_launcher.c
echo "built: tools/chatter-launcher"
