#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

set -o errexit -o nounset
set -o pipefail

ANDROID_HOME=/opt/android-sdk-linux
ASDKTOOLS_HOME=/opt/android-sdk-tools
ASDKTOOLS_VERSION=9477386_latest
ASDKTOOLS_SHA256=bd1aa17c7ef10066949c88dc6c9c8d536be27f992a1f3b5a584f9bd2ba5646a0

ANDROID_NDK_VERSION=21.3.6528147
CMAKE_VERSION=3.6.4111459
BUILD_TOOLS_VERSION=27.0.3
ANDROID_PLATFORM=27
ANDROID_NDK_MAJOR=21
ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION

if [ $# -gt 0 ] ; then
  ANDROID_NDK_VERSION=$1
  ANDROID_NDK_MAJOR=`echo $1 | cut -d "." -f1`
fi
if [ $# -gt 1 ] ; then
  CMAKE_VERSION=$2
fi
if [ $# -gt 2 ] ; then
  BUILD_TOOLS_VERSION=$3
fi
if [ $# -gt 3 ] ; then
  ANDROID_PLATFORM=$4
fi

echo "NDK Version: ${ANDROID_NDK_VERSION}"
echo "NDK Major: ${ANDROID_NDK_MAJOR}"
echo "CMake Version: ${CMAKE_VERSION}"
echo "Build Tools: ${BUILD_TOOLS_VERSION}"
echo "Android Platform: ${ANDROID_PLATFORM}"

# Skip installation if Android NDK and Build Tools already exist
if [ -d "${ANDROID_HOME}/ndk/${ANDROID_NDK_VERSION}" ] && [ -d "${ANDROID_HOME}/build-tools/${BUILD_TOOLS_VERSION}" ]; then
  echo "Android NDK ${ANDROID_NDK_VERSION} and Build Tools ${BUILD_TOOLS_VERSION} already installed. Re-installing"
fi

wget -q http://dl.google.com/android/repository/commandlinetools-linux-${ASDKTOOLS_VERSION}.zip -O commandlinetools-linux.zip
echo "${ASDKTOOLS_SHA256} *commandlinetools-linux.zip" | sha256sum --check -
unzip commandlinetools-linux.zip
rm commandlinetools-linux.zip
mkdir -p "${ASDKTOOLS_HOME}"
cp -r cmdline-tools/* "${ASDKTOOLS_HOME}/"
rm -r cmdline-tools

# The following popular fix makes sdkmanager honour $http_proxy variables
mv ${ASDKTOOLS_HOME}/bin/sdkmanager ${ASDKTOOLS_HOME}/bin/sdkmanager-vanilla
cat >${ASDKTOOLS_HOME}/bin/sdkmanager <<"EOF"
#!/bin/sh
if test -n "$http_proxy"; then
  PROXY_HOST=`echo $http_proxy | sed 's@.*//\(.*\):.*@\1@'`
  PROXY_PORT=`echo $http_proxy | sed 's@.*//.*:\(.*\)@\1@'`
  PROXY="--proxy=http --proxy_host=$PROXY_HOST --proxy_port=$PROXY_PORT"
else
  PROXY=""
fi
exec "`dirname $0`/sdkmanager-vanilla" $PROXY "$@"
EOF
for f in ${ASDKTOOLS_HOME}/bin/* ; do
  chmod +x "$f"
  target="/usr/bin/$(basename "$f")"
  [ -e "$target" ] || ln -s "$f" "$target"
done


# Create package list
cat >./package-list-minimal.txt <<EOF
platform-tools
build-tools;${BUILD_TOOLS_VERSION}
cmake;${CMAKE_VERSION}
platforms;android-${ANDROID_PLATFORM}
ndk;${ANDROID_NDK_VERSION}
extras;android;m2repository
extras;google;m2repository
EOF

mkdir ~/.android 2>/dev/null || true
touch ~/.android/repositories.cfg
# NOTE: sdkmanager returns exit code 141
(yes || true) | ${ASDKTOOLS_HOME}/bin/sdkmanager --licenses --sdk_root="$ANDROID_HOME" || true
${ASDKTOOLS_HOME}/bin/sdkmanager --verbose --package_file=./package-list-minimal.txt --sdk_root="$ANDROID_HOME"
test -d "${ANDROID_HOME}/build-tools/${BUILD_TOOLS_VERSION}"
test -d "${ANDROID_HOME}/ndk/${ANDROID_NDK_VERSION}"
for f in ${ANDROID_HOME}/ndk/${ANDROID_NDK_VERSION}/* ; do
  ln -sf "$f" "/usr/bin/`basename $f`"
done


ENV_FILE="android.env"

# Create or overwrite the environment file
tee "${ENV_FILE}" > /dev/null <<EOF
export ANDROID_HOME=${ANDROID_HOME}
export ANDROID_NDK_HOME=${ANDROID_NDK_HOME}
export ANDROID_NDK_VERSION=${ANDROID_NDK_VERSION}
export ANDROID_NDK_MAJOR=${ANDROID_NDK_MAJOR}
export ANDROID_NDK=${ANDROID_NDK_HOME}
export LEIP_NDK_CC=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android29-clang++
EOF

echo "Environment written to ${ENV_FILE}"
echo "To use it in your current shell, run:"
echo "   source ${ENV_FILE}"
echo ""
