#!/bin/bash

. faustpath
. faustoptflags

#-------------------------------------------------------------------
# Wrapping resources

CODE_WRAPPER1=""
CODE_WRAPPER2=""
JS_WRAPPER=""
COMB="false"
COMB_SRC=
COMB_EXPORTED=
COMB_WRAPPED=
COMB_WRAPPED_FILES=
COMB_SEP=
WASM=
WORKLET="false"
EFFECT=""
WAP="false"
NPM="false"

OPT="false"
EMCC="false"
POLY="false"

OPTIONS="-ftz 2"

EXPORTED_MONO="['_"$name"_constructor','_"$name"_destructor','_"$name"_getSampleRate','_"$name"_init','_"$name"_instanceInit','_"$name"_instanceConstants','_"$name"_instanceResetUserInterface','_"$name"_instanceClear','_"$name"_compute','_"$name"_getNumInputs','_"$name"_getNumOutputs','_"$name"_setParamValue','_"$name"_getParamValue','_"$name"_getJSON']"

EXPORTED_POLY="['_"$name"_poly_constructor','_"$name"_poly_destructor','_"$name"_poly_getSampleRate','_"$name"_poly_init','_"$name"_poly_instanceInit','_"$name"_poly_instanceConstants','_"$name"_poly_instanceResetUserInterface','_"$name"_poly_instanceClear','_"$name"_poly_compute','_"$name"_poly_getNumInputs','_"$name"_poly_getNumOutputs','_"$name"_poly_setParamValue','_"$name"_poly_getParamValue','_"$name"_poly_getJSON','_"$name"_poly_keyOn','_"$name"_poly_keyOff','_"$name"_poly_allNotesOff','_"$name"_poly_ctrlChange','_"$name"_poly_pitchWheel']"

#-------------------------------------------------------------------
# Analyze command arguments :
# faust options                 -> OPTIONS
# existing *.dsp files          -> FILES
#

while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "faust2wasm [-poly] [-effect auto|<effect.dsp>] [-comb] [-opt] [-worklet] [-emcc] [-wap] [-npm] <file.dsp>"
        echo "Use '-poly' to produce a polyphonic DSP, ready to be used with MIDI events"
        echo "Use '-effect <effect.dsp>' to produce a polyphonic DSP connected to a global output effect, ready to be used with MIDI"
        echo "Use '-effect auto' to produce a polyphonic DSP connected to a global output effect defined as 'effect' in <file.dsp>, ready to be used with MIDI"
        echo "Use '-opt' to optimize the wasm module using Binaryen tools (https://github.com/WebAssembly/binaryen)"
        echo "Use '-worklet' to generate AudioWorklet compatible code"
        echo "Use '-wap' to generate a WAP (Web Audio Plugin). This forces -worklet mode, and create additional files"
        echo "Use '-comb' to combine several DSPs in a unique resulting 'comb.js' file, sharing the same Emscripten runtime"
        echo "Use '-emcc' to compile C++ generated code to wasm with Emscripten, otherwise the internal wasm backend is used [experimental]"
        echo "Use '-npm' to add a package.json file for npm package distribution"
        exit
    elif [ $p = "-comb" ]; then
        COMB="true"
    elif [ $p = "-poly" ]; then
        POLY="true"
    elif [ $p = "-effect" ]; then
        shift
        EFFECT=$1
    elif [ $p = "-opt" ]; then
        OPT="true"
    elif [ $p = "-worklet" ]; then
        WORKLET="true"
    elif [ $p = "-emcc" ]; then
        EMCC="true"
    elif [ $p = "-wap" ]; then
        WAP="true"
        echo "Forcing -worklet mode"
        WORKLET="true"
    elif [ $p = "-npm" ]; then
        NPM="true"
    elif [ ${p:0:1} = "-" ]; then
	    OPTIONS="$OPTIONS $p"
	elif [[ -e "$p" ]]; then
	    FILES="$FILES $p"
	else
	    OPTIONS="$OPTIONS $p"        
	fi

shift

done

echo "Compiling with :" $OPTIONS

#-------------------------------------------------------------------
# Set the compilation wrapping files depending of the compilation options
#

