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

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

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

# Possible path

. faustpath
. faustoptflags
. usage.sh

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

ARCHFILE=$FAUSTARCH/supercollider.cpp

SOUNDFILE="0"
SOUNDFILEDEFS=""
SOUNDFILEINC=""
SOUNDFILELIBS=""

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

#-------------------------------------------------------------------
# Analyze command arguments :
# faust options                 -> OPTIONS
# 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
# if -soundfile                 -> load soundfiles
# existing *.dsp files          -> FILES
#

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

echoHelp()
{
    usage faust2supercollider "[options] [Faust options] <file.dsp>"
    require SuperCollider includes
    echo "Compiles Faust programs to SuperCollider UGens. On macOS, soundfiles will be searched in 'xx/Library/Application Support/SuperCollider/Extensions/' and 'xx/Library/Application Support/SuperCollider/Extensions/FaustSounds'"
    option
    option "-d[ebug]" "to activate debug mode"
    option "-dm" "to activate debug message mode"
    option "-sd" "activate the --synthdef option in faust2sc"
    option "-ks" "to keep the UGen source"
    option "-sn" "to compile for SuperNova"
    option "-noprefix" "to remove the standard 'Faust' prefix"
    options -soundfile
    option -universal "to generate a universal (arm/intel) UGens"
    option "Faust options"
    exit
}


if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

for p in $@; do

   if [ $p = "-help" ] || [ $p = "-h" ]; then
       echoHelp
    elif [ "$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
        SC_FAUST_PREFIX=""
    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 = "-soundfile" ]; then
        SOUNDFILE="1"
        SOUNDFILEDEFS="-DSOUNDFILE `pkg-config --cflags sndfile`"
    elif [ $p = "-soundfile_wav" ]; then
        SOUNDFILE="1"
        SOUNDFILEDEFS="-DSOUNDFILE -DSUPERCOLLIDER"
    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 [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

#-------------------------------------------------------------------
# Check Darwin specifics
#
if [[ $(uname) == Darwin ]]; then
    # Use -undefined dynamic_lookup flag for libsndfile (part of the SC runtime) weak linking
    ARCHLIB+=" -framework CoreFoundation -framework CoreServices -undefined dynamic_lookup"
else
    ARCHLIB+=""
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
    (
        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 $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_x86_64.$EXT" 
            $CXX $BUILDFLAGS -arch arm64 $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB "-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 $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB $IGNORE_FLAGS "-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 $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB "-Dmydsp=${f%.dsp}" "${f%.dsp}.cpp" -o "${f%.dsp}_supernova_x86_64.$EXT" 
                $CXX $BUILDFLAGS -arch arm64 -DSUPERNOVA $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB "-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 $SOUNDFILEDEFS $SOUNDFILELIBS $ARCHLIB "-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"
