#!/bin/bash

# Copyright (C) 2024 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 mcts-tradefed harness
# can be used from an Android build environment, or a standalone mcts 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 adb
checkPath java
checkJavaVersion java

RDBG_FLAG=$(getRemoteDbgFlag)

# get OS
HOST=`uname`
if [ "$HOST" == "Linux" ]; then
    OS="linux-x86"
elif [ "$HOST" == "Darwin" ]; then
    OS="darwin-x86"
else
    echo "Unrecognized OS"
    exit
fi

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

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

JAR_DIR=${MCTS_ROOT}/android-mcts/tools

for JAR in ${JAR_DIR}/*.jar; do
    JAR_PATH=${JAR_PATH}:${JAR}
done

# check if APE_API_KEY is set in the env by user.
if [ ! -n "${APE_API_KEY}" ]; then
    GTS_GOOGLE_SERVICE_ACCOUNT=${ANDROID_BUILD_TOP}/vendor/xts/tools/gts-google-service-account/gts-google-service-account.json
    # set KEY only for google if APE_API_KEY isn't set and GTS_GOOGLE_SERVICE_ACCOUNT exists in the soure tree.
    if [ -f "$GTS_GOOGLE_SERVICE_ACCOUNT" ]; then
        APE_API_KEY=${GTS_GOOGLE_SERVICE_ACCOUNT}
        export APE_API_KEY
    else
        echo "APE_API_KEY not set, GTS tests may fail without authentication."
    fi;
fi;
echo "APE_API_KEY: $APE_API_KEY"

LIB_DIR=${MCTS_ROOT}/android-mcts/lib
loadSharedLibraries "$HOST" "$LIB_DIR"

# include any host-side test jars
for j in $(find ${MCTS_ROOT}/android-mcts/testcases -name '*.jar'); do
    case "$j" in
      *testcases/art*)
        /bin/true
        ;;
      *)
        JAR_PATH=${JAR_PATH}:$j
        ;;
    esac
done

echo "java $RDBG_FLAG -cp ${JAR_PATH} -DMCTS_ROOT=${MCTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@""
java $RDBG_FLAG -cp ${JAR_PATH} -DMCTS_ROOT=${MCTS_ROOT} com.android.compatibility.common.tradefed.command.CompatibilityConsole "$@"
