#!/bin/bash

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

# 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 options
INSTALL=0
SOURCE=0
SWIG=0
FAUST=0
KEYBOARD=0

# PHASE 2 : dispatch command arguments
for p in $@; do
	if [ $p = "-swig" ]; then
		SWIG=1
	fi
	if [[ -f "$p" ]]; then
	    FILES="$FILES $p"
	elif [ $p = "-install" ]; then
		INSTALL=1
	elif [ $p = "-source" ]; then
		SOURCE=1
	elif [ $p = "-faust" ]; then
		FAUST=1
	elif [ $p = "-keyboard" ]; then
		KEYBOARD=1
	elif [ $p = "-debug" ]; then
		LOG="/dev/stdout"
	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 "-debug: verbose output."
		exit 1
	else
	    OPTIONS="$OPTIONS $p"        
	fi
done

# only carry out the faust compilation 
if [ $FAUST -eq 1 ]; then
	for f in $FILES; do
		faust -i -a android.cpp "$f" -o "app/src/main/jni/dsp_faust.cpp"
	done
	exit 1
fi
 
# create the temporary directory where compilation will take place
TMPFILES=$(mktemp -d faust.XXX)
cp -r /usr/lib64/faust/android/*  $TMPFILES

APPFOLDER="$TMPFILES/app/src/main"
JNIFOLDER="$APPFOLDER/jni"

# Faust compilation (for now only one file should be specified)
for f in $FILES; do
	faust -i -a android.cpp "$f" -o "$JNIFOLDER/dsp_faust.cpp"
done

APPNAME=$(basename "$f")
APPNAME="${APPNAME%.dsp}"

# Copy include files *.h if any (ignore any error here)
(cp *.h $JNIFOLDER 2> $LOG) || true

#cd $TMPFILES 

#************************************************************************
# CUSTOMIZING THE APP'S MAIN PACKAGE IN FUNCTION OF THE DSP FILE NAME
#************************************************************************

PLATFORM=$(uname)

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

mv $APPFOLDER/java/com/grame/faust $APPFOLDER/java/com/grame/$APPNAME

# *************
# COMPILATION
# *************

# TODO wrong: should be checked
if [ $SWIG -eq 1 ]; then
	rm -rf $APPFOLDER/java/com/grame/dsp_faust || true
	mkdir -p $APPFOLDER/java/com/grame/dsp_faust
	swig -java -package com.grame.dsp_faust -includeall -verbose -outdir $APPFOLDER/java/com/grame/dsp_faust -c++ -I/usr/local/include -I/System/Library/Frameworks/JavaVM.framework/Headers -I$JNIFOLDER -o $JNIFOLDER/java_interface_wrap.cpp $TMPFILES/dsp_faust_interface.i
fi

cd $TMPFILES
./gradlew assembleRelease > $LOG
cd ..

cp -r $TMPFILES/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 $TMPFILES faustApp
	echo "An Android studio project named faustApp was created."
else
	rm -rf $TMPFILES
fi

echo "$APPNAME.apk;"


