#!/bin/bash
# autopkgtest: smoke test for the libgig C++ library and the restricted
# runtime environment (JACK dummy backend, SDL dummy drivers).
#
# AI assisted code, reviewed and tested by snd@debian.org

set -eu

WORKDIR=${AUTOPKGTEST_TMP:-$(mktemp -d)}
mkdir -p "$WORKDIR"
export AUTOPKGTEST_TMP="$WORKDIR"

export LC_ALL=C.UTF-8
export SDL_VIDEODRIVER=dummy
export SDL_AUDIODRIVER=dummy
export JACK_NO_AUDIO_RESERVATION=1
export JACK_DEFAULT_SERVER="libgig-autopkgtest-$$"

JACK_PID=

cleanup() {
    if [ -n "$JACK_PID" ]; then
        kill "$JACK_PID" 2>/dev/null || true
        wait "$JACK_PID" 2>/dev/null || true
        JACK_PID=
    fi
}
trap cleanup EXIT

log() {
    echo "[libgig-autopkgtest] $*"
}

fail() {
    echo "[libgig-autopkgtest] FAILED: $*" >&2
    exit 1
}

SF2=/usr/share/sounds/sf2/default-GM.sf2
if [ ! -f "$SF2" ]; then
    SF2=$(find /usr/share/sounds/sf2 -maxdepth 1 -name '*.sf2' 2>/dev/null | sort | head -n 1)
fi
[ -n "$SF2" ] && [ -f "$SF2" ] || fail "no SoundFont file found"

log "soundfont under test: $SF2"

TESTDIR=$(cd "$(dirname "$0")" && pwd)
GIG="$TESTDIR/openo_instrument_template_v1.1.gig"
[ -f "$GIG" ] || fail "no Gigasampler test file found"

log "gigasampler under test: $GIG"

log "starting JACK server on the dummy backend"
jackd -r -n "$JACK_DEFAULT_SERVER" -d dummy -r 48000 -p 1024 \
    >"$WORKDIR/jackd.log" 2>&1 &
JACK_PID=$!
i=0
JACK_READY=0
while [ "$i" -lt 150 ]; do
    if ! kill -0 "$JACK_PID" 2>/dev/null; then
        wait "$JACK_PID" 2>/dev/null || true
        JACK_PID=
        fail "JACK server terminated during startup"
    fi
    if grep -q "JACK server starting" "$WORKDIR/jackd.log" 2>/dev/null; then
        JACK_READY=1
        break
    fi
    i=$((i + 1))
    sleep 0.2
done
if [ "$JACK_READY" != 1 ]; then
    JACK_PID=
    cat "$WORKDIR/jackd.log" || true
    fail "JACK server did not become ready"
fi
if ! kill -0 "$JACK_PID" 2>/dev/null; then
    JACK_PID=
    cat "$WORKDIR/jackd.log" || true
    fail "JACK server terminated during startup"
fi
log "JACK server is ready on the dummy backend"

log "checking the SDL dummy drivers"
SDL_PROBE="$WORKDIR/sdl_probe"
c++ -O2 -o "$SDL_PROBE" "$TESTDIR/sdl_probe.cpp" \
    $(pkgconf --cflags --libs sdl2) \
    || fail "cannot compile the SDL probe"
out=$("$SDL_PROBE" 2>&1) || fail "the SDL dummy drivers are not usable"
printf '%s\n' "$out"
grep -q "SDL_DUMMY_OK" <<<"$out" \
    || fail "the SDL dummy drivers did not initialize"
log "SDL dummy video and audio drivers work headless"

log "building the libgig probe"
PROBE="$WORKDIR/gig_probe"
GIG_VERSION=$(pkgconf --modversion gig) \
    || fail "cannot determine the libgig version via pkgconf"
log "libgig version: $GIG_VERSION"
c++ -O2 -o "$PROBE" "$TESTDIR/gig_probe.cpp" \
    $(pkgconf --cflags --libs gig) \
    || fail "cannot compile the libgig probe"

log "running the libgig probe against $SF2"
out=$("$PROBE" "$SF2" 2>&1) || fail "the libgig probe failed"
printf '%s\n' "$out"
grep -q "LIBGIG_PROBE_OK" <<<"$out" \
    || fail "the libgig probe did not complete successfully"
grep -q "libgig instruments: [1-9]" <<<"$out" \
    || fail "the libgig SoundFont contains no instruments"
grep -q "libgig samples: [1-9]" <<<"$out" \
    || fail "the libgig SoundFont contains no samples"
log "libgig library loaded the SoundFont and enumerated instruments"

log "running the libgig probe against $GIG"
out=$("$PROBE" "$GIG" 2>&1) \
    || fail "the libgig probe failed on the Gigasampler file"
printf '%s\n' "$out"
grep -q "LIBGIG_PROBE_OK" <<<"$out" \
    || fail "the libgig probe did not complete successfully on the Gigasampler file"
grep -q "libgig gig instruments: [1-9]" <<<"$out" \
    || fail "the libgig Gigasampler file contains no instruments"
grep -q "libgig gig samples: [1-9]" <<<"$out" \
    || fail "the libgig Gigasampler file contains no samples"
log "libgig library loaded the Gigasampler file and enumerated instruments"

log "shutting down the JACK server"
kill "$JACK_PID" 2>/dev/null || true
wait "$JACK_PID" 2>/dev/null || true
JACK_PID=

log "PASS: the libgig C++ API (sf2 + gig) and the runtime environment work headless (JACK dummy, SDL dummy)"
exit 0
