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

# Figure out where ADAM is installed
SCRIPT_DIR="$(cd `dirname $0`/..; pwd)"

# Get list of required jars for ADAM
ADAM_JARS=$("$SCRIPT_DIR"/bin/compute-adam-jars.sh)

# Binary distribution
REPO_DIR="$SCRIPT_DIR/repo"
if [ ! -d "$REPO_DIR" ]; then
# Fallback to source directory
REPO_DIR="$SCRIPT_DIR/adam-cli/target/appassembler/repo/"
fi

# Find the ADAM CLI jar
CLI_DIR="$REPO_DIR/org/bdgenomics/adam/adam-cli"
num_versions=$(ls "$CLI_DIR" | wc -l)
if [ "$num_versions" -eq "0" ]; then
  echo "Failed to find adam-cli jar in $CLI_DIR"
  echo "You need to build ADAM before running this program."
  exit 1
fi
if [ "$num_versions" -gt "1" ]; then
  versions_list=$(ls "$CLI_DIR")
  echo "Found multiple ADAM CLI versions in $CLI_DIR:"
  echo "$versions_list"
  echo "Please remove all but one."
  exit 1
fi
ADAM_CLI_JAR=$(ls $CLI_DIR/*/adam-cli-*.jar)

# Find spark-submit script
if [ -z "$SPARK_HOME" ]; then
  echo "SPARK_HOME must be set for 'adam-submit'"
  exit 1
else
  SPARK_SUBMIT="$SPARK_HOME"/bin/spark-submit
fi

# Split args into Spark args and ADAM args
# NOTE: if Spark uses gatherSparkSubmitOpts in spark-submit, this is unnecessary
function usage() {
  echo "adam-submit <spark-args> <adam-args>"
  exit 0
}
source "$SPARK_HOME"/bin/utils.sh
SUBMIT_USAGE_FUNCTION=usage
gatherSparkSubmitOpts "$@"

# submit the job to Spark
"$SPARK_SUBMIT" \
  --class org.bdgenomics.adam.cli.ADAMMain \
  --conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
  --conf spark.kryo.registrator=org.bdgenomics.adam.serialization.ADAMKryoRegistrator \
  --conf spark.kryoserializer.buffer.mb=4 \
  --conf spark.kryo.referenceTracking=true \
  --jars "$ADAM_JARS" \
  "${SUBMISSION_OPTS[@]}" \
  "$ADAM_CLI_JAR" \
  "${APPLICATION_OPTS[@]}"
