#!/bin/bash

#####################################################################
#                                                                   #
#               Compiles Faust programs to iOS applications         #
#               (c) Grame, 2012-2013                                #
#                                                                   #
#####################################################################

. faustpath

#PHASE 1 : collects files and options from the command line

for p in $@; do
    if [ "$p" = -omp ]; then
        if [[ $CXX == "icpc" ]]; then
            OMP="-openmp"
        else
            OMP="-fopenmp"
        fi
    fi

    if [ "$p" = -icc ]; then
        ignore=" "
    elif [ $p = "-osc" ]; then
        OSCDEFS="-DOSCCTRL -lOSCFaust"
    elif [ "$p" = "-httpd" ]; then
        HTTPDEFS="-DHTTPCTRL -lHTTPDFaust -lmicrohttpd"
    elif [ "$p" = "-jack" ]; then
        JACK="1"
    elif [ "$p" = "-xcode" ]; then
        XCODE="1"
    elif [ "$p" = "-all" ]; then
        ALL="1"
    elif [ "$p" = "-noagc" ]; then
        NOAGC="1"
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi
done


#PHASE 2 : compile all files

for p in $FILES; do
	S=$(dirname "$p")
	F=$(basename "$p")
	P=${F%.dsp}

	T=$(mktemp -d faust.XXX)
	cp -r /usr/local/lib/faust/iOS/* $T
	cp -r /usr/local/include/faust $T

	if [ "$JACK" = "1" ]; then
	   #echo "Compile with JACK/CoreAudio support"
	   faust -i $OPTIONS -a ios-coreaudio-jack.cpp "$p" -o "$T/ios-faust.h"
	   (
		xcodebuild -project "$T/iOS.xcodeproj" -target Template_Jack PRODUCT_NAME=$P
        cd "$T" && xcodebuild -scheme Template_Jack archive PRODUCT_NAME=$P
	   ) > /dev/null
    elif [ "$NOAGC" = "1" ]; then
        if [ "$ALL" = "1" ]; then
            echo "Compile with CoreAudio support in 64/32 bits and no AGC"
            faust -i $OPTIONS -a ios-coreaudio.cpp "$p" -o "$T/ios-faust.h"
            (
                xcodebuild GCC_PREPROCESSOR_DEFINITIONS='${inherited} DISABLE_AGC=1' -project "$T/iOS.xcodeproj" -target Template_CoreAudio PRODUCT_NAME=$P
                cd "$T" && xcodebuild -scheme Template_CoreAudio archive PRODUCT_NAME=$P
            ) > /dev/null
        else
            echo "Compile with CoreAudio support in 32 bits and no AGC"
            faust -i $OPTIONS -a ios-coreaudio.cpp "$p" -o "$T/ios-faust.h"
            (
                xcodebuild GCC_PREPROCESSOR_DEFINITIONS='${inherited} DISABLE_AGC=1' -project "$T/iOS.xcodeproj" -target Template_CoreAudio_32bits PRODUCT_NAME=$P
                cd "$T" && xcodebuild -scheme Template_CoreAudio_32bits archive PRODUCT_NAME=$P
            ) > /dev/null
        fi
    else
        if [ "$ALL" = "1" ]; then
            echo "Compile with CoreAudio support in 64/32 bits and AGC"
            faust -i $OPTIONS -a ios-coreaudio.cpp "$p" -o "$T/ios-faust.h"
            (
                xcodebuild  -project "$T/iOS.xcodeproj" -target Template_CoreAudio PRODUCT_NAME=$P
                cd "$T" && xcodebuild -scheme Template_CoreAudio archive PRODUCT_NAME=$P
            ) > /dev/null
        else
            echo "Compile with CoreAudio support in 32 bits and AGC"
            faust -i $OPTIONS -a ios-coreaudio.cpp "$p" -o "$T/ios-faust.h"
            (
                xcodebuild -project "$T/iOS.xcodeproj" -target Template_CoreAudio_32bits PRODUCT_NAME=$P
                cd "$T" && xcodebuild -scheme Template_CoreAudio_32bits archive PRODUCT_NAME=$P
            ) > /dev/null
        fi
	fi
    
    # move generated app to source directory
    rm -rf "$S/$P.app"
    mv "$T/build/Release-iphoneos/$P.app" "$S/"
    if [ "$XCODE" != "1" ]; then
        rm -rf "$T"
    else
        echo "Keep Xcode project"
    fi
        
	# collect binary file name for FaustGIDE
    BINARIES="$BINARIES$S/$P.app;"
done

echo $BINARIES
