# The folder containing this script
export TOOLS=`dirname $0`
export TRUNK=`cd $TOOLS/..; pwd`

# Global variables needed later
if [ "$MODE" == "" ]; then
	export MODE=debug
fi

if [ "$MODE" == "debug" ]; then
	export DEBUGGABLE_FLAG=true
	export DEBUG_FLAG=1
else
	export DEBUGGABLE_FLAG=false
	export DEBUG_FLAG=0
fi

export ANDROID_NDK=$TRUNK/sdks/android-ndk
export ANDROID_SDK=$TRUNK/sdks/android-sdk

# Local variables
export SRCROOT=$TRUNK
export JAVA_SDK=`/usr/libexec/java_home`
export JCOUNT=20
export PREBUILT_LIB_DIR=$SRCROOT/prebuilt/lib/android/armv6

# Find the appropriate folder for the platform-tools, now located in the build-tools
BUILD_TOOLS=${ANDROID_SDK}/build-tools/$(ls $ANDROID_SDK/build-tools | tail -n 1)

# Path variables
export CLASSPATH="$ANDROID_SDK/platforms/android-8/android.jar"
export NDKBUILD=$ANDROID_NDK/ndk-build
export DSTROOT=$SRCROOT/_build/android/$MODE
export RAWROOT=$SRCROOT/_build/android/$MODE/assets

export JAVAC=$JAVA_SDK/bin/javac
export JAR=$JAVA_SDK/bin/jar
export ADB=$ANDROID_SDK/platform-tools/adb

# Android SDK changed a bit from the version 20 and part of the tools can't be found in
# the platform-tools anymore
if [ -f "$ANDROID_SDK/platform-tools/aidl" ]; then
	export DX=$ANDROID_SDK/platforms/android-8/tools/dx
	export AAPT=$ANDROID_SDK/platforms/android-8/tools/aapt
	export APKBUILDER=$ANDROID_SDK/tools/apkbuilder
	export ZIPALIGN=$ANDROID_SDK/tools/zipalign
	export AIDL=$ANDROID_SDK/platform-tools/aidl
else
	# Android SDK 20+ has been installed
	# Find the appropriate folder for the platform-tools, now located in the build-tools
	BUILD_TOOLS=${ANDROID_SDK}/build-tools/$(ls $ANDROID_SDK/build-tools | tail -n 1)

	export DX=$BUILD_TOOLS/dx
	export AAPT=$BUILD_TOOLS/aapt
	export APKBUILDER="$ANDROID_SDK/tools/lib/sdklib.jar"
	export ZIPALIGN=$BUILD_TOOLS/zipalign
	export AIDL=$BUILD_TOOLS/aidl
fi

##########

# Update version info
#$TRUNK/prebuilt/bin/Revolution.osx -ui $TRUNK/engine/encode_version.rev $TRUNK/engine
cd $TRUNK/engine
$TRUNK/util/encode_version.pl $TRUNK/engine
cd $TRUNK

# Build the native code components
export NDK_PROJECT_PATH=$DSTROOT
echo "Building native code components..."
$NDKBUILD NDK_DEBUG=$DEBUG_FLAG NDK_APP_DEBUGGABLE=$DEBUGGABLE_FLAG NDK_APPLICATION_MK=$SRCROOT/Application.mk -j $JCOUNT -s
if [ $? != 0 ]; then
	exit $?
fi


export JAVASRC=$SRCROOT/engine/src/java

# generate the  interface classes from the aidl files

"$AIDL" -I"$JAVASRC" -p"$ANDROID_SDK/platforms/android-8/framework.aidl" "$JAVASRC/com/sec/android/iap/IAPServiceCallback.aidl"
if [ $? != 0 ]; then
	exit $?
fi

"$AIDL" -I"$JAVASRC" -p"$ANDROID_SDK/platforms/android-8/framework.aidl" "$JAVASRC/com/sec/android/iap/IAPConnector.aidl"
if [ $? != 0 ]; then
	exit $?
fi


# generate the IInAppBillingService interface class from the aidl file
"$AIDL" -I"JAVASRC" -p"$ANDROID_SDK/platforms/android-8/framework.aidl" "$JAVASRC/com/android/vending/billing/IInAppBillingService.aidl"
if [ $? != 0 ]; then
	exit $?
fi

