#! /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

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"
	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] 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
# existing *.dsp files          -> FILES
#

# PHASE 1 : Look for -icc option to force use of intel icc (actually icpc)
# without having to configure CXX and CXXFLAGS
for p in $@; do
	if [ "$p" = -icc ]; then
		CXX=icpc
		CXXFLAGS='-O3 -xT -ftz -fno-alias -fp-model fast=2'
    fi
done

#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: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] 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="-O3 $FAUSTTOOLSFLAGS $SCFLAGS -I$CUR $INCLUDE $CXXFLAGS $OMP -DSC_FAUST_PREFIX=\"$SC_FAUST_PREFIX\""
        
        #build for build scsynth
        $CXX $BUILDFLAGS -Dmydsp=${f%.dsp} -o ${f%.dsp}.$EXT ${f%.dsp}.cpp
        
        #build for supernova
        if [ $SUPERNOVA_FLAG = 1 ]; then
        	$CXX $BUILDFLAGS -DSUPERNOVA -Dmydsp=${f%.dsp} -o ${f%.dsp}_supernova.$EXT ${f%.dsp}.cpp
        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"

    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"
