#! /bin/bash -e

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

. faustpath
. faustoptflags
. usage.sh

CXXFLAGS+=" $MYGCCFLAGS"  # So that additional CXXFLAGS can be used

ARCHFILE=$FAUSTARCH/sndfile.cpp
FILE_MODE=""

echoHelp()
{
    usage faust2sndfile "[Faust options] <file.dsp>"
    require libsndfile
    echo "Process audio files with Faust DSP"
    option "Faust options"
    exit
}

if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

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

CXX=g++

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

    if [ $p = "-help" ] || [ $p = "-h" ]; then
       echoHelp
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done

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

for f in $FILES; do

    SRCDIR=$(dirname "$p")
    LINPUTS=$(faust $OPTIONS -uim "$f" | grep FAUST_INPUTS)
    NINPUTS=${LINPUTS//[^0-9]/}
    if [ $NINPUTS -gt 0 ]; then
        echo "Will process an input file to produce an output file. With the compiled program 'foo' you can add:"
        echo "[-bs <num>] to setup the rendering buffer size in frames (default: 64)"
        echo "[-c samples] for the number of frames to append beyond the input file (default: 0)"
        echo "[-ct <filename>] to use the <filename> control messages file"
        echo "[-rc 1] to use the foo.rc file"
        FILE_MODE="INPUT_OUTPUT_FILE"
    else
        echo "Will generate an output file. With the compiled program 'foo' you can add:"
        echo "[-bs <num>] to setup the rendering buffer size in frames (default: 64)"
        echo "[-bd 16|24|32] to setup the output file bit-depth (default: 16)"
        echo "[-sr <num>] to setup the output file sample rate (default: 44100)"
        echo "[-s <num>] to setup the output file length in frames, when -ct is not used (default: SR*5)"
        echo "[-ct <filename>] to use the <filename> control messages file"
        echo "[-rc 1] to use the foo.rc file"
        FILE_MODE="OUTPUT_FILE"
    fi

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

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

    # collect binary file name for FaustGIDE
    BINARIES="$BINARIES$SRCDIR/${f%.dsp}"

done

echo $BINARIES
