#!/bin/sh

set -e

if [ -z "$BS1770GAIN" ] ; then
    BS1770GAIN=bs1770gain
fi

if type valgrind > /dev/null 2>&1 ; then
    BS1770GAIN="valgrind $BS1770GAIN"
fi

at_exit() {
    set +x
    echo "info: test exiting, removing $WORKDIR"
    rm -rf $WORKDIR
}

WORKDIR=$(mktemp -d)
trap at_exit INT TERM EXIT

set -x

$BS1770GAIN -h

test_on_file() {
    file="$1"

    $BS1770GAIN $file
    mkdir $WORKDIR/tmp

    $BS1770GAIN $file -o $WORKDIR/tmp/test1
    for f in $WORKDIR/tmp/test1/*; do $BS1770GAIN $f; done

    $BS1770GAIN $file -a -o $WORKDIR/tmp/test2 
    for f in $WORKDIR/tmp/test2/*; do $BS1770GAIN $f; done

    $BS1770GAIN $file -a -o $WORKDIR/tmp/test3 
    for f in $WORKDIR/tmp/test3/*; do $BS1770GAIN --xml $f; done

    rm -rf $WORKDIR/tmp
}

test_on_file debian/tests/yell.wav

# Test using 64 bit samples to check alignment
for bits in 16 32 64 ; do
    file=$WORKDIR/sine-${bits}bit.wav
    sox -b $bits -n $file synth 3 sine 300-3300
    test_on_file $file
done
