#!/bin/sh
# Compare complete output from two executions of the same native image.
set -eu
if [ "$#" -lt 4 ]; then
    printf 'usage: replay profile emulator image harts [machine-options...]\n' >&2
    exit 1
fi
profile=$1
emulator=$2
image=$3
harts=$4
shift 4
mkdir -p bin/acceptance/traces
prefix="bin/acceptance/traces/$profile-$harts"
for attempt in 1 2; do
    if ! "$emulator" -machine -harts="$harts" -memory-size=262144 -max-steps=2000000000 "$@" -run "$image" > "$prefix.$attempt.log" 2>&1; then
        cat "$prefix.$attempt.log"
        exit 1
    fi
done
if ! cmp -s "$prefix.1.log" "$prefix.2.log"; then
    diff -u "$prefix.1.log" "$prefix.2.log" || true
    printf 'replay: %s %s-hart output differs\n' "$profile" "$harts" >&2
    exit 1
fi
cat "$prefix.1.log"
printf 'replay: %s %s-hart output matched\n' "$profile" "$harts"
