#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.

#set -o xtrace
set -o errexit
set -o pipefail
set -o nounset

bin="$(cd "$(dirname "$0")" > /dev/null; pwd)"
home="$(cd "$bin/../.." > /dev/null; pwd)" # this script lives in .build/sh

# Switch to the project root so all commands work properly
cd "$home"

error() {
  echo "ERROR: $*" 1>&2
  exit 1
}

usage() {
  if [[ $# -gt 0 ]]; then
    echo "$*" 1>&2
  fi
  cat <<EOF
Usage: $(basename "$0") [class name] (options)*
Options:
--cdc                               - runs with cdc enabled
--compression                       - run with compression enabled
--jvm-dtest-version|--dtest-version - which jvm-dtest version to use
--reuse                             - skip calling ant realclean
--tokens|--num-tokens               - number of vnode tokens to use for jvm-dtest
-h|--help                           - this help page
EOF
  exit 1
}

if [[ $# -eq 0 ]]; then
  usage "Missing required arguments"
fi

class_name=""
task="testclasslist"
fresh_build=true
jvm_dtest_version=""
num_tokens=""
while [[ "$#" -gt 0 ]]; do
  case "$1" in
    --tokens|--num-tokens)
      num_tokens="$2"
      shift 2
      ;;
    --cdc)
      task="testclasslist-cdc"
      shift
      ;;
    --compression)
      task="testclasslist-compression"
      shift
      ;;
    --reuse)
      fresh_build=false
      shift
      ;;
    --jvm-dtest-version|--dtest-version)
      jvm_dtest_version="$2"
      shift 2
      ;;
    -h|--help)
      usage
      shift
      ;;
    *)
      if [[ -z "${class_name:-}" ]]; then
        class_name="$1"
        shift
      else
        usage "Unknown argument $1"
      fi
      ;;
  esac
done
if [[ -z "${class_name:-}" ]]; then
  usage "No class provided"
fi
# Is the class name valid?
if [[ "$class_name" =~ [:#$] ]]; then
  error "Unexpected class name: $class_name.  Illegal characters were detected: ':', '#', and '\$' are not allowed!  ci-test only supports class names, and not references to methods."
fi
path="$(echo "$class_name" | tr '.' '/' ).java"
prefix="$( find test | grep "$path" | awk -F/ '{print $2}' )"

JAVA_VERSION=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F. '{print $1}')

if [ "$JAVA_VERSION" -ge 11 ]; then
  export CASSANDRA_USE_JDK11=true
fi

if [ -n "${num_tokens:-}" ]; then
  export CASSANDRA_DTEST_NUM_TOKENS="$num_tokens"
fi

opts=(
  -Dant.gen-doc.skip=true
  -Dno-checkstyle=true
  -Dant.gen-doc.skip=true
  -Drat.skip=true
)
if [[ -n "${jvm_dtest_version:-}" ]]; then
  opts+=( "-Ddtest-api.version=$jvm_dtest_version" )
fi

if [[ "$fresh_build" == true ]]; then
  if [[ ! "$path" =~ distributed/upgrade ]]; then
    ant "${opts[@]}" realclean
    ant "${opts[@]}"
    ant "${opts[@]}" generate-idea-files
  fi
fi

# cleanup logs so w/e is around are for this run
rm -rf build/test/logs || true

test_timeout=$(grep "name=\"test.$prefix.timeout\"" build.xml | awk -F'"' '{print $4}' || true)
if [ -z "$test_timeout" ]; then
  test_timeout=$(grep 'name="test.timeout"' build.xml | awk -F'"' '{print $4}')
fi
if [[ "$fresh_build" == false ]]; then
  opts+=(-Dno-build-test=true)
fi

if [[ "$prefix" == "simulator" ]]; then
  ant "${opts[@]}" test-simulator-dtest -Dtest.name="$(echo "$path" | awk -F'.' '{print $1}' | awk -F'/' '{print $NF}')"
else
  ant "${opts[@]}" "$task" -Dtest.timeout="$test_timeout" -Dtest.classlistfile=<(echo "$path") -Dtest.classlistprefix="$prefix"
fi
