#!/bin/bash
# 
# logs.sh — stream the live CoreIK invocation log
#  - Niall George Horn; June 2026

# Info: 
# The running app writes _CoreIKSolverFusion* call entries to this file.
# The simulator's /tmp maps to the host's /private/tmp — no container
# boundary, so this works directly on the host without xcrun or simctl.
#
# Usage:
#   ./deploy/logs.sh
#
# The app must be running and Immersive Space entered before the log appears.
#

LOG="/tmp/coreik_invocations.log"

# Clean exit on Ctrl+C. Set early so it also breaks the wait-loop below — a bare
# `while ...; do sleep; done` doesn't reliably exit on SIGINT, which is why Ctrl+C
# felt dead before.
trap 'echo; echo "  Stopped streaming."; exit 0' INT TERM

if [ ! -f "$LOG" ]; then
    echo "Log not found: $LOG"
    echo "Waiting for app to write it (enter Immersive Space if you haven't)..."
    while [ ! -f "$LOG" ]; do sleep 1; done
    echo "Log appeared. Streaming..."
fi

echo "==> Streaming: $LOG  (Ctrl+C to stop)"
# exec so tail becomes the foreground process and receives Ctrl+C directly.
exec tail -f "$LOG"
