#!/bin/bash

# Copyright (C) 2019 The Android Open Source Project
#
# Licensed 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.

# launcher script for vts-tradefed harness
# can be used from an Android build environment, or a standalone vts zip

UTILS_SCRIPT="$(dirname $(realpath $0))/test-utils-script"

if [ ! -f "${UTILS_SCRIPT}" ]
then
  UTILS_SCRIPT="${ANDROID_BUILD_TOP}/platform_testing/scripts/test-utils-script"
fi

if [ ! -f "${UTILS_SCRIPT}" ]
then
  echo -e "Cannot find test-utils-script in the same location as this script and ANDROID_BUILD_TOP is not defined."
  exit 1
fi

source ${UTILS_SCRIPT}

checkPath aapt
checkPath adb

RDBG_FLAG=$(getRemoteDbgFlag)

# get OS
HOST=`uname`
HOST_ARCH=`uname -sm`
if [ "$HOST_ARCH" == "Linux x86_64" ]; then
    OS="linux-x86"
elif [ "$HOST_ARCH" == "Linux aarch64" ]; then
    OS="linux-arm64"
    # Bundled java is for linux-x86 so use host JDK on linux-arm64
    JAVA_BINARY=java
elif [ "$HOST_ARCH" == "Darwin x86_64" ]; then
    OS="darwin-x86"
    # Bundled java is for linux so use host JDK on Darwin
    JAVA_BINARY=java
else
    echo "Unrecognized OS"
    exit
fi

# check if in Android build env
if [ ! -z "${ANDROID_BUILD_TOP}" ]; then
    if [ ! -z "${ANDROID_HOST_OUT}" ]; then
      VTS_ROOT=${ANDROID_HOST_OUT}/vts
    else
      VTS_ROOT=${ANDROID_BUILD_TOP}/${OUT_DIR:-out}/host/${OS}/vts
    fi
    if [ ! -d ${VTS_ROOT} ]; then
        echo "Could not find $VTS_ROOT in Android build environment. Try 'make vts'"
        exit
    fi;
fi;

if [ -z ${VTS_ROOT} ]; then
    # assume we're in an extracted vts install
    VTS_ROOT="$(dirname $(realpath $0))/../.."
fi;

if [ -z ${JAVA_BINARY} ]; then
      JAVA_BINARY=${VTS_ROOT}/android-vts/jdk/bin/java
fi;

if [ ! -f "${JAVA_BINARY}" ]; then
      JAVA_BINARY=java
fi

checkPath ${JAVA_BINARY}
checkJavaVersion ${JAVA_BINARY}

JAR_DIR=${VTS_ROOT}/android-vts/tools

for JAR in ${JAR_DIR}/*.jar; do
    JAR_PATH=${JAR_PATH}:${JAR}
done
JAR_PATH=${JAR_PATH:1} # Strip off leading ':'

OPTIONAL_JARS="
  google-tradefed
  google-tradefed-tests
  google-tf-prod-tests"

STANDALONE_JAR_DIR=${ANDROID_HOST_OUT}/framework
for JAR in $OPTIONAL_JARS; do
    if [ -f "${JAR_DIR}/${JAR}.jar" ]; then
        JAR_PATH=${JAR_PATH}:${JAR_DIR}/${JAR}.jar
    elif [ -f "${STANDALONE_JAR_DIR}/${JAR}.jar" ]; then
        JAR_PATH=${JAR_PATH}:${STANDALONE_JAR_DIR}/${JAR}.jar
    fi;
done

LIB_DIR=${VTS_ROOT}/android-vts/lib
loadSharedLibraries "$HOST" "$LIB_DIR"

# include any host-side test jars
for j in $(find ${VTS_ROOT}/android-vts/testcases -type f -name '*.jar'); do
    JAR_PATH=${JAR_PATH}:$j
done

VTS_TESTCASES=${VTS_ROOT}/android-vts/testcases/
VTS_TESTCASES=${VTS_TESTCASES} ${JAVA_BINARY} $RDBG_FLAG -Xmx16g -XX:+HeapDumpOnOutOfMemoryError -cp ${JAR_PATH} -DVTS_ROOT=${VTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
