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

ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
PROBE_DIR="$ROOT_DIR/tests/cubism-probe"
CONTROL_DIR="$ROOT_DIR/tests/gdextension-control"
OPENVT_DIR="$ROOT_DIR/vendor/open-vt"
ADDON_DIR="$PROBE_DIR/addons/gd_cubism"
LIB_DIR="$ADDON_DIR/bin/linux/x86_64"
MODEL_PATH="/home/nandi/.local/share/godot/app_userdata/open-vt/Live2DModels/Haru/Haru.model3.json"

export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export WAYLAND_DISPLAY="${WAYLAND_DISPLAY:-wayland-1}"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}"

pass() { echo "PASS: $1"; }
fail() { echo "FAIL: $1"; }

mkdir -p "$PROBE_DIR/addons"
rm -rf "$ADDON_DIR"
cp -R "$OPENVT_DIR/thirdparty/gd_cubism/demo/addons/gd_cubism" "$ADDON_DIR"
mkdir -p "$LIB_DIR"
cp "$OPENVT_DIR/addons/gd_cubism/bin/linux/x86_64/libgd_cubism.linux.debug.x86_64.so" "$LIB_DIR/"
cp "$OPENVT_DIR/addons/gd_cubism/bin/linux/x86_64/libLive2DCubismCore.so" "$LIB_DIR/"

if [ -f "$OPENVT_DIR/addons/gd_cubism/bin/linux/x86_64/libgd_cubism.linux.release.x86_64.so" ]; then
  cp "$OPENVT_DIR/addons/gd_cubism/bin/linux/x86_64/libgd_cubism.linux.release.x86_64.so" "$LIB_DIR/"
else
  cp "$OPENVT_DIR/addons/gd_cubism/bin/linux/x86_64/libgd_cubism.linux.debug.x86_64.so" "$LIB_DIR/libgd_cubism.linux.release.x86_64.so"
fi

test -f "$ADDON_DIR/gd_cubism.gdextension" && pass "gdextension file present" || fail "gdextension file missing"
test -f "$LIB_DIR/libgd_cubism.linux.debug.x86_64.so" && pass "gd_cubism binary present" || fail "gd_cubism binary missing"
test -f "$LIB_DIR/libgd_cubism.linux.release.x86_64.so" && pass "gd_cubism release binary present" || fail "gd_cubism release binary missing"
test -f "$LIB_DIR/libLive2DCubismCore.so" && pass "Cubism core binary present" || fail "Cubism core binary missing"
test -f "$MODEL_PATH" && pass "Haru model present" || fail "Haru model missing"

echo "--- Control case ---"
mkdir -p "$CONTROL_DIR/bin"
cp "$ROOT_DIR/tests/gdextension-control-src/example.gdextension" "$CONTROL_DIR/example.gdextension"
devenv shell -- bash -lc 'c++ -shared -fPIC -std=c++17 \
  -I /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/include \
  -I /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/gen/include \
  -I /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/gdextension \
  /home/nandi/code/character/tests/gdextension-control-src/example.cpp \
  /home/nandi/code/character/tests/gdextension-control-src/register_types.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/godot.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/class_db.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/error_macros.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/memory.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/method_bind.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/object.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/variant/variant.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/core/print_string.cpp \
  /home/nandi/code/character/vendor/open-vt/thirdparty/gd_cubism/godot-cpp/src/variant/variant_internal.cpp \
  -o /home/nandi/code/character/tests/gdextension-control/bin/libgdext_control.linux.template_debug.x86_64.so'
cp "$CONTROL_DIR/bin/libgdext_control.linux.template_debug.x86_64.so" "$CONTROL_DIR/bin/libgdext_control.linux.template_release.x86_64.so"

if ldd "$LIB_DIR/libgd_cubism.linux.debug.x86_64.so" | grep -q 'not found'; then
  fail "gd_cubism binary has missing shared library deps"
else
  pass "gd_cubism binary shared library deps resolved"
fi

if ldd "$LIB_DIR/libLive2DCubismCore.so" | grep -q 'not found'; then
  fail "Cubism core binary has missing shared library deps"
else
  pass "Cubism core shared library deps resolved"
fi

echo "--- Godot probe ---"
echo "Using Godot binary: ${GODOT4_BIN:-godot4}"
"${GODOT4_BIN:-godot4}" --headless --path "$PROBE_DIR" --quit-after 1 2>&1 | tee "$PROBE_DIR/probe.log"

echo "--- GDExtension control probe ---"
"${GODOT4_BIN:-godot4}" --headless --path "$CONTROL_DIR" --quit-after 1 2>&1 | tee "$CONTROL_DIR/probe.log"

echo "--- Extension file sanity ---"
grep -n . "$ADDON_DIR/gd_cubism.gdextension" | sed -n '1,120p'
