#!/bin/bash

echo $0
set -e
set -x

if [ -z $ANDROID_HOME ]; then
    echo "ANDROID_HOME env var must be set!"
    exit 1
fi

# disable the repositories of proprietary stuff
disabled="
@version@=1
@disabled@https\://dl.google.com/android/repository/extras/intel/addon.xml=disabled
@disabled@https\://dl.google.com/android/repository/glass/addon.xml=disabled
@disabled@https\://dl.google.com/android/repository/sys-img/android/sys-img.xml=disabled
@disabled@https\://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml=disabled
@disabled@https\://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml=disabled
@disabled@https\://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml=disabled
"
test -d ${HOME}/.android || mkdir ${HOME}/.android
# there are currently zero user repos
echo 'count=0' > ${HOME}/.android/repositories.cfg
for line in $disabled; do
    echo $line >> ${HOME}/.android/sites-settings.cfg
done

# Include old makebuildserver cache that is a Vagrant synced_folder
# for sdkmanager to use.
cachedir=$HOME/.cache/sdkmanager
mkdir -p $cachedir
pushd $cachedir
for f in /vagrant/cache/*.zip; do
    test -e $f && ln -s $f
done
popd

# TODO do not preinstall 'tools' or 'platform-tools' at all, app builds don't need them
packages="
  tools;25.2.5
  platform-tools
  build-tools;19.1.0
  build-tools;20.0.0
  build-tools;21.1.2
  build-tools;22.0.1
  build-tools;23.0.1
  build-tools;23.0.2
  build-tools;23.0.3
  build-tools;24.0.0
  build-tools;24.0.1
  build-tools;24.0.2
  build-tools;24.0.3
  build-tools;25.0.0
  build-tools;25.0.1
  build-tools;25.0.2
  build-tools;25.0.3
  build-tools;26.0.0
  build-tools;26.0.1
  build-tools;26.0.2
  build-tools;26.0.3
  build-tools;27.0.0
  build-tools;27.0.1
  build-tools;27.0.2
  build-tools;27.0.3
  build-tools;28.0.0
  build-tools;28.0.1
  build-tools;28.0.2
  build-tools;28.0.3
  build-tools;29.0.2
  build-tools;29.0.3
  build-tools;30.0.0
  build-tools;30.0.1
  build-tools;30.0.2
  build-tools;30.0.3
  build-tools;31.0.0
  build-tools;32.0.0
  build-tools;33.0.0
  platforms;android-10
  platforms;android-11
  platforms;android-12
  platforms;android-13
  platforms;android-14
  platforms;android-15
  platforms;android-16
  platforms;android-17
  platforms;android-18
  platforms;android-19
  platforms;android-20
  platforms;android-21
  platforms;android-22
  platforms;android-23
  platforms;android-24
  platforms;android-25
  platforms;android-26
  platforms;android-27
  platforms;android-28
  platforms;android-29
  platforms;android-30
  platforms;android-31
  platforms;android-32
  platforms;android-33
"

if [ $# -gt 0 ]; then
    echo found args
    packages=$@
fi

# temporary test of whether this script ran. It will change once
# 'tools' is no longer installed by default.
if [ ! -x $ANDROID_HOME/tools/bin/sdkmanager ]; then
    mkdir -p ${ANDROID_HOME}/
    sdkmanager $packages
fi

# this hacked cache should not end up in the Vagrant box or Docker image
rm -rf $cachedir

mkdir -p $ANDROID_HOME/licenses/

cat << EOF > $ANDROID_HOME/licenses/android-sdk-license

8933bad161af4178b1185d1a37fbf41ea5269c55

d56f5187479451eabf01fb78af6dfcb131a6481e

24333f8a63b6825ea9c5514f83c2829b004d1fee
EOF

cat <<EOF > $ANDROID_HOME/licenses/android-sdk-preview-license

84831b9409646a918e30573bab4c9c91346d8abd
EOF

cat <<EOF > $ANDROID_HOME/licenses/android-sdk-preview-license-old

79120722343a6f314e0719f863036c702b0e6b2a

84831b9409646a918e30573bab4c9c91346d8abd
EOF

cat <<EOF > $ANDROID_HOME/licenses/intel-android-extra-license

d975f751698a77b662f1254ddbeed3901e976f5a
EOF

chmod a+X $(dirname $ANDROID_HOME/)
chmod -R a+rX $ANDROID_HOME/
chgrp vagrant $ANDROID_HOME
chmod g+w $ANDROID_HOME
find $ANDROID_HOME/ -type f -executable -print0 | xargs -0 chmod a+x

# allow gradle to install newer build-tools and platforms
mkdir -p $ANDROID_HOME/{build-tools,platforms}
chgrp vagrant $ANDROID_HOME/{build-tools,platforms}
chmod g+w $ANDROID_HOME/{build-tools,platforms}

# allow gradle/sdkmanager to install into the new m2repository
test -d $ANDROID_HOME/extras/m2repository || mkdir -p $ANDROID_HOME/extras/m2repository
find $ANDROID_HOME/extras/m2repository -type d | xargs chgrp vagrant
find $ANDROID_HOME/extras/m2repository -type d | xargs chmod g+w
