#!/bin/sh

set -e

retval=0

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

sox -n $WORKDIR/file.wav synth 1 sine 1000
if ! $BS1770GAIN --xml $WORKDIR/file.wav | xmllint - ; then
    echo "error: xmllint reject XML output with progress information"
    retval=1
else
    echo "success: xmllint accept XML output with progress information"
fi

sox -n $WORKDIR/'1 & 2.wav' synth 1 sine 1000
if ! $BS1770GAIN --xml --suppress-progress $WORKDIR/1*.wav | xmllint - ; then
    echo "error: xmllint rejected XML output with ampersant in filename"
    retval=1
else
    echo "success: xmllint accepted XML output with ampersant in filename"
fi

# Neiter test work with 0.6.7, so make the test non-fatal for now
#exit $retval
