#!/bin/bash
set -euo pipefail
export PATH=/root/.cargo/bin:$PATH

cargo build --release

apt-get install -y -qq patchelf 2>/dev/null || true
for b in target/release/{bms_test,rpc_test_server,rpc_test_client}; do
    [ -x "$b" ] || continue
    patchelf --replace-needed libddsc.so.0   libddsc.so   "$b"
    patchelf --replace-needed libddscxx.so.0 libddscxx.so "$b"
    patchelf --set-rpath '$ORIGIN/../lib' "$b"
done

rm -rf install
mkdir -p install/bin install/lib
for b in bms_test rpc_test_server rpc_test_client; do
    [ -x "target/release/$b" ] && cp "target/release/$b" install/bin/
done

SDK=../unitree_sdk2/thirdparty/lib/$(uname -m)
cp "$SDK"/libddsc.so "$SDK"/libddscxx.so install/lib/
