#! /bin/bash -e
#set -x

#####################################################################
#                                                                   #
#               Compiles Faust programs to supercollider            #
#               (c) Grame, 2010                                     #
#                                                                   #
#####################################################################

#-------------------------------------------------------------------
# Supercollider headers : path to the folder containing :
# plugin_interface/, common/ and server/ headers

# Possible path

. faustpath
. faustoptflags

CXXFLAGS+=" $MYGCCFLAGS"  # So that additional CXXFLAGS can be used
UNIVERSAL="0"

ARCHFILE=$FAUSTARCH/supercollider.cpp

#-------------------------------------------------------------------
# Need SuperCollider 'plugin_interface' include path to compile UGens:

SC0=$SUPERCOLLIDER_HEADERS
SC1="/usr/local/include/SuperCollider/include"
SC2="/usr/local/include/supercollider"
SC3="/usr/include/SuperCollider"
SC4="/usr/include/supercollider"
SC5="/usr/local/include/SuperCollider/"

# Check if directory exists
if [ -d $SC0/plugin_interface ]; then
    SC=$SC0
elif [ -d $SC1 ]; then
    SC=$SC1
elif [ -d $SC2 ]; then
    SC=$SC2
elif [ -d $SC3 ]; then
    SC=$SC3
elif [ -d $SC4 ]; then
    SC=$SC4
elif [ -d $SC5 ]; then
    SC=$SC5
else
    echo "Can't find SuperCollider headers, try to set the $SUPERCOLLIDER_HEADERS variable with the appropriate path"
    exit 1
fi

# Echo Using SC Headers in $SC

INCLUDE="-I$SC/plugin_interface/ -I$SC/common/ -I$SC/server/ -I$FAUSTINC"

