#!/usr/bin/env bash
# Tail Lambda's artifacts/ + reports/eval/ and pull new files to the Pi.
# Lightweight — pure rsync, no compute.
set -uo pipefail
KEY=$HOME/.ssh/lambda_ed25519
REMOTE=ubuntu@129.153.93.192
REPO=/home/max/.env/CIS490
mkdir -p "$REPO/artifacts" "$REPO/reports/eval"
while true; do
    n0=$(ls -1 "$REPO/artifacts"/*.ckpt.json 2>/dev/null | wc -l)
    rsync -aq -e "ssh -i $KEY -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10" \
        "$REMOTE:cis490/artifacts/" "$REPO/artifacts/" 2>&1
    rsync -aq -e "ssh -i $KEY -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10" \
        "$REMOTE:cis490/reports/eval/" "$REPO/reports/eval/" 2>&1
    n1=$(ls -1 "$REPO/artifacts"/*.ckpt.json 2>/dev/null | wc -l)
    if [[ "$n1" -gt "$n0" ]]; then
        echo "[$(date +%H:%M:%S)] +$((n1 - n0)) new artifacts (total=$n1)"
    fi
    sleep 30
done
