#!/bin/bash
[% c("var/set_default_env") -%]
output_dir=[% dest_dir %]/[% c('filename') %]
gradle_repo=$rootdir/[% c('input_files_by_name/gradle-dependencies') %]
export GRADLE_MAVEN_REPO="file://$gradle_repo"

# The download script assumes artifact package name is the complete URL path.
# In some cases this is incorrect, so copy those artifacts to correct location
cp -r $gradle_repo/dl/android/maven2/* $gradle_repo
cp -r $gradle_repo/plugins-release/* $gradle_repo
cp -r $gradle_repo/maven2/* $gradle_repo

mkdir -p /var/tmp/build $output_dir

[% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz

# Patch projects
cd /var/tmp/build/[% project %]-[% c('version') %]
patch -p1 < $rootdir/gradle.patch
patch -p1 < $rootdir/require-api.patch

# Remove native files, otherwise build will fail after gradle.patch
rm -fR service/src/main/jni

# Replace libraries with the config dependencies
rm service/libs/*
cp $rootdir/[% c('input_files_by_name/topl') %]/*{.aar,.jar} service/libs/

# Build Android Libraries and Apps
$GRADLE_HOME/gradle-4.1/bin/gradle --offline -P androidplugin=3.0.1 -P appcompatVersion=23.4.0 -P compileVersion=26 -P targetVersion=26 -P minVersion=16 assembleRelease -x lint

# Package
cp jsocksAndroid/build/outputs/aar/* service/build/outputs/aar/* $output_dir
