#!/bin/bash

#change this to valid ndk path, otherwise it will be downloaded
NDK=/bad/path/to/android-ndk-r11c/
BUILD_PATH=$(pwd)/build_android
#if you have cjdns android app somewhere else then change it
NDK_VERSION="android-ndk-r11c"

case $(uname -s) in
    Darwin)
        TYPE=darwin
        ;;
    Linux)
        TYPE=linux
        ;;
    *)
        TYPE=
        ;;
esac

cpu_arch="$(uname -m)"
[[ -z "$cpu_arch" ]] && {
    echo "ERROR: NO CPU ARCHITECTURE DETECTED"
    exit 1
}
[[ "$cpu_arch" = "i686" ]] \
    && cpu_arch="x86"

android_log=android_build_$$.log
enabled_log=${LOG}

mkdir $BUILD_PATH

if [ "$NDK" == "/bad/path/to/android-ndk-r11c/" ]; then
  if [ ! -d $BUILD_PATH/$NDK_VERSION/ ]; then
    echo "NDK path is not specified. Downloading it..."

    NDK=$BUILD_PATH/$NDK_VERSION/

    ##SETUP NDK
    cd "$BUILD_PATH"
    if [ ! -d "$NDK" ]; then
          echo "$NDK_VERSION-${TYPE}-${cpu_arch}.tar.bz2"
          [[ -f "$NDK_VERSION-${TYPE}-${cpu_arch}.tar.bz2" ]] \
              || wget "https://dl.google.com/android/repository/$NDK_VERSION-${TYPE}-${cpu_arch}.zip" \
              || (echo "Can't find download for your system"; exit 1)
          [[ -d "$NDK_VERSION" ]] || (unzip -q "$NDK_VERSION-${TYPE}-${cpu_arch}.zip" || exit 1)
    fi

    [[ ! -d "$NDK" ]] && {
        echo "The NDK variable is not pointing to a valid directory"
        exit 1
    }
    cd ..
  else
    NDK=$BUILD_PATH/$NDK_VERSION/
  fi
fi

Seccomp_NO=1
mkdir $(pwd)/build_android/out

declare -a APP_ABI=("armeabi-v7a" "arm64-v8a" "x86")
declare -a APP_ABI2=("arm" "aarch64" "i686") 
declare -a TARGET_ARCH=("arm" "aarch64" "x64")
declare -a EABI=("androideabi" "android" "android")
declare -a PLATFORM=(16 21 16)
declare -a TOOLCHAIN=("arm-linux-androideabi-4.9" "aarch64-linux-android-4.9" "x86-4.9") 

for i in {0..2}
do
    GCC=$BUILD_PATH/${APP_ABI2[$i]}-${TYPE}-${EABI[$i]}/bin/${APP_ABI2[$i]}-linux-${EABI[$i]}-gcc
    [[ ! -x "$GCC" ]] && {
        $NDK/build/tools/make-standalone-toolchain.sh --platform=android-${PLATFORM[$i]} --toolchain=${TOOLCHAIN[$i]} --install-dir=$BUILD_PATH/${APP_ABI2[$i]}-${TYPE}-${EABI[$i]}/ \
        || exit 1
    }

    mkdir $(pwd)/build_android/out/${APP_ABI[$i]}
    rm -rf build_linux

    export PLATFORM=android
    export SYSTEM=linux

    export CROSS_COMPILE=$BUILD_PATH/${APP_ABI2[$i]}-${TYPE}-${EABI[$i]}/bin/${APP_ABI2[$i]}-linux-${EABI[$i]}-
    export CROSS=${CROSS_COMPILE}
    export CC=${CROSS}gcc
    export AR=${CROSS}ar
    export RANLIB=${CROSS}ranlib
    export CFLAGS=${CROSS_CFLAGS}
    export LDFLAGS=${CROSS_LDFLAGS}

    gcc_version=$(${CC} --version)
    echo Using $gcc_version
    echo Compiler CC: $CC
    echo Compiler CFLAGS: $CFLAGS
    echo Compiler LDFLAGS: $LDFLAGS
    time ./do
    cp cjdroute $(pwd)/build_android/out/${APP_ABI[$i]}/ || ret=$?

    if [ "$ret" != "" ] && [ "$ret" != "0" ]; then
      echo -e "\e[1;31mCopying ${APP_ABI[$i]} binary failed, non zero status returned - $ret\e[0m"
      exit 1
    else
      echo -e "\e[1;32mCopied ${APP_ABI[$i]} successfully\e[0m"
    fi

    rm cjdroute
done

echo -e "\n\e[1;34mOutput:\e[0m"
for i in {0..2}
do
    echo -e   "\e[1;34m        $(pwd)/build_android/out/${APP_ABI[$i]}/cjdroute\e[0m"
done

exit $ret
