#!/bin/bash

#####################################################################
#                                                                   #
#                        Faust2ESP32 generator                      #
#               (c) Romain Michon CCRMA and Grame, 2016             #
#                                                                   #
#####################################################################

. faustpath

# change if you want to get the log of what's happening
LOG="/dev/null"

# exit if a command fails
set -e

# global option variables
LIB="0"

echoHelp ()
{
    echo "FAUST2ESP32 HELP"
    echo "faust2esp32 can be used to fully program the ESP32 microncontroller and to generate DSP objects that can be integrated into any ESP32 project. "
    echo "Additional information about this tool can be found on the Faust website: https://faust.grame.fr."
    echo ""
    echo "USAGE:"
    echo "faust2esp32 -lib faustFile.dsp: Generates a package containing an object compatible with any ESP32 project."
    echo ""
    echo "Optional: -wm8978 or -ac101 to choose codec driver."
    echo ""
    # TODO option for board type
}

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

while [ $1 ]
do
	p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
		echoHelp
		exit 0
    elif [[ -f "$p" ]]; then
		FILE="$p"
    elif [ $p = "-lib" ]; then
		LIB=1
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done

if [ -z $FILE ]; then
    echo "Please, provide a Faust file to process"
    exit 1
fi

###########################
# Post Argument Processing
###########################

BASENAME=$(basename "$FILE")
MODULENAME="${BASENAME%.dsp}"

###########################
# Generating a lib package
###########################

if [ $LIB -eq 1 ]; then
    mkdir -p $MODULENAME
    faust -a $FAUSTARCH/esp32/esp32.cpp -i $FILE -o $MODULENAME/$MODULENAME.cpp
    cp $FAUSTARCH/esp32/esp32.h $MODULENAME/$MODULENAME.h
    if [ $OPTIONS = "-wm8978" ] || [ $OPTIONS = "-WM8978" ]; then
    		cp $FAUSTARCH/esp32/drivers/wm8978/WM8978.cpp $MODULENAME
    		cp $FAUSTARCH/esp32/drivers/wm8978/WM8978.h $MODULENAME
    elif [ $OPTIONS = "-ac101" ] || [ $OPTIONS = "-AC101" ]; then
    		cp $FAUSTARCH/esp32/drivers/ac101/AC101.cpp $MODULENAME
    		cp $FAUSTARCH/esp32/drivers/ac101/AC101.h $MODULENAME 
    else
    		cp $FAUSTARCH/esp32/drivers/wm8978/WM8978.cpp $MODULENAME
    		cp $FAUSTARCH/esp32/drivers/wm8978/WM8978.h $MODULENAME
    fi
    TMP=$(awk -v modName="$MODULENAME" '{gsub(/AudioFaust/,modName)}1' $MODULENAME/$MODULENAME.cpp)
    echo "$TMP" > $MODULENAME/$MODULENAME.cpp
    TMP=$(awk -v modName="$MODULENAME" '{gsub(/AudioFaust/,modName)}1' $MODULENAME/$MODULENAME.h)
    echo "$TMP" > $MODULENAME/$MODULENAME.h
    TMP=$(awk -v modName="$MODULENAME.h" '{gsub(/esp32.h/,modName)}1' $MODULENAME/$MODULENAME.cpp)
    echo "$TMP" > $MODULENAME/$MODULENAME.cpp
    TMP=$(awk -v modName="faust_$MODULENAME" '{gsub(/faust_esp32/,modName)}1' $MODULENAME/$MODULENAME.h)
    echo "$TMP" > $MODULENAME/$MODULENAME.h
    zip -r $MODULENAME.zip $MODULENAME > $LOG
    rm -r $MODULENAME
fi