# Compile the java classes
echo "Building java classes..."
mkdir -p "$DSTROOT/classes_livecode_community"
mkdir -p "$DSTROOT/classes_app"
"$JAVAC" -d "$DSTROOT/classes_livecode_community" -source 1.5 -target 1.5 -cp "$CLASSPATH" -sourcepath "$JAVASRC" "$JAVASRC/com/runrev/android/LiveCodeActivity.java"
if [ $? != 0 ]; then
	exit $?
fi

"$JAVAC" -d "$DSTROOT/classes_livecode_community" -source 1.5 -target 1.5 -cp "$CLASSPATH" -sourcepath "$JAVASRC" "$JAVASRC/com/runrev/android/EngineReceiver.java"
if [ $? != 0 ]; then
	exit $?
fi

"$JAVAC" -d "$DSTROOT/classes_livecode_community" -source 1.5 -target 1.5 -cp "$CLASSPATH" -sourcepath "$JAVASRC" "$JAVASRC/com/runrev/android/PushReceiver.java"
if [ $? != 0 ]; then
	exit $?
fi

"$JAVAC" -d "$DSTROOT/classes_livecode_community" -source 1.5 -target 1.5 -cp "$CLASSPATH" -sourcepath "$JAVASRC" "$JAVASRC/com/runrev/android/billing/google/GoogleBillingProvider.java"
if [ $? != 0 ]; then
	exit $?
fi

"$JAVAC" -d "$DSTROOT/classes_livecode_community" -source 1.5 -target 1.5 -cp "$CLASSPATH" -sourcepath "$JAVASRC" "$JAVASRC/com/runrev/android/billing/samsung/SamsungBillingProvider.java"
if [ $? != 0 ]; then
	exit $?
fi

# add LiveCode classes to jar
echo "Create Classes jar"
"$JAR" cf "$DSTROOT/LiveCode-Community.jar" -C "$DSTROOT/classes_livecode_community" .

##########
# 
# CUSTOM_DIR="$HOME/Documents/My LiveCode/Runtime/Android"
# CUSTOM_EXT_DIR="$HOME/Documents/My LiveCode/Externals"
# 
# mkdir -p "$CUSTOM_DIR"
# 
# cp $DSTROOT/libs/armeabi/librevandroid.so "$CUSTOM_DIR/Standalone"
# cp $DSTROOT/libs/armeabi/librevxml.so "$CUSTOM_DIR/RevXml"
# cp $DSTROOT/libs/armeabi/librevzip.so "$CUSTOM_DIR/RevZip"
# cp $DSTROOT/libs/armeabi/librevdb.so "$CUSTOM_DIR/RevDb"
# cp $DSTROOT/libs/armeabi/libdbsqlite.so "$CUSTOM_DIR/DbSqlite"
# cp $DSTROOT/libs/armeabi/libdbmysql.so "$CUSTOM_DIR/DbMysql"
# cp $DSTROOT/LiveCode.jar "$CUSTOM_DIR/Classes"
# cp $TRUNK/engine/rsrc/android-manifest.xml "$CUSTOM_DIR/Manifest.xml"
# cp $TRUNK/engine/rsrc/android-notify-icon.png "$CUSTOM_DIR/notify_icon.png"
# cp $TRUNK/engine/rsrc/android-inputcontrol.xml "$CUSTOM_DIR/livecode_inputcontrol.xml"
# 
# ##########

PKGDIR=$DSTROOT

cp $DSTROOT/libs/armeabi/librevandroid-community.so $PKGDIR/Standalone-Community
cp $DSTROOT/libs/armeabi/librevxml.so $PKGDIR/RevXml
cp $DSTROOT/libs/armeabi/librevzip.so $PKGDIR/RevZip
cp $DSTROOT/libs/armeabi/librevdb.so $PKGDIR/RevDb
cp $DSTROOT/libs/armeabi/libdbsqlite.so $PKGDIR/DbSqlite
cp $DSTROOT/libs/armeabi/libdbmysql.so $PKGDIR/DbMysql
cp $DSTROOT/libs/armeabi/librevsecurity.so $PKGDIR/RevSecurity
cp $DSTROOT/LiveCode-Community.jar $PKGDIR/Classes-Community
cp $TRUNK/engine/rsrc/android-manifest.xml $PKGDIR/Manifest.xml
cp $TRUNK/engine/rsrc/android-notify-icon.png "$PKGDIR/notify_icon.png"
cp $TRUNK/engine/rsrc/android-inputcontrol.xml "$PKGDIR/livecode_inputcontrol.xml"

##########
