#!/usr/bin/env bash

set -eu

function help() {
    cat <<EOF
usage: install-android-ndk-sdk [options]

Examples
  ./install-android-ndk-sdk.sh --ndk_version r19b --sdk_version 28.0.3 --sdk_api_level android-28

Options
  --directory <directory>
        Root directory where to install the components.
        Maps to C:/Android on Windows, /opt/Android on Linux, /usr/local/Android on Mac
        by default.

  --target <target-platform>
        The desired target platform. Can be one of desktop, android, ios.
        The default value is desktop.

  --ndk_version <version>
        The desired ndk version
  --sdk_version <version>
        The desired sdk version
  --sdk_api_level <level>
        The desired sdk api level

EOF
}

NDK_VERSION="r20"
SDK_VERSION="29.0.0"
SDK_API_LEVEL="android-29"

OS_NAME=linux

case "$OSTYPE" in
    *linux*)
        INSTALL_DIR=/home/raphael/AndroidTest
        OS_NAME=linux
        ;;
    *darwin*)
        INSTALL_DIR=/usr/local/Android
        OS_NAME=darwin
        ;;
    msys)
        INSTALL_DIR=/c/Android
        OS_NAME=windows
        ;;
    *)
        ;;
esac

echo ${INSTALL_DIR}
SDK_TOOLS_FILE=sdk-tools-${OS_NAME}-4333796.zip
NDK_FILE=android-ndk-${NDK_VERSION}-${OS_NAME}-x86_64.zip

while [ $# -gt 0 ]; do
    case "$1" in
        --directory|-d)
            INSTALL_DIR="$2"
            shift
            ;;
        --ndk_version)
            NDK_VERSION="$2"
            shift
            ;;
        --sdk_version)
            SDK_VERSION="$2"
            shift
            ;;
        --sdk_api_level)
            SDK_API_LEVEL="$2"
            shift
            ;;
        --help|-h)
            help
            exit 0
            ;;
    esac
    shift
done

if [ -z "${INSTALL_DIR}" ]; then
    echo "No --directory specified or auto-detection failed." >&2
    exit 1
fi

mkdir -p ${INSTALL_DIR}

DOWNLOAD_DIR=`mktemp -d 2>/dev/null || mktemp -d -t 'install-android-ndk-sdk'`

REPOSITORY=https://dl.google.com/android/repository

echo "Downloading sdk tools..." >&2
curl --progress-bar -L -o ${DOWNLOAD_DIR}/sdk_tools.zip ${REPOSITORY}/${SDK_TOOLS_FILE} >&2
unzip -qq ${DOWNLOAD_DIR}/sdk_tools.zip -d ${INSTALL_DIR}
rm -f ${DOWNLOAD_DIR}/sdk_tools.zip

echo "Downloading ndk..." >&2
curl --progress-bar -L -o ${DOWNLOAD_DIR}/ndk.zip ${REPOSITORY}/${NDK_FILE} >&2
unzip -qq ${DOWNLOAD_DIR}/ndk.zip -d ${INSTALL_DIR}
rm -f ${DOWNLOAD_DIR}/ndk.zip

echo "Configuring environment"
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin

echo "Installing SDK packages"
cd ${INSTALL_DIR}/tools/bin
echo "y" | ./sdkmanager "platforms;${SDK_API_LEVEL}" "platform-tools" "build-tools;${SDK_VERSION}" >> sdkmanager.log
#echo "y" | ./sdkmanager --install "emulator" >> sdkmanager.log
#echo "y" | ./sdkmanager --install "system-images;android-21;google_apis;x86" >> sdkmanager.log
#echo "no" | ./avdmanager create avd -n x86emulator -k "system-images;android-21;google_apis;x86" -c 2048M -f >> sdkmanager.log

echo "Provisioniong complete. Here's the list of packages and avd devices:"
./sdkmanager --list
./avdmanager list avd

