#!/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

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

for f in $FILES; do

    name=$(basename "$f" .dsp)
    name_exp=$name"_exp.dsp"
    dirname=$(dirname "$f");

  	# compile Faust to c++
    faust -e "$f"
    cd $dirname
    faust $OPTIONS -i -A .. -a impulsearch.cpp $name_exp -o "$name.cpp" || exit

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

	# run the resulting binary to generate the impulse response
	./"$name" -n 30000

	# cleanup
    rm "$name" "$name.cpp" $name_exp

done

