#!/bin/bash -e

NDK=0
SDK=0
JDK=0
REBUILD=0
CLEAN=0
#DEBUG="NDK_DEBUG=0"
DEBUG="NDK_DEBUG=0 APP_OPTIM=release"
#default should be assembleRelease but not working with gradle 2, to fix
GRADLE_FLAG="assembleDebug"
VERB=""

#parse options
for i in $* ; do

#! Check for force rebuild
if [ "$i" = "-force_rebuild" ] ; then
  echo -e "\033[31m Usage: $0 Force Rebuild : Activated \033[0m"
  REBUILD=1
fi

if [ "$i" = "-clean" ]; then
  CLEAN=1
fi

if [ "$i" = "-debug" ]; then
  DEBUG="NDK_DEBUG=1"
  GRADLE_FLAG="assembleDebug"
fi

if [ "$i" = "-v" ]; then
  VERB="V=1"
fi

#! Check for the NDK path
if [ "${i%=*}" = "-ndk" ] ; then
  echo -e "\033[31m Usage: $0 Path to Android NDK : Ok \033[0m"
  NDK=1
  export PATH="$(echo $i | cut -d"=" -f2):$PATH"
fi

#! Check for the SDK path
if [ "${i%=*}" = "-sdk" ] ; then
  echo -e "\033[31m Usage: $0 Path to Android SDK : Ok \033[0m"
  SDK=1
  export PATH="$(echo $i | cut -d"=" -f2)/tools:$PATH"
  export ANDROID_HOME="$(echo $i | cut -d"=" -f2)/"
fi

#! Check for the JDK path
if [ "${i%=*}" = "-jdk" ] ; then
 echo -e "\033[31m Usage: $0 Path to JDK : Ok \033[0m"
 JDK=1
 export JAVA_HOME="$(echo $i | cut -d"=" -f2)"
fi

if [ "$i" = "-h" ]; then
echo "Usage: "
echo "-ndk=PATH: path to android NDK"
echo "-sdk=PATH: path to android SDK (for command-line build of APK)"
echo "-jdk=PATH: path to JDK (for command-line build of APK)"
echo "-force_rebuild: forces clean before build"
echo "-clean: remove objs and libs"
echo "-debug: build with debug symbols "
echo "-h: print this screen"
echo "-v: verbose build"
echo ""
echo -e "\n\033[32m If you have problem using this script, check https://wiki.gpac.io/Build/build/GPAC-Build-Guide-for-Android or gpac/build/android/README \033[0m\n"
exit 0
fi

done
#args


if [ $NDK -eq 0 ]
then
  echo "\nUsage: $0 ndk=PATH_TO_ANDROID_NDK [ADDITIONAL_NDK-BUILD arguments] : You must give as first argument the directory path of the NDK, add -ndk=PATH_TO_NDK"
  exit 1
fi

if [ $SDK -eq 0 ]
then
  echo -e "\n\033[34m You have not give the directory path of SDK, build of GPAC.apk not possible. Build it with Android Studio or add -sdk=PATH_TO_SDK\033[0m"
fi

if [ $JDK -eq 0 ]
then
  echo -e "\n\033[34m Usage: $0 PATH_TO_JDK : You have not give the directory path of JDK, build of GPAC.apk not possible. Build it with with Android Studio or add -jdk=PATH_TO_JDK \033[0m"
fi

if [ -z $JAVA_HOME ]
then
  echo "The JAVA_HOME environment variable must be set if you build without Android studio"
  echo "Please refer to https://github.com/gpac/gpac/commit/705e199c8eadf35774520206d7f7cf1a043c0539"

fi

if [ -z $ANDROID_HOME ]
then
  echo "The ANDROID_HOME environment variable must be set if you build without Android studio"
  echo "Please refer to https://github.com/gpac/gpac/commit/705e199c8eadf35774520206d7f7cf1a043c0539"

fi

echo -e "\n $PATH"

# Be sure to resolve if script not called from its directory
export BUILDPATH=$(dirname "$0")
cd $BUILDPATH
export BUILDPATH=$(pwd)
echo "Current Dir = $BUILDPATH"

echo "Building libgpac"
cd ../../../
BASEPATH=$(pwd)
#check revision.h is here
./check_revision.sh
version="`grep '#define GPAC_GIT_REVISION' \"./include/gpac/revision.h\" | cut -d '"' -f 2`"

#rm -rf $BUILDPATH/../obj/*

cd $BUILDPATH

if [ $CLEAN -eq 1 ]
then
echo -n "Cleaning all from $BUILDPATH"
ndk-build clean
cd $BASEPATH/applications/gpac_android/
./gradlew clean
cd -
exit 0
fi

echo -n "Buiding all libs from $BUILDPATH"
if [ $REBUILD -eq 1 ]
then
ndk-build clean
cd $BASEPATH/applications/gpac_android/
./gradlew clean
cd -
ndk-build -B $VERB $DEBUG
else
ndk-build $VERB $DEBUG
fi
echo "[OK] build successful."

cd ../libs/


LIBS_DIR="$BASEPATH/applications/gpac_android/src/main/libs"
mkdir -p $LIBS_DIR
echo "GPAC.apk: Copying libs to libs dir $LIBS_DIR ..."
for i in $(find . -name '*.so')
do
	echo -n " Copy $i"
	TMP_DIR=$(dirname $i)
	echo $TMP_DIR
	mkdir -p "$LIBS_DIR/$TMP_DIR"
	cp "$i" "$LIBS_DIR/$i"
done
echo
echo "Done."

LIBS_DIR="$BASEPATH/applications/gpac_android/src/main/libs"
mkdir -p $LIBS_DIR
cd "$BASEPATH/extra_lib/lib/android"
echo "GPAC.apk: Copying all extra libs to $LIBS_DIR ..."
for i in $(find . \( -name '*.so' \))
do
	echo " Copy $i"
	TMP_DIR=$(dirname $i)
	mkdir -p "$LIBS_DIR/$TMP_DIR"
	cp "$i" "$LIBS_DIR/$i"
done
echo "Done."

ASSETS_DIR="$BASEPATH/applications/gpac_android/src/main/assets/share"
mkdir -p $ASSETS_DIR

echo "GPAC.apk: Copying shared resources (GUI, scripts, shaders...) to $ASSETS_DIR ..."
cp -R "$BASEPATH/share/gui" $ASSETS_DIR
cp -R "$BASEPATH/share/shaders" $ASSETS_DIR
cp -R "$BASEPATH/share/scripts" $ASSETS_DIR
cp -R "$BASEPATH/share/res" $ASSETS_DIR
echo "Done."

if [ \( $SDK -eq 1 \) -a \( $JDK -eq 1 \) ]
then

	TAG="$(grep "\#define GPAC_VERSION " $BASEPATH/include/gpac/version.h | cut -d "\"" -f 2 )"

	echo "Building GPAC.apk"
	cd $BASEPATH/applications/gpac_android/
	./gradlew $GRADLE_FLAG
	cd -
	if [ -f $BASEPATH/gpac-$TAG-rev$version.apk ]
	then
	echo -e "\nOld GPAC.apk found. Removing it ..."
	rm $BASEPATH/gpac-$TAG-rev$version.apk
	fi

	cp $BASEPATH/applications/gpac_android/build/outputs/apk/gpac_android-debug.apk $BASEPATH/gpac-$TAG-rev$version.apk
	echo "[OK] success Building GPAC apk"
else
	echo "Run project with Android studio to generate APK"
fi