if [ $POLY = true ]; then
    WASM="wasm-e"
    if [ $EMCC = "true" ]; then
        echo "Compiled with 'emcc' in polyphonic mode"
        CODE_WRAPPER1=webaudio-asm-poly.cpp
        JS_WRAPPER=webaudio-wasm-poly-emcc.js
    else
        if [ $WORKLET = "true" ]; then
            WASM="wasm-eb"
            echo "Compiled with 'wasm' backend in polyphonic and AudioWorklet mode"
            CODE_WRAPPER1=webaudio-workletprocessor-poly-standalone-wrapper.js
            CODE_WRAPPER2=webaudio-workletnode-poly-standalone-wrapper.js
        else
            echo "Compiled with 'wasm' backend in polyphonic mode"
            CODE_WRAPPER1=webaudio-wasm-poly-standalone-wrapper.js
        fi
    fi
else
    WASM="wasm"
    if [ $EMCC = "true" ]; then
        echo "Compiled with 'emcc'"
        CODE_WRAPPER1=webaudio-asm.cpp
        JS_WRAPPER=webaudio-wasm-emcc.js
    else
        if [ $WORKLET = "true" ]; then
            WASM="wasm-ib"
            echo "Compiled with 'wasm' backend in AudioWorklet mode"
            CODE_WRAPPER1=webaudio-workletprocessor-standalone-wrapper.js
            CODE_WRAPPER2=webaudio-workletnode-standalone-wrapper.js
        else
            echo "Compiled with 'wasm' backend"
            CODE_WRAPPER1=webaudio-wasm-standalone-wrapper.js
        fi
    fi
fi

#-------------------------------------------------------------------
# compile the *.dsp files
#
BINARIES=""

if [ $COMB = "false" ]; then

for f in $FILES; do
    name=$(basename "$f" .dsp)
    
    # compile the Faust DSP to C++ or wasm code
    if [ $EMCC = "true" ] ; then
        faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
    else
        faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit

        # possibly compile effect
        if [ "$EFFECT" = "auto" ]; then
        cat > $name"_effect".dsp << EndOfCode
            adapt(1,1) = _;
            adapt(2,2) = _,_;
            adapt(1,2) = _ <: _,_;
            adapt(2,1) = _,_ :> _;
            adaptor(F,G) = adapt(outputs(F),inputs(G));
            process = adaptor(library("$f").process, library("$f").effect) : library("$f").effect;
EndOfCode
            faust -lang $WASM $OPTIONS -cn effect $name"_effect".dsp -o $name"_effect".wasm || exit
            rm $name"_effect".dsp
        elif [ "$EFFECT" != "" ]; then
            faust -lang $WASM $OPTIONS -cn effect $EFFECT -o $name"_effect".wasm || exit
        fi

        # wasm ==> wasm optimizations
        if [ $OPT = "true" ]; then
            echo "Optimize wasm module"
            wasm-opt $name.wasm -O3 -o $name.wasm
        fi
    fi

    if [ $EMCC = "true" ]; then
        
        # prepare emcc compilation files
        if [ $POLY = false ]; then
            EXPORTED=EXPORTED_MONO
        else
            EXPORTED=EXPORTED_POLY
        fi
    
        # compile the C++ code to wasm
        emcc -O2 --memory-init-file 0 $name.cpp -s TOTAL_MEMORY=100663296 --post-js $FAUSTARCH/webaudio/$JS_WRAPPER -o $name-temp.js \
            -s EXPORTED_FUNCTIONS=$EXPORTED || exit
       
        # compose the wasm code
        sed -e "s/DSP/"$name"/g" $name-temp.js > $name.js
        
        rm $name-temp.js
        rm $name.cpp
    else

        if [ "$EFFECT" != "" ]; then
            cat $name"_effect".js >> $name.js
            rm $name"_effect".js
        fi

        if [ $WORKLET = "true" ]; then
            cp $name.js $name-processor.js
            sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name-processor.js
            sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER2 >> $name.js
        else
            sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name.js
        fi
    fi

    # create additional files for WAP
	if [ $WAP = true ]; then

   		# create the main.json file
    	echo "{" > main.json
    	echo "\"name\": \"$name\"," >> main.json
    	echo "\"vendor\": \"Faust\"," >> main.json
    	echo "\"version\": \"1.01\"," >> main.json
    	echo "\"homepage\": \"https://faust.grame.fr\"," >> main.json
    	echo "\"thumbnail\": \"\"" >> main.json
    	echo "}" >> main.json
    	
        # compose the HTML test page
        if [ $POLY = true ]; then
        	cp  $FAUSTARCH/webaudio/testWAPPoly.html test$name-tmp1.html
        else
        	cp  $FAUSTARCH/webaudio/testWAP.html test$name-tmp1.html
        fi
        sed -e "s/VENDOR/"Faust"/g" test$name-tmp1.html >> test$name-tmp2.html
        sed -e "s/DSP/"$name"/g" test$name-tmp2.html > test$name.html
    fi

    # collect binary file name
    if [ $WORKLET = "true" ]; then
    	rm -f $name"_effect".wasm test$name-tmp1.html test$name-tmp2.html
    	if [ $WAP = "true" ]; then
    		mv $name.js main.js
    		mv test$name.html index.html
        	BINARIES="$BINARIES main.js, $name.wasm, main.json, index.html"
        else
        	BINARIES="$BINARIES$name.js, $name-processor.js, $name.wasm"
        fi
    else
        BINARIES="$BINARIES$name.js, $name.wasm"
    fi

    if [ "$NPM" = "true" ]; then
        sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json > package.json
        BINARIES="$BINARIES, package.json"
    fi
    
    BINARIES="$BINARIES;"

