#!/bin/bash

#-------------------------------------------------------------------
# Wrapping resources
HTML_FOOTER=""
CODE_WRAPPER1=""
CODE_WRAPPER2=""
JS_WRAPPER=""

LINKS=""
SVG=""

EMCC="false"
POLY="false"
OPT="false"
EXPORT="false"
EFFECT=""
WORKLET="false"
WASM=

OPTIONS="-ftz 2"

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

if [ -f $FAUST_LIB_PATH/music.lib ]
then
    FAUSTLIB=$FAUST_LIB_PATH
elif [ -f /usr/local/share/faust/music.lib ]
then
    FAUSTLIB=/usr/local/share/faust/
elif [ -f /usr/share/faust/music.lib ]
then
    FAUSTLIB=/usr/share/faust/
else
    echo "ERROR : $0 cannot find Faust library dir (usually /usr/local/share/faust)"
fi

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

while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "faust2webaudiowasm [-poly] [-effect auto|<effect.dsp>] [-links] [-opt] [-worklet] [-emcc] <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 '-links' to add links to source code and SVG diagrams in the generated HTML file"
        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 '-emcc' to compile C++ generated code to wasm with Emscripten, otherwise the internal wasm backend is used"
        exit
    elif [ $p = "-links" ]; then
        SVG="-svg"
        LINKS="<div style=\"text-align:center;height:20px\"> 
                <style>
				a:link {font-family:Arial; font-size:12px; color:#3D3C3A; text-decoration:none}
				a:visited {font-family:Arial; font-size:12px; color:#3D3C3A; text-decoration:none}
				a:hover {font-family:Arial; font-size:12px; color:white; text-decoration:none}
                </style>
            <a href=\"DSP.dsp\" target=\"_blank\">source</a> 
            <a href=\"DSP-svg/process.svg\" target=\"_blank\">diagram</a>
            </div>"
        EXPORT_FOOTER=export-wrapper.html
        EXPORT="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: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
        HTML_FOOTER=webaudio-wasm-poly-footer.html
    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
            HTML_FOOTER=webaudio-wasm-poly-worklet-footer.html
        else
            echo "Compiled with 'wasm' backend in polyphonic mode"
            CODE_WRAPPER1=webaudio-wasm-poly-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-poly-footer.html
        fi
    fi
else
    WASM="wasm"
    if [ $EMCC = "true" ]; then
        echo "Compiled with 'emcc'"
        CODE_WRAPPER1=webaudio-asm.cpp
        JS_WRAPPER=webaudio-wasm-emcc.js
        HTML_FOOTER=webaudio-wasm-footer.html
    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
            HTML_FOOTER=webaudio-wasm-worklet-footer.html
        else
            echo "Compiled with 'wasm' backend"
            CODE_WRAPPER1=webaudio-wasm-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-footer.html
        fi
    fi
fi

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

BINARIES=""

for f in $FILES; do
    name=$(basename "$f" .dsp)
    
    # compile the Faust DSP to C++ or wasm code
    if [ $EMCC = "true" ]; then
        faust $SVG -a $FAUSTLIB/webaudio/$CODE_WRAPPER1 -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
    else
        faust -lang $WASM $SVG $OPTIONS -cn $name $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="['_"$name"_constructor','_"$name"_destructor','_"$name"_init','_"$name"_getSampleRate','_"$name"_instanceInit','_"$name"_instanceConstants','_"$name"_instanceResetUserInterface','_"$name"_instanceClear','_"$name"_compute','_"$name"_getNumInputs','_"$name"_getNumOutputs','_"$name"_setParamValue','_"$name"_getParamValue','_"$name"_getJSON']"
        else        EXPORTED="['_"$name"_poly_constructor','_"$name"_poly_destructor','_"$name"_poly_init','_"$name"_poly_getSampleRate','_"$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']"
        fi
        # compile the C++ code to wasm
        emcc -O3 --memory-init-file 0 -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 $name.cpp --post-js $FAUSTLIB/webaudio/$JS_WRAPPER -o $name.js -s EXPORTED_FUNCTIONS=$EXPORTED || exit
 
        # remove intermediate C++ file
        rm $name.cpp
    fi
    
    #java -jar /usr/local/bin/yuicompressor-2.4.8.jar $name-temp1.js -o $name-temp1.js --charset utf-8
          
    # compose the self-contained HTML page
    echo "<html>" > $name-temp2.html
    echo "<head>" >> $name-temp2.html
    echo "<meta charset=\"UTF-8\">" >> $name-temp2.html

    # add AudioWorklet Origin Trial for Chrome
    echo "<!-- Origin Trial Token, feature = AudioWorklet, origin = https://faust.grame.fr, expires = 2018-03-03 --> <meta http-equiv=\"origin-trial\" data-feature=\"AudioWorklet\" data-expires=\"2018-03-03\" content=\"ApDZ87+zpjI09+oIyEDTPDZtGN4bs1lh9n83f+4ssENINML3DxJbklOsd8Hu+M82TY4eIP/B2l7Iued7Y5I4hQAAAABUeyJvcmlnaW4iOiJodHRwczovL2ZhdXN0LmdyYW1lLmZyOjQ0MyIsImZlYXR1cmUiOiJBdWRpb1dvcmtsZXQiLCJleHBpcnkiOjE1MjAxMjA2MDB9\">" >> $name-temp2.html

    echo "<style type=\"text/css\">" >> $name-temp2.html
    cat $FAUSTLIB/js/stylesheet.js >> $name-temp2.html
    echo "</style>" >> $name-temp2.html
    echo "<script type=\"text/javascript\">" >> $name-temp2.html
    cat $FAUSTLIB/js/jsscripts.js >> $name-temp2.html
    if [ $EMCC != "true" ]; then

        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" $FAUSTLIB/webaudio/$CODE_WRAPPER1 >> $name-processor.js
            sed -e "s/mydsp/"$name"/g" $FAUSTLIB/webaudio/$CODE_WRAPPER2 >> $name.js
        else
            sed -e "s/mydsp/"$name"/g" $FAUSTLIB/webaudio/$CODE_WRAPPER1 >> $name.js
        fi

    fi
    sed -e "s/mydsp/"$name"/g" $name.js >> $name-temp2.html
    echo "</script>" >> $name-temp2.html
    echo "</head>" >> $name-temp2.html
    echo "<body>" >> $name-temp2.html
    echo $LINKS >> $name-temp2.html
    cat $FAUSTLIB/webaudio/$HTML_FOOTER >> $name-temp2.html
    if [ $EXPORT = "true" ] ; then
        cat $FAUSTLIB/webaudio/$EXPORT_FOOTER >> $name-temp2.html
    fi
    echo "</body>" >> $name-temp2.html
    echo "</html>" >> $name-temp2.html
    sed -e "s/DSP/"$name"/g" $name-temp2.html > $name.html
    
    rm $name.js $name-temp2.html

    # collect binary file name
    if [ $WORKLET = "true" ]; then
        BINARIES="$BINARIES$name.html;$name-processor.js;"
        rm $name.wasm
        rm -f $name"_effect".wasm
    else
        BINARIES="$BINARIES$name.html;$name.wasm;"
    fi

done

echo $BINARIES
