#!/bin/bash

. faustpath
. faustoptflags

#-------------------------------------------------------------------
# Wrapping resources
HTML_FOOTER=""
CODE_WRAPPER=""
JS_WRAPPER=""

LINKS=""
SVG=""

EMCC="false"
POLY="false"
EXPORT="false"

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

for p in $@; do
    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "faust2webaudioasm [-poly] [-links] [-emcc] <file.dsp>"
        echo "Use '-poly' to produce a polyphonic DSP, ready to be used with MIDI events"
        echo "Use '-links' to add links to source code and SVG diagrams in the generated HTML file"
        echo "Use '-emcc' to compile C++ generated code to asm.js with Emscripten, otherwise the internal asm.js 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 = "-emcc" ]; then
        EMCC="true"
    elif [ ${p:0:1} = "-" ]; then
	    OPTIONS="$OPTIONS $p"
	elif [[ -e "$p" ]]; then
	    FILES="$FILES $p"
	else
	    OPTIONS="$OPTIONS $p"        
	fi
done

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

if [ $POLY = "true" ]; then
    HTML_FOOTER=webaudio-asm-poly-footer.html
    if [ $EMCC = "true" ]; then
        echo "Compiled with 'emcc' in polyphonic mode"
        CODE_WRAPPER=webaudio-asm-poly.cpp
        JS_WRAPPER=webaudio-asm-poly-emcc.js
    else
        echo "Compiled with 'asm.js' backend in polyphonic mode"
        CODE_WRAPPER=webaudio-asm-poly-standalone-wrapper.js
    fi
else
    HTML_FOOTER=webaudio-asm-footer.html
    if [ $EMCC = "true" ]; then
        echo "Compiled with 'emcc'"
        CODE_WRAPPER=webaudio-asm.cpp
        JS_WRAPPER=webaudio-asm-emcc.js
    else
        echo "Compiled with 'asm.js' backend"
        CODE_WRAPPER=webaudio-asm-standalone-wrapper.js
    fi
fi

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

BINARIES=""

for f in $FILES; do
    name=$(basename "$f" .dsp)
    
    # compile the Faust DSP to C++ or asm.js code
    if [ $EMCC = "true" ]; then
        faust $SVG -a $FAUSTLIB/webaudio/$CODE_WRAPPER -i -uim -cn $name $OPTIONS $f -o $name.cpp || exit
    else
        faust -lang ajs $SVG -a $FAUSTLIB/webaudio/$CODE_WRAPPER -cn $name $OPTIONS $f -o $name-temp1.js || exit
    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"_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_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 asm.js
        emcc -O2 --memory-init-file 0 $name.cpp -s TOTAL_MEMORY=100663296 --post-js $FAUSTLIB/webaudio/$JS_WRAPPER -o $name-temp1.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
    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
    cat $FAUSTLIB/webaudio/WebMIDIAPI.js >> $name-temp2.html
    sed -e "s/DSP/"$name"/g" $name-temp1.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-temp1.js
    rm $name-temp2.html
 
    # collect binary file name
	BINARIES="$BINARIES$name.html;"

done

echo $BINARIES
