#!/bin/bash

#####################################################################
#                                                                   #
#               Compiles Faust programs to dummy                    #
#               (c) Grame, 2017                                     #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags

ARCHFILE=$FAUSTARCH/dummy-mem.cpp

CXXFLAGS=$MYGCCFLAGS

OSCDEFS=""
NVOICES=-1
OSCDEFS=""
HTTPDEFS=""
MIDIDEFS=""

#-------------------------------------------------------------------
# Analyze command arguments :
# faust options                 -> OPTIONS
# if -omp : -openmp or -fopenmp -> OPENMP
# existing *.dsp files          -> FILES
#

if [[ $(uname) == Darwin ]]; then
    ARCHLIB="-framework CoreServices -framework CoreMIDI -framework CoreFoundation -framework CoreAudio -framework AudioUnit"
else
    ARCHLIB="`pkg-config --cflags --libs alsa` -lpthread"
fi

#PHASE 2 : dispatch command arguments
while [ $1 ]
do
    p=$1
    if [ $p = "-osc" ]; then
        OSCDEFS="-DOSCCTRL -lOSCFaust"
    elif [ $p = "-httpd" ]; then
        HTTPDEFS="-DHTTPCTRL -lHTTPDFaust -lmicrohttpd"
    elif [ $p = "-nvoices" ]; then
        shift
        NVOICES=$1
        if [ $NVOICES -ge 0 ]; then
            CXXFLAGS="$CXXFLAGS -DNVOICES=$NVOICES"
        fi
    elif [ $p = "-midi" ]; then
        MIDIDEFS="-DMIDICTRL"
    elif [ ${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 using dummy
#
for f in $FILES; do
	
	# compile faust to c++
	faust -i -a $ARCHFILE -mem $OPTIONS "$f" -o "$f.cpp" || exit

	# compile c++ to binary
	(
		$CXX $CXXFLAGS "$f.cpp" $OSCDEFS $HTTPDEFS $POLYDEFS $MIDIDEFS $ARCHLIB -o "${f%.dsp}"
	) > /dev/null || exit
	rm "$f.cpp"

	# collect binary file name for FaustWorks
	BINARIES="$BINARIES${f%.dsp};"
done

echo $BINARIES


