#! /bin/bash -e

#############################################################################################
#                                                                                           #
#   Compiles Faust programs to Microsoft libraries suitable for the Unity environment       #
#                                                                                           #
#   (c) Grame, 2017                                                                         #
#                                                                                           #
#############################################################################################

#   This script shouldn't be used on its own, use the "faust2unity" script instead
#   The libraries are firstly wrapped by the unityplugin.cpp architecture file
#   They have to be used in the Unity environment
#   The libraries does not work by themselves, they need the FaustPlugin_<dspname>.cs and FaustUtilities_<dspname>.cs files to be functionnal
#   These files are automatically generated by the "faust2unity" script

. faustpath

#-----------------------------------------------------------------------
# Dispatch command arguments

for p in $@; do
    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echo "faust2unitywin -32 -64 <file1.dsp> [<file2.dsp>]"
        echo "Create Microsoft 32/64bits library for faust unity plugin."
        echo "Mingw crosscompiler should be installed ('mingw32' package on Ubuntu)"
        echo "Use 'faust2unity -w32 -w64' to also create the C# and JSON files"
        echo "See architecture/unity/README.md for more info"
        exit
    fi

    if [ $p = "-32" ]; then
        BITS="$BITS 32"
    elif [ $p = "-64" ]; then
        BITS="$BITS 64"
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi
done

# Check at least one bit setting has been specified
if [ -z "$BITS"  ]; then
    echo "bits not specified, pass -32 and/or -64"
    exit
fi

#-----------------------------------------------------------------------
# Compiler settings

CXXFLAGS="-O3 -mfpmath=sse -msse -ffast-math -Wl,--enable-auto-import"
LIB="-shared"
EXT=".dll"


#-----------------------------------------------------------------------
# compiles the *.dsp files

for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    NAME=${f%.dsp}
    FNAME=libFaustPlugin_$NAME
    LIBNAME=$FNAME$EXT

    SRCDIR=$(dirname "$p")

    # create a temporary dir
    TDR=$(mktemp -d faust.XXXXXX)
    TMP=$TDR/$NAME
    mkdir "$TMP"

    # compile faust to c++
    faust -i -a $FAUSTLIB/unity/unityplugin.cpp $OPTIONS "$SRCDIR/$f" -o "$TMP/$NAME.cpp" || (echo "$f : Faust to C++ compilation failed in faust2unitywin"; exit 1)

    for BIT in $BITS; do
        #-----------------------------------------------------------------------
        # mingw crosscompiler should be installed ('mingw32' package on Ubuntu)
        # the exact prefix should be specified in the environment variable MINGW
        if [ $BIT = "32" ]; then
            FIN=$FNAME/x86
            MINGWPREFIX="i686-w64-mingw32-"
        elif [ $BIT = "64" ]; then
            FIN=$FNAME/x64
            MINGWPREFIX="x86_64-w64-mingw32-"
        fi

        # checks if final dir exists, if not creates i
        if [ ! -d "$FIN" ]; then
            mkdir -p "$FIN"
        fi

        CXX="${MINGWPREFIX}g++"
        (which "$CXX" >/dev/null) || (echo "MingW compiler $CXX not found. See -help for more info"; exit 1)

        # compile c++ to binary
        (
            cd "$TMP"
            $CXX $CXXFLAGS $PROCARCH $LIB $OMP -static-libstdc++ -static-libgcc -Dmydsp=$NAME -o $LIBNAME $NAME.cpp
        ) > /dev/null || (echo "$f : C++ to win$BIT library compilation failed in faust2unitywin"; exit 1)

        # moves binary to final dir
        mv "$TMP/$LIBNAME" "$FIN/$LIBNAME"
        echo "$f : w$BIT compilation completed"
    done


    # remove intermediary files
    rm -rf "$SRCDIR/$LIBNAME"
    rm -rf "$TDR"

done

