#!/bin/bash
# autopkgtest: smoke test for the libgig command line tools (gigtools).
#
# 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

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

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

TESTDIR=$(cd "$(dirname "$0")" && pwd)

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"

GIG="$TESTDIR/openo_instrument_template_v1.1.gig"
[ -f "$GIG" ] || fail "no Gigasampler test file found"

log "gigasampler under test: $GIG"

log "checking that all gigtools binaries are installed"
for t in akaidump akaiextract dlsdump gig2mono gig2stereo gigdump gigextract \
         gigmerge korg2gig korgdump rifftree sf2dump sf2extract wav2gig; do
    command -v "$t" >/dev/null || fail "the tool $t is not installed"
done
log "all 14 gigtools binaries are installed"

log "checking the version banners"
for t in dlsdump gig2mono gig2stereo gigdump gigextract gigmerge korg2gig \
         korgdump rifftree sf2dump sf2extract wav2gig; do
    out=$("$t" -v 2>&1) || fail "$t -v failed"
    printf '%s\n' "$out"
    grep -q "using libgig" <<<"$out" \
        || fail "$t does not report the libgig version"
done
log "the 12 version-reporting tools all report the libgig version"

log "running sf2dump and rifftree on $SF2"
out=$(sf2dump "$SF2" 2>&1) || fail "sf2dump failed on $SF2"
printf '%s\n' "$out"
grep -q "Instruments" <<<"$out" \
    || fail "sf2dump did not report the instruments"
out=$(rifftree "$SF2" 2>&1) || fail "rifftree failed on $SF2"
printf '%s\n' "$out"
grep -q "RIFF(sfbk)" <<<"$out" \
    || fail "rifftree did not report the SoundFont RIFF tree"
log "sf2dump and rifftree processed the SoundFont"

log "running gigdump and rifftree on $GIG"
out=$(gigdump "$GIG" 2>&1) || fail "gigdump failed on $GIG"
printf '%s\n' "$out"
grep -q "Available Instruments" <<<"$out" \
    || fail "gigdump did not report the instruments"
out=$(rifftree "$GIG" 2>&1) || fail "rifftree failed on $GIG"
printf '%s\n' "$out"
grep -q "RIFF(DLS )" <<<"$out" \
    || fail "rifftree did not report the Gigasampler RIFF tree"
log "gigdump and rifftree processed the Gigasampler file"

log "extracting the Gigasampler samples with gigextract"
mkdir -p "$WORKDIR/gigextract"
out=$(gigextract "$GIG" "$WORKDIR/gigextract" 2>&1) \
    || fail "gigextract failed on $GIG"
printf '%s\n' "$out"
ls -l "$WORKDIR"/gigextract/
[ -n "$(ls -A "$WORKDIR"/gigextract/*.wav 2>/dev/null)" ] \
    || fail "gigextract did not create any .wav file"
log "gigextract extracted the samples to .wav files"

log "extracting a SoundFont sample with sf2extract"
mkdir -p "$WORKDIR/sf2extract"
out=$(sf2extract "$SF2" "$WORKDIR/sf2extract" 1 2>&1) \
    || fail "sf2extract failed on $SF2"
printf '%s\n' "$out"
ls -l "$WORKDIR"/sf2extract/
[ -n "$(ls -A "$WORKDIR"/sf2extract/*.wav 2>/dev/null)" ] \
    || fail "sf2extract did not create any .wav file"
log "sf2extract extracted a sample to a .wav file"

log "building a .gig from the extracted .wav with wav2gig (round trip)"
mkdir -p "$WORKDIR/wav2gig"
mv "$WORKDIR"/sf2extract/*.wav "$WORKDIR/wav2gig/a - dummy - 64 - 60 - c-3.wav"
out=$(wav2gig -f "$WORKDIR/wav2gig/roundtrip.gig" \
    "$WORKDIR/wav2gig/a - dummy - 64 - 60 - c-3.wav" 2>&1) \
    || fail "wav2gig failed to create a .gig from the .wav"
printf '%s\n' "$out"
out=$(gigdump "$WORKDIR/wav2gig/roundtrip.gig" 2>&1) \
    || fail "gigdump failed on the wav2gig output"
printf '%s\n' "$out"
grep -q "Available Instruments" <<<"$out" \
    || fail "the wav2gig output contains no instrument"
log "wav2gig created a .gig from the .wav (sf2extract -> wav2gig -> gigdump)"

log "running the in-place conversions gig2mono and gig2stereo"
cp "$GIG" "$WORKDIR/mono.gig"
out=$(gig2mono "$WORKDIR/mono.gig" 2>&1) || fail "gig2mono failed"
printf '%s\n' "$out"
gigdump "$WORKDIR/mono.gig" >/dev/null \
    || fail "the gig2mono output is not loadable"
cp "$GIG" "$WORKDIR/stereo.gig"
out=$(gig2stereo "$WORKDIR/stereo.gig" 2>&1) || fail "gig2stereo failed"
printf '%s\n' "$out"
gigdump "$WORKDIR/stereo.gig" >/dev/null \
    || fail "the gig2stereo output is not loadable"
log "gig2mono and gig2stereo converted the Gigasampler files in place"

log "PASS: the gigtools work on SoundFont and Gigasampler files"
exit 0
