#!/bin/bash --posix
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# 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.
#
# This script was generated from android_device_stub_template.txt.  Please
# don't edit it directly.


# determine our runfiles directory.
# largely cribbed from java_stub_template.txt

# Call this program X.  X was generated by an android_device rule
# X may be invoked in many ways:
#   1a) directly by a user, with $0 in the output tree
#   1b) via 'bazel run' (similar to case 1a)
#   2) directly by a user, with $0 in X's runfiles tree
#   3) by another program Y which has a data dependency on X, with $0 in Y's
#   runfiles tree
#   4) via 'bazel test --test_strategy=local'
#   5) by a genrule cmd, with $0 in the output tree
#   6) case 3 in the context of a genrule
#   7) as the first argument to --run_under for a run or test action, with $0 in
#   the output tree - the --blaze_unittest flag identifies this case
#
# For case 1, $0 will be a regular file, and the runfiles tree will be
# at $0.runfiles.
# For case 2 or 3, $0 will be a symlink to the file seen in case 1.
# For case 4, $TEST_SRCDIR should already be set.
# Case 5 is handled like case 1.
# Case 6 is handled like case 3.


# First normalize $0 to an absolute path and store it in $self
case "$0" in
  /*) self="$0" ;;
  *)  self="$PWD/$0" ;;
esac

if [[ -z "$DEVICE_RUNFILES" ]]; then
  while true; do
    if [[ -e "$self.runfiles" ]]; then
      # case 1 and 2
      DEVICE_RUNFILES="$self.runfiles"
      break
    fi
    if [[  $self == *.runfiles/* ]]; then
      DEVICE_RUNFILES="${self%.runfiles/*}.runfiles"
      # don't break; this value is only a last resort for case 6b
    fi
    if [[ ! -L "$self" ]]; then
      break
    fi
    # $self is a symbo link...
    readlink="$(readlink "$self")"
    if [[ "$readlink" = /* ]]; then
      # and its absolute, point change self to point to where the
      # symbolink was pointing.
      self="$readlink"
    else
      # and its relative, since self is absolute, use its location
      # to determine the absolute location of the symbolic link ref
      # and repoint self to this new location.
      self="${self%/*}/$readlink"
    fi
  done
  if [[ -n "$DEVICE_RUNFILES" ]]; then
    export TEST_SRCDIR=${TEST_SRCDIR:-$DEVICE_RUNFILES}
  fi
  if [[ -z "$TEST_SRCDIR" ]]; then
    echo "$0:  Cannot locate runfiles directory."  1>&2
    echo '  (Set $DEVICE_RUNFILES to inhibit searching.)'  1>&2
    exit 1
  fi
fi

# In case 7 a --blaze_unittest flag indicates that all subsequent argv are test
# to be copied into the emulator and run.  Otherwise all argv belong to
# $unified_launcher.
extra_launcher_flags=()
test_argv=()
is_blaze_unittest=0
for arg; do
  if ((is_blaze_unittest)); then
    test_argv+=("${arg}")
  elif [[ "${arg}" == "--blaze_unittest" ]]; then
    is_blaze_unittest=1
  else
    extra_launcher_flags+=("${arg}")
  fi
done

WORKSPACE_DIR=${TEST_SRCDIR}/%workspace%

unified_launcher="${WORKSPACE_DIR}/%unified_launcher%"
adb="${WORKSPACE_DIR}/%adb%"
emulator_x86="${WORKSPACE_DIR}/%emulator_x86%"
emulator_arm="${WORKSPACE_DIR}/%emulator_arm%"
adb_static="${WORKSPACE_DIR}/%adb_static%"
source_properties_file="${WORKSPACE_DIR}/%source_properties_file%"
mksdcard="${WORKSPACE_DIR}/%mksdcard%"
empty_snapshot_fs="${WORKSPACE_DIR}/%empty_snapshot_fs%"
emulator_system_images="%system_images%"
image_input_file="${WORKSPACE_DIR}/%image_input_file%"
emulator_metadata_path="${WORKSPACE_DIR}/%emulator_metadata_path%"
emulator_bios_files="%bios_files%"
android_sdk_path="${WORKSPACE_DIR}/%sdk_path%"

system_images=''
for image in $emulator_system_images
do
  system_images="${WORKSPACE_DIR}/${image} ${system_images}"
done

bios_files=''
for bios_file in $emulator_bios_files
do
  bios_files="${WORKSPACE_DIR}/${bios_file},${bios_files}"
done

if [[ -n "${test_argv[0]}" ]]; then
  # We are trying to run a unittest that's not an APK, e.g. a cc_test.
  source "%testing_shbase%" || exit 1
  adb_port="$(pick_random_unused_tcp_port)"
  emulator_port="$(pick_random_unused_tcp_port)"
  extra_launcher_flags=(
      --adb_port="${adb_port}"
      --emulator_port="${emulator_port}"
      "${extra_launcher_flags[@]}"
  )
fi

"${unified_launcher}" \
    --action=start \
    --flag_configured_android_tools \
    --adb="${adb}" \
    --emulator_x86="${emulator_x86}" \
    --emulator_arm="${emulator_arm}" \
    --adb_static="${adb_static}" \
    --mksdcard="${mksdcard}" \
    --empty_snapshot_fs="${empty_snapshot_fs}" \
    --system_images="${system_images}" \
    --image_input_file="${image_input_file}" \
    --emulator_metadata_path="${emulator_metadata_path}" \
    --source_properties_file="${source_properties_file}" \
    --bios_files="${bios_files}" \
    --android_sdk_path="${android_sdk_path}" \
    "${extra_launcher_flags[@]}" || exit 1

if [[ -n "${test_argv[0]}" ]]; then
  source "%android_runtest%"
  runtest "localhost:${adb_port}" "${test_argv[@]}"
fi
