#!/bin/bash

. faustpath
. faustoptflags
. usage.sh

#-------------------------------------------------------------------
# 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"

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

while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        usage faust2webaudiowasm "[options] <file.dsp>"
        option
        option -poly
        option "-effect <effect.dsp>"
        option "-effect auto"
        option -opt "optimize the wasm module using Binaryen tools (https://github.com/WebAssembly/binaryen)"
        option -worklet "generates AudioWorklet compatible code"
        option -links "add links to source code and SVG diagrams in the generated HTML file"
        option -emcc "use the EMCC generated glue (mandatory when using 'soundfiles' in the DSP code) [experimental]"
        exit
    fi
     
    if [ $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 [[ -f "$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
    if [ $WORKLET = "true" ]; then
        if [ $EMCC = "true" ]; then
            echo "Compiled with 'wasm' backend in polyphonic and AudioWorklet mode with EMCC glue"
            WASM="wasm-eb"
            CODE_WRAPPER2=webaudio-workletnode-emcc-poly-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-emcc-poly-worklet-footer.html
        else
            echo "Compiled with 'wasm' backend in polyphonic and AudioWorklet mode"
            WASM="wasm-eb"
            CODE_WRAPPER1=webaudio-workletprocessor-poly-standalone-wrapper.js
            CODE_WRAPPER2=webaudio-workletnode-poly-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-poly-worklet-footer.html
            echo "The mixer32.wasm code is copied"
            cp $FAUSTARCH/webaudio/mixer32.wasm .
        fi
    else
        if [ $EMCC = "true" ]; then
            echo "Compiled with 'wasm' backend in polyphonic mode with EMCC glue"
            WASM="wasm-eb"
            CODE_WRAPPER1=webaudio-wasm-emcc-poly-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-emcc-poly-footer.html
        else
            echo "Compiled with 'wasm' backend in polyphonic mode"
            WASM="wasm-e"
            CODE_WRAPPER1=webaudio-wasm-poly-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-poly-footer.html
            echo "The mixer32.wasm code is copied"
            cp $FAUSTARCH/webaudio/mixer32.wasm .
        fi
    fi
else
    if [ $WORKLET = "true" ]; then
        if [ $EMCC = "true" ]; then
            WASM="wasm-eb"
            echo "Compiled with 'wasm' backend in AudioWorklet mode with EMCC glue"
            CODE_WRAPPER2=webaudio-workletnode-emcc-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-emcc-worklet-footer.html
        else
            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
        fi
    else
        if [ $EMCC = "true" ]; then
            echo "Compiled with 'wasm' backend with EMCC glue"
            WASM="wasm-eb"
            CODE_WRAPPER1=webaudio-wasm-emcc-standalone-wrapper.js
            HTML_FOOTER=webaudio-wasm-emcc-footer.html
        else
            echo "Compiled with 'wasm' backend"
            WASM="wasm-ib"
            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)
    
    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

    # compose the self-contained HTML page
    echo "<html>" > $name-temp2.html
    echo "<head>" >> $name-temp2.html
    echo "<meta charset=\"UTF-8\">" >> $name-temp2.html
    if [ $EMCC = "true" ]; then
        echo "<script src="libfaust-glue.js"></script>" >> $name-temp2.html
    fi
    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
    fi

    if [ $WORKLET = "true" ]; then
        if [ $EMCC = "true" ]; then
            sed -e "s/mydsp/"$name"/g" $FAUSTLIB/webaudio/$CODE_WRAPPER2 >> $name.js
        else
            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
        fi
    else
        sed -e "s/mydsp/"$name"/g" $FAUSTLIB/webaudio/$CODE_WRAPPER1 >> $name.js
    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
    if [ $EMCC = "true" ]; then
        cp $name-temp2.html $name.html
    else
        sed -e "s/DSP/"$name"/g" $name-temp2.html > $name.html
    fi
   
    # collect binary file name
    if [ $WORKLET = "true" ]; then
        if [ $EMCC = "true" ]; then
        	cp $FAUSTLIB/webaudio/libfaust-glue.js $FAUSTLIB/webaudio/libfaust-glue.wasm $FAUSTLIB/webaudio/libfaust-worklet-glue.js .
            BINARIES="$BINARIES$name.html;$name.wasm;libfaust-glue.js;libfaust-glue.wasm;libfaust-worklet-glue.js;"
        else
            BINARIES="$BINARIES$name.html;$name-processor.js;$name.wasm;"
        fi
    else
        if [ $EMCC = "true" ]; then
            cp $FAUSTLIB/webaudio/libfaust-glue.js $FAUSTLIB/webaudio/libfaust-glue.wasm $FAUSTLIB/webaudio/libfaust-worklet-glue.js .
            BINARIES="$BINARIES$name.html;$name.wasm;libfaust-glue.js;libfaust-glue.wasm;libfaust-worklet-glue.js;"
        else
            BINARIES="$BINARIES$name.html;$name.wasm;"
        fi
    fi

    # cleanup
    rm $name.js $name-temp2.html

done

echo $BINARIES
