#!/bin/sh
# retroDE_ps2 — copy the Ch357 (native 640x480) board-side files to the DE25.
#
# One scp invocation => a single password prompt. Copies to the home dir so the host runs from ~ and the descriptor
# table (sh3_s640_epochs.txt) finds the tex/list files by their relative names.
#
#   ./tools/scp_ch357_to_board.sh                 # default terasic@192.168.50.161:~
#   ./tools/scp_ch357_to_board.sh user@host:dir   # override destination
#
# On the board afterward:
#   gcc -O2 -o ps2_sh3_sched ps2_sh3_sched.c
#   sudo ./ps2_sh3_sched sh3_s640_epochs.txt
set -eu

DEST="${1:-terasic@192.168.50.161:~}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DATA="$ROOT/sim/data/top_psmct32_raster_demo"

FILES="
$ROOT/tools/ps2_sh3_sched.c
$DATA/sh3_s640_epochs.txt
$DATA/sh3_s6400_tex_lpddr.mem
$DATA/sh3_s6401_tex_lpddr.mem
$DATA/sh3_s6402_tex_lpddr.mem
$DATA/feeder_sh3_s6400.mem
$DATA/feeder_sh3_s6401.mem
$DATA/feeder_sh3_s6402.mem
"

# fail early with a clear message if anything is missing (e.g. fixtures not emitted yet)
missing=0
for f in $FILES; do
    if [ ! -f "$f" ]; then echo "MISSING: $f" >&2; missing=1; fi
done
if [ "$missing" -ne 0 ]; then
    echo "" >&2
    echo "Regenerate the LOCAL 640 fixtures first:" >&2
    echo "  python3 tools/gs_make_sh3_scheduler_fixture.py --fb640 --emit" >&2
    exit 1
fi

echo "Copying 8 files -> $DEST (one password prompt):"
for f in $FILES; do echo "  $(basename "$f")"; done
# shellcheck disable=SC2086
exec scp $FILES "$DEST"
