#!/bin/sh

# Author: Dennis Braun <snd@debian.org>
# Simple test with lv2file, zam-plugins and ffmpeg

set -e

echo '(1/4) Copying beats.wav from csound-doc...'
if cp /usr/share/doc/csound-doc/html/examples/beats.wav.gz .; then
    gunzip beats.wav.gz && echo "Successfully decompressed beats.wav.gz"
else
    echo "Error: Failed to copy beats.wav.gz" >&2
    exit 1
fi

echo '(2/4) Finding ZamAutoSat plugin'
ZamAutoSat=$(/usr/bin/lv2file -l 2>/dev/null | grep ZamAutoSat | cut -f1)
if [ -z "$ZamAutoSat" ]; then
    echo "ZamAutoSat plugin not found"
else
    echo "Successfully found ZamAutoSat plugin ID: $ZamAutoSat"
fi

echo "(3/4) Applying ZamAutoSat plugin on beats.wav"
if lv2file -i beats.wav -o beats_autosat.wav "$ZamAutoSat" --ignore-clipping 2>/dev/null; then
    echo "Successfully applied ZamAutoSat plugin on beats.wav"
else
    echo "Error: Failed to apply ZamAutoSat plugin" >&2
    exit 1
fi

echo "(4/4) Check that the output file is a valid WAV file using ffmpeg"
if ! ffmpeg -v error -i beats_autosat.wav -f null -; then
    echo "Error: beats_autosat.wav is not a valid WAV file" >&2
    exit 1
else
    echo "The beats_autosat.wav looks like a valid WAVE file"
fi