if [ $# = 0 ]; then
    echo USAGE:
    echo "$0 [-d[ebug]] [-dm] [-sd] [-ks] [-sn] [-noprefix] [-universal] file1.dsp [file2.dsp ...]"
    exit 1
fi

#-------------------------------------------------------------------
# Analyze command arguments :
# faust options                 -> OPTIONS
# if -omp : -openmp or -fopenmp -> OPENMP
# if -d or -debug               -> F2SC_DEBUG
# if -dm                        -> F2SC_DEBUG_MES
# if -ks                        -> KEEP_SRC
# if -noprefix                  -> NO_FAUST_PREFIX
# if -sd or -synthdef           -> SYNTHDEF
# if -sn                        -> SUPERNOVA_FLAG
# if -universal                 -> build a universal (arm/intel) external
# existing *.dsp files          -> FILES
#

# PHASE 2 : dispatch command arguments
F2SC_DEBUG=0
F2SC_DEBUG_MES=0
SUPERNOVA_FLAG=0
KEEP_SRC=0
SYNTHDEF=""
NO_FAUST_PREFIX=0

for p in $@; do

    if [ "$p" = -debug ] || [ "$p" = -d ]; then
        F2SC_DEBUG=1
    elif [ "$p" = -dm ]; then
        F2SC_DEBUG_MES=1
    elif [ "$p" = -ks ]; then
        KEEP_SRC=1
    elif [ "$p" = -noprefix ]; then
        NO_FAUST_PREFIX=1
    elif [ "$p" = -sd ] || [ "$p" = -synthdef ]; then
        SYNTHDEF="--synthdef"
    elif [ "$p" = -sn ] || [ "$p" = -supernova ]; then
        SUPERNOVA_FLAG=1
    elif [ $p = "-universal" ]; then
        UNIVERSAL="1"
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi
done

if [ $F2SC_DEBUG = 1 ]; then
    OUTDEV=/dev/tty
else
    OUTDEV=/dev/null
fi

if [ $F2SC_DEBUG_MES = 1 ]; then
    echo "Activate debug messages in the generated plugin"
    DNDEBUG="-DF2SC_DEBUG_MES"
else
    DNDEBUG="-DNDEBUG"
fi

if [ "$FILES" = "" ]; then
    echo USAGE:
    echo "$0 [-d[ebug]] [-dm] [-sd] [-ks] [-sn] [-noprefix] [-universal] file1.dsp [file2.dsp ...]"
    exit 1
fi

#-------------------------------------------------------------------
# Check plateform specifics
#
if [[ $(uname) == Darwin || $CROSSTARGET == Darwin ]]; then
    EXT="scx"
    SCFLAGS="-DNO_LIBSNDFILE -DSC_DARWIN $DNDEBUG -bundle"
elif [[ $(uname) == Linux ]]; then
    EXT="so"
    SCFLAGS="-DNO_LIBSNDFILE -DSC_LINUX $DNDEBUG -shared -fPIC"
else
    echo "unsupported plateform"
    exit 1
fi

#-------------------------------------------------------------------
# Compile the *.dsp files
#
for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")

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

    # compile the .dsp file to c++ and xml
    faust -i -a $ARCHFILE -xml $OPTIONS "$SRCDIR/$f" -o "$TMP/${f%.dsp}.cpp" || exit
    mv "$SRCDIR/$f.xml" "$TMP/"

    # compile c++ to binary; --prefix should be same as in ../../examples/Makefile.sccompile
    (
        SC_FAUST_PREFIX="Faust"
        if [ $NO_FAUST_PREFIX = 1 ]; then
            SC_FAUST_PREFIX=""
        fi

        cd "$TMP"
        faust2sc --prefix="$SC_FAUST_PREFIX" $SYNTHDEF "$f.xml" > "${f%.dsp}.sc" 2>$OUTDEV
        BUILDFLAGS="$FAUSTTOOLSFLAGS $SCFLAGS -I$CUR $INCLUDE $CXXFLAGS $OMP -DSC_FAUST_PREFIX=\"$SC_FAUST_PREFIX\""

        # build for build scsynth
        # universal binaries produced on M1
        if [[ $(uname) == Darwin && ($(uname -p) = arm  || "$UNIVERSAL" = "1") ]]; then
            $CXX $BUILDFLAGS -arch x86_64 "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_x86_64.$EXT" 
            $CXX $BUILDFLAGS -arch arm64 "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_arm64.$EXT"
            $LIPO -create "${f%.dsp}_x86_64.$EXT" "${f%.dsp}_arm64.$EXT" -output "${f%.dsp}.$EXT" || exit
        else
            $CXX $BUILDFLAGS "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}.$EXT" 
        fi 
        # build for supernova
        if [ $SUPERNOVA_FLAG = 1 ]; then
            if [[ $(uname) == Darwin && ($(uname -p) = arm  || "$UNIVERSAL" = "1") ]]; then
                $CXX $BUILDFLAGS -arch x86_64 -DSUPERNOVA "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_supernova_x86_64.$EXT" 
                $CXX $BUILDFLAGS -arch arm64 -DSUPERNOVA "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_supernova_arm64.$EXT" 
                $LIPO -create "${f%.dsp}_supernova_x86_64.$EXT" "${f%.dsp}_supernova_arm64.$EXT" -output "${f%.dsp}_supernova.$EXT" || exit
            else
                $CXX $BUILDFLAGS -DSUPERNOVA "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_supernova.$EXT" 
            fi
        fi

    )> $OUTDEV || exit

    # move the produced files from tmp to source dir
    cp "$TMP/${f%.dsp}.$EXT" "$SRCDIR/${f%.dsp}.$EXT"
    if [ $SUPERNOVA_FLAG = 1 ]; then
        cp "$TMP/${f%.dsp}_supernova.$EXT" "$SRCDIR/${f%.dsp}_supernova.$EXT"
    fi
    cp "$TMP/${f%.dsp}.sc" "$SRCDIR/${f%.dsp}.sc"

    # Deactivated for now
    #if [[ $(uname) == Darwin || $CROSSTARGET == Darwin ]]; then
    #    codesign --sign - --deep --force "$SRCDIR/${f%.dsp}.$EXT"
    #    if [ $SUPERNOVA_FLAG = 1 ]; then
    #        codesign --sign - --deep --force "$SRCDIR/${f%.dsp}_supernova.$EXT"
    #    fi
    #fi

    if [ $KEEP_SRC = 1 ]; then
        cp "$TMP/${f%.dsp}.cpp" "$SRCDIR/${f%.dsp}.cpp"
    fi

    if [ $F2SC_DEBUG = 1 ]; then
        echo "Retaining intermediate products directory '$TDR'"
        OUTDEV=/dev/tty
    else
        rm -rf "$TDR"
    fi

    # collects all the files produced
    BINARIES="$SRCDIR/${f%.dsp}.$EXT;"
    if [ $SUPERNOVA_FLAG = 1 ]; then
        BINARIES="$BINARIES$SRCDIR/${f%.dsp}_supernova.$EXT;"
    fi
    BINARIES="$BINARIES$SRCDIR/${f%.dsp}.sc;"

done

# return the binaries names
echo "$BINARIES"
