#!/bin/bash

#####################################################################
#                                                                   #
#               Compile a Faust program to an android app           #
#               (c) Romain Michon CCRMA and Grame, 2014             #
#               (c) Yann Orlarey Grame, 2015             			#
#                                                                   #
#####################################################################

. faustpath
. faustoptflags

# change if you want to get the log of what's happening
LOG="/dev/null"

# exit if a command fails
set -e

# Global variables for file and options
FILE=
INSTALL=0
SOURCE=0
SWIG=0
FAUST=0
KEYBOARD=0
REUSE=0

# PHASE 2 : dispatch command arguments
for p in $@; do
	if [ $p = "-swig" ]; then
		SWIG=1
	fi
	if [[ -f "$p" ]]; then
	    FILE="$p"
	elif [ $p = "-install" ]; then
		INSTALL=1
	elif [ $p = "-source" ]; then
		SOURCE=1
	elif [ $p = "-faust" ]; then
		FAUST=1
	elif [ $p = "-reuse" ]; then
		REUSE=1
	elif [ $p = "-keyboard" ]; then
		KEYBOARD=1
	elif [ $p = "-debug" ]; then
		LOG="/dev/stdout"
    elif [ "$p" = "-noagc" ]; then
        NOAGC="1"
	elif [ $p = "-h" ]; then
		echo "Usage: faust2android faustFile.dsp"
		echo "OPTIONS:"
		echo "-install: once compilation is over, installs the generated app on the Android device connected to the computer."
		echo "-source: creates an eclipse project of the app in the current directory."
		echo "Any other options are considered as Faust options. To get a list of the Faust options type: faust -h."
		echo "-swig: regenerate the C++ and the JAVA interface for the native portion of the app."
		echo "-faust: only carries out the Faust compilation and install the generated C++ file in the JNI folder."
		echo "-reuse: keep build directory and reuse it to speedup compilation."
		echo "-debug: verbose output."
		exit 1
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
	else
	    OPTIONS="$OPTIONS $p"        
	fi
done

# only carry out the faust compilation 
if [ $FAUST -eq 1 ]; then
	faust -i -a api/android/DspFaust.cpp $OPTIONS "$FILE" -o "app/src/main/cpp/DspFaust.cpp"
	exit 1
fi
 
# Create the temporary directory where compilation will take place

APPNAME=$(basename "$FILE")
APPNAME="${APPNAME%.dsp}"
BUILDDIR="faustandro.$APPNAME"
APPFOLDER="$BUILDDIR/app/src/main"
JNIFOLDER="$APPFOLDER/cpp"

if [ $REUSE -eq 0 ]; then
	if [ -d "$BUILDDIR" ]; then
		echo "Delete existing Android project $BUILDDIR" > $LOG
		rm -rf "$BUILDDIR"
	fi
fi
		
if [ ! -d "$BUILDDIR" ]; then
  	echo "Creating new Android project $BUILDDIR"  > $LOG
  	
  	mkdir -p "$BUILDDIR"
	cp -r $FAUSTLIB/android/*  "$BUILDDIR"
	install -d $JNIFOLDER
	# Copy include files *.h if any (ignore any error here)
	(cp *.h $JNIFOLDER 2> $LOG) || true
	
	# change 'faust' with real *APPNAME
	PLATFORM=$(uname)

	if [ $PLATFORM = "Darwin" ]; then
		sed -i '' 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle
        if [ "$NOAGC" = "1" ]; then
            sed -i '' 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle
        else
            sed -i '' 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle
        fi
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/*
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/*
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml
		sed -i '' 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/*
		sed -i '' 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml
	else
		sed -i 's,com.faust,com.'$APPNAME',g' $BUILDDIR/app/build.gradle
        if [ "$NOAGC" = "1" ]; then
            sed -i 's,-DDISABLE_AGC,'-DDISABLE_AGC',g' $BUILDDIR/app/build.gradle
        else
            sed -i 's,-DDISABLE_AGC,'',g' $BUILDDIR/app/build.gradle
        fi
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/faust/*
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/java/com/triggertrap/seekarc/*
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/AndroidManifest.xml
		sed -i 's,com.faust,com.'$APPNAME',g' $APPFOLDER/res/layout/*
		sed -i 's,1,'$APPNAME',g' $APPFOLDER/res/values/strings.xml
	fi
	
	mv $APPFOLDER/java/com/faust $APPFOLDER/java/com/$APPNAME

	# TODO wrong: should be checked
	if [ $SWIG -eq 1 ]; then
		rm -rf $APPFOLDER/java/com/dsp_faust || true
		mkdir -p $APPFOLDER/java/com/dsp_faust
		swig -java -package com.dsp_faust -includeall -verbose -outdir $APPFOLDER/java/com/dsp_faust -c++ -I$FAUSTINC -I/System/Library/Frameworks/JavaVM.framework/Headers -I$JNIFOLDER -o $JNIFOLDER/java_interface_wrap.cpp $BUILDDIR/dsp_faust_interface.i
	fi
else
	echo "Reusing existing Android project $BUILDDIR" > $LOG
fi

# Copying the Faust API files in the project
cp $FAUSTLIB/api/android/DspFaust.h $JNIFOLDER
cp $FAUSTLIB/api/android/jni/java_interface_wrap.cpp $JNIFOLDER
mkdir $APPFOLDER/java/com/DspFaust
cp $FAUSTLIB/api/android/jni/*.java $APPFOLDER/java/com/DspFaust

# Compile the Faust code for the NDK
faust -i -a api/android/DspFaust.cpp $OPTIONS "$FILE" -o "$JNIFOLDER/DspFaust.cpp" || exit


# Run Gradle
cd $BUILDDIR
./gradlew assembleRelease > $LOG
cd ..

cp -r $BUILDDIR/app/build/outputs/apk/app-release.apk $APPNAME.apk

# ****************
# TREAT OPTIONS
# ****************

if [ $INSTALL -eq 1 ]; then
	adb install -r $APPNAME.apk
fi

if [ $SOURCE -eq 1 ]; then
	rm -rf faustApp
	mv $BUILDDIR faustApp
	echo "An Android studio project named faustApp was created." > $LOG
else
	if [ $REUSE -eq 0 ]; then
		echo "Delete Android project $BUILDDIR" > $LOG
		rm -rf $BUILDDIR
	fi
fi

echo "$APPNAME.apk;"


