#! /bin/bash -e

#####################################################################
#                                                                   #
#               Process files with Faust DSP                        #
#               (c) Grame, 2017                                     #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags

CXXFLAGS=$MYGCCFLAGS

ARCHFILE=$FAUSTARCH/sndfile.cpp

#-------------------------------------------------------------------
# Set Faust include path

CXX=g++

#PHASE 2 : dispatch command arguments
while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "faust2sndfile <file.dsp>"
    fi
    
    if [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done

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

for f in $FILES; do

    # compile Faust DSP then create the binary
    faust -i -a $ARCHFILE $OPTIONS "$f" -o "$f.cpp" || exit
    (
        $CXX $CXXFLAGS "$f.cpp" `pkg-config --cflags --static --libs sndfile` -o "${f%.dsp}"
    ) > /dev/null || exit

    # remove c++ file
    rm "$f.cpp"

done