done

else

# TODO : worklet support
echo "Compiled with 'comb' mode"

for f in $FILES; do
    name=$(basename "$f" .dsp)
    
    # compile the Faust DSP to C++ or wasm code
    if [ $EMCC = "true" ] ; then
        faust -a $FAUSTARCH/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
    else
        faust -lang $WASM -cn $name $OPTIONS $f -o $name.wasm || exit

        # wasm ==> wasm optimizations
        if [ $OPT = "true" ]; then
            echo "Optimize $name wasm module"
            wasm-opt $name.wasm -O3 -o $name.wasm
        fi

        # possibly compile effect
        if [ "$EFFECT" = "auto" ]; then
            cat > $name"_effect".dsp << EndOfCode
            adapt(1,1) = _;
            adapt(2,2) = _,_;
            adapt(1,2) = _ <: _,_;
            adapt(2,1) = _,_ :> _;

            adaptor(F,G) = adapt(outputs(F),inputs(G));

            process = adaptor(library("$f").process, library("$f").effect) : library("$f").effect;
EndOfCode
            faust -lang $WASM $OPTIONS -cn effect $name"_effect".dsp -o $name"_effect".wasm || exit
            rm $name"_effect".dsp
        elif [ "$EFFECT" != "" ]; then
            faust -lang $WASM $OPTIONS -cn effect $EFFECT -o $name"_effect".wasm || exit
        fi
    fi

    if [ $EMCC = "true" ]; then

        # prepare emcc compilation files
        if [ $POLY = false ]; then
            EXPORTED=EXPORTED_MONO
        else
            EXPORTED=EXPORTED_POLY
        fi

        # compose the wasm code
        sed -e "s/DSP/"$name"/g" $FAUSTARCH/webaudio/$JS_WRAPPER > $name-wrapper.js
        
        COMB_SRC+=$name.cpp
        COMB_SRC+=" "
        
        COMB_EXPORTED+=$COMB_SEP$EXPORTED
        COMB_SEP=","
        
        COMB_WRAPPED_FILES+=$name-wrapper.js
        COMB_WRAPPED_FILES+=" "
        
        COMB_WRAPPED+=" --post-js "
        COMB_WRAPPED+=$name-wrapper.js
        
    else
        echo $name.js

        if [ "$EFFECT" != "" ]; then
            cat $name"_effect".js >> $name.js
            rm $name"_effect".js
        fi

        sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/$CODE_WRAPPER1 >> $name.js
        cat $name.js >> comb.js
        rm $name.js
    fi
  	
done

if [ $EMCC = "true" ]; then
    # compile final file
    emcc -O2 --memory-init-file 0 $COMB_SRC -s TOTAL_MEMORY=100663296 $COMB_WRAPPED -o comb.js \
        -s EXPORTED_FUNCTIONS="["$COMB_EXPORTED"]" || exit

    rm $COMB_SRC $COMB_WRAPPED_FILES
fi

# collect binary file name
BINARIES="comb.js"

if [ "$NPM" = "true" ]; then
    sed -e "s/mydsp/"$name"/g" $FAUSTARCH/webaudio/package.json > package.json
    BINARIES="$BINARIES, package.json"
fi

BINARIES="$BINARIES;"

fi

echo $BINARIES
