#! /bin/bash -e

#############################################################################################################
#                                                                                                           #
#   Compiles Faust programs to Linux x86_64 library 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 "faust2linuxunity <file1.dsp> [<file2.dsp>]"
        echo "Create Linux shared library for faust unity plugin."
        echo "Use 'faust2unity -linux' to also create the c# files"
        echo "See architecture/unity/README.md for more info"
        exit
    fi

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

#------------------------------------------------------------------------------
# Compiler g++
#

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

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

if [[ $(uname) == Darwin ]]; then
    MARCH=""
else
    MARCH="-march=native"
fi

CXXFLAGS="-O3 -fPIC $MARCH -mfpmath=sse -msse -msse2 -msse3 -ffast-math -ftree-vectorize"
LIB="-shared"
EXT=".so"

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

for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    NAME=${f%.dsp}
    FNAME=FaustPlugin_$NAME
    LIBNAME=lib$FNAME$EXT
    SRCDIR=$(dirname "$p")
    FIN=$FNAME/Linux

    # creates a temporary dir 
    TDR=$(mktemp -d faust.XXXXXX)
    TMP=$TDR/${f%.dsp}
    mkdir "$TMP"

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

    # 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 faust2linuxunity"; exit 1)

    # compile c++ to binary
    (
        cd "$TMP"
		$CXX $CXXFLAGS $LIB -Dmydsp=$NAME -o $LIBNAME $NAME.cpp

    ) > /dev/null || (echo "$f : C++ to linux library compilation failed in Faust2linuxunity"; exit 1)

    # moves binary to final dir
    rm -rf "$SRCDIR/$LIBNAME"
    mv "$TMP/$LIBNAME" "$FIN/$LIBNAME"
    rm -rf "$TDR"

    echo "$f : linux compilation completed"
done



