#!/bin/bash

#   ***************************************************************************
#     config.conf - configuration template for android QGIS
#      --------------------------------------
#      Date                 : 01-Jun-2011
#      Copyright            : (C) 2011 by Marco Bernasocchi
#      Email                : marco at bernawebdesign.ch
#   ***************************************************************************
#   *                                                                         *
#   *   This program is free software; you can redistribute it and/or modify  *
#   *   it under the terms of the GNU General Public License as published by  *
#   *   the Free Software Foundation; either version 2 of the License, or     *
#   *   (at your option) any later version.                                   *
#   *                                                                         *
#   ***************************************************************************/

echo ""
set -e
#######check if config was already loaded#######
if [ ! -n "${QGIS_ANDROID_CONF_LOADED+x}" ]; then
  #############################
  #######CONFIGURE HERE########
  #############################
  export ROOT_DIR=$HOME/dev/grass/android
  
  #Make sure we are having the latest SVN
  cd ../../..
  svn co https://svn.osgeo.org/grass/grass/trunk grass
  cd $ROOT_DIR/scripts

  #WARNING: armeabi-v7a builds usually don't work on android emulators
  DEFAULT_ANDROID_TARGET_ARCH=armeabi #or armeabi-v7a
  #passing ANDROID_TARGET_ARCH=armeabi-v7a when calling a script will override the default
  #for example ANDROID_TARGET_ARCH=armeabi-v7a ./build-all.sh
  
  DEFAULT_BUILD_TYPE=Debug #or Release
  #passing BUILD_TYPE=Release when calling a script will override the default
  #for example BUILD_TYPE=Release ./build-all.sh
  
#  export ANDROID_SDK_ROOT=$ROOT_DIR/android-sdk
  export ANDROID_NDK_ROOT=$ROOT_DIR/android-ndk-r9d
  

  ######LIKELY UNNEEDED TO CONFIGURE FURTHER######
  if [ ! -n "${ANDROID_TARGET_ARCH+x}" ]; then
    export ANDROID_TARGET_ARCH=$DEFAULT_ANDROID_TARGET_ARCH
  fi
  if [ ! -n "${BUILD_TYPE+x}" ]; then
    export BUILD_TYPE=$DEFAULT_BUILD_TYPE
  fi
  
  export INSTALL_DIR=$ROOT_DIR/out/$ANDROID_TARGET_ARCH
  export SCRIPT_DIR=$ROOT_DIR/scripts
  export PATCH_DIR=$ROOT_DIR/patches
  export SRC_DIR=$ROOT_DIR/src
  export TMP_DIR=$ROOT_DIR/tmp
  export ANDROID_LEVEL=9
  export ANDROID_NDK_PLATFORM=android-$ANDROID_LEVEL
  export ANDROID_NDK_HOST=linux-x86
  export ANDROID_NDK_TOOLCHAIN_PREFIX=arm-linux-androideabi
  export ANDROID_NDK_TOOLCHAIN_VERSION=9
  export ANDROID_NDK_TOOLCHAIN_ROOT=$ROOT_DIR/$ANDROID_NDK_PLATFORM-standalonetoolchain
  export APK_DIR=$ROOT_DIR/apk
  
  #setting compilers names
  #see https://groups.google.com/forum/?hl=en#!topic/android-ndk/DVNS_mQHxdA
  export PATH=$ANDROID_NDK_TOOLCHAIN_ROOT/bin:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH
  export CC=arm-linux-androideabi-gcc
  export CXX=arm-linux-androideabi-g++
  export LD=arm-linux-androideabi-ld
  export AR=arm-linux-androideabi-ar
  export RANLIB=arm-linux-androideabi-ranlib
  export AS=arm-linux-androideabi-as

  export MY_STD_CONFIGURE_FLAGS="--prefix=$INSTALL_DIR --host=arm-linux-androideabi"
  #FPU (floating point unit)FLAG:
  #see http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/ARM-Options.html
  #see http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0408f/index.html
  #see NDK docs/STANDALONE-TOOLCHAIN.html
  #for generic MY_FPU=vfp
  MY_FPU=vfp
  #for tegra processors (xoom, transformer, iconia, galaxy tab)
  MY_FPU=vfpv3-d16
  #for hummingbird and neon supporting processors (galaxy phone) -mfpu=neon
  #MY_FPU=neon
  ARMV7A_flags=" -march=armv7-a -mfloat-abi=softfp -mfpu=$MY_FPU"

  #TODO check ALL_FLAGS if other flags re needed
  #ALL_FLAGS="-c -Wno-psabi -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -DANDROID -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wa,--noexecstack -O2 -Os -g -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -D_REENTRANT -Wall -Wno-psabi -W -fPIC"
  #export MY_STD_CFLAGS="$MY_STD_CFLAGS" #-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -D__ARM_ARCH_7-A__"
  export MY_STD_CFLAGS="-DANDROID=ON -Wno-psabi -O2 -mthumb"
  if [ "$ANDROID_TARGET_ARCH" = "armeabi-v7a" ]; then
    export MY_STD_CFLAGS=$MY_STD_CFLAGS$ARMV7A_flags
  fi
  export MY_STD_CXXFLAGS="$MY_STD_CFLAGS -std=gnu++0x"
  export MY_STD_LDFLAGS="-Wl,--fix-cortex-a8"

  #############################
  #######END CONFIGURE#########
  #############################

  #######detect cpu cores######
  CORES=$(cat /proc/cpuinfo | grep processor | wc -l)
  export CORES=$(($CORES+1))
  #######mark conf as loaded#######
  export QGIS_ANDROID_CONF_LOADED=1
fi
