#!/bin/bash -e

#####################################################################
#                                                                   #
#                    faust2daisy generator                          #
#                       (c) Grame, 2020                             #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

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

ARCHFILE=$FAUSTARCH/daisy/ex_faust.cpp

# exit if a command fails
set -e

# global option variables
NVOICES=0
MIDI=false
POLY=""
PATCH=false
POD=false
SR=44100
BS=16
SOURCE=false

echoHelp ()
{
    echo "faust2daisy [-patch] [-midi] [-nvoices <num>] [-sr <num>] [-bs <num>] [-source] [Faust options (-vec -vs 8...)] <file.dsp>"
    echo "Compiles Faust programs to Daisy boards"
    option
    option -patch "to compile for 4 ins/outs Patch (knob[1,2,3,4])"
    option -pod "to compile for 2 ins/outs Pod (knob[1,3])"
    options -midi
    option "-nvoices <num>"
    option "-sr <num>"
    option "-bs <num>"
    option -source
    option "Faust options"
    echo ""
    exit
}

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

###########################
# Processing Arguments
###########################

while [ $1 ]
do
    p=$1
    # help
    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echoHelp
    # -nvoices:
    elif [ $p = "-nvoices" ]; then
        shift
        NVOICES=$1
    # -midi
    elif [ $p = "-midi" ]; then
        MIDI=true
    #patch
    elif [ $p = "-patch" ]; then
        PATCH=true
    elif [ $p = "-pod" ]; then
        POD=true
    # -sr
    elif [ $p = "-sr" ]; then
        shift
        SR=$1
    # -bs
    elif [ $p = "-bs" ]; then
        shift
        BS=$1
    # -source
    elif [  $p = "-source" ]; then
        SOURCE=true
    elif [[ -f "$p" ]]; then
        FILE="$p"
    # other compile options
    else
        OPTIONS="$OPTIONS $p"
    fi

shift
done

###########################
# Compile the *.dsp files
###########################

for p in $FILE; do
    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")

    # creates the dir
    dspName="${f%.dsp}"
    rm -rf "$SRCDIR/$dspName"
    mkdir "$SRCDIR/$dspName"

    # compile faust to c++
    cp -r $FAUSTARCH/daisy/Makefile "$SRCDIR/$dspName/"
    faust -i -a $ARCHFILE $OPTIONS $f -o "$SRCDIR/$dspName/ex_faust.cpp" || exit

    # for PATCH support
    if [ $PATCH == true ]; then
        echo "#define PATCH" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"
    fi
    # for POD support
    if [ $POD == true ]; then
        echo "#define POD" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"
    fi

    # for MIDI
    if [ $MIDI == true ]; then
        echo "#define MIDICTRL" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"
    fi

    # for Sample Rate
    echo "#define MY_SAMPLE_RATE $SR" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"

    # for Buffer Size
    echo "#define MY_BUFFER_SIZE $BS" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"

    # for POLY
    if [ "$NVOICES" -gt "0" ]; then
        echo "#define POLY" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"
        echo "#define NVOICES $NVOICES" | cat - "$SRCDIR/$dspName/ex_faust.cpp" > temp && mv temp "$SRCDIR/$dspName/ex_faust.cpp"
    fi

    # compile and install plugin or keep the source folder
    if [ $SOURCE == false ]; then
        read -p "Press ENTER when Daisy is in DFU mode"
        ( cd "$SRCDIR/$dspName" && make && make program-dfu ) > /dev/null || exit
        rm -rf "$SRCDIR/$dspName"
        echo "Success !"
    else
        echo "Create the $SRCDIR/$dspName folder"
    fi

done
