#!/bin/bash

#####################################################################
#                                                                   #
#               Compiles Faust programs to impulse tests			#
#               (c) Grame, 2017                                     #
#                                                                   #
#####################################################################

for p in $@; do
    if [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi
done

P=$(dirname $0)

#-------------------------------------------------------------------
# compile the *.dsp files

for f in $FILES; do

	# compile Faust to c++
    faust -lang c $OPTIONS -i -a $P/archs/impulsearch2.cpp "$f" -o "$f.cpp" || exit

	# compile c++ to binary
	(
		${CXX=g++} ${CXXFLAGS=-O3 -std=c++11 -I$P -I../archs -pthread} $OMP "$f.cpp" -o "${f%.dsp}"
	) > /dev/null || exit

	# run the resulting binary to generate the impulse response
	./"${f%.dsp}"

	# cleanup
    rm "${f%.dsp}" "$f.cpp"
done

