#!/bin/bash

# Define version for beast-spark package.
beast_version=0.10.1

# Initialize jars and packages list
declare -a jars
declare -a packages=("edu.ucr.cs.bdlab:beast-spark:$beast_version" "org.mortbay.jetty:jetty:6.1.26" "org.eclipse.jetty:jetty-servlet:9.4.48.v20220622" "org.eclipse.jetty:jetty-server:9.4.48.v20220622" "org.geotools:gt-epsg-hsql:26.1" "org.geotools:gt-coverage:26.1" "org.locationtech.jts:jts-core:1.19.0" "com.google.protobuf:protobuf-java:3.21.12")
declare -a excluded_packages=("org.apache.hadoop:hadoop-common" "org.apache.hadoop:hadoop-hdfs" "javax.media:jai_core")
declare -a repositories=("https://repo.osgeo.org/repository/release/")

# Initialize path to lib dir and check for existing beast-spark JAR
lib_dir="$(dirname "$0")/../lib"

# Create the lib directory if it does not exist
[ ! -d "$lib_dir" ] && mkdir -p "$lib_dir"

# Download javax activation and jai_core if they do not exist locally
[ ! -f "${lib_dir}/jai_core-1.1.3.jar" ] && curl -o "${lib_dir}/jai_core-1.1.3.jar" "https://repo1.maven.org/maven2/javax/media/jai_core/1.1.3/jai_core-1.1.3.jar"

declare -a additional_spark_args
# Populate jars with all jar files under ../lib/
for jar_file in "$lib_dir"/*.jar; do
  jars+=("$jar_file")
done

# Handle command line arguments
# Loop over command-line arguments
while [[ $# -gt 0 ]]; do
    key="$1"
    shift # remove the first argument from the list

    if [[ $key == "--jars" ]]; then
        jars+=("$1")
        shift # remove the value argument
    elif [[ $key == "--packages" ]]; then
        packages+=("$1")
        shift
    elif [[ $key == "--repositories" ]]; then
        repositories+=("$1")
        shift
    elif [[ $key == "--exclude-package" ]]; then
        excluded_packages+=("$1")
        shift
    elif [[ $key == --* ]]; then
        additional_spark_args+=("$key" "$1")
        shift
    else
        program="$key"
        break # exit the loop when the first non-option argument is found
    fi
done

# Generate Spark arguments
spark_args="--jars $(IFS=,; echo "${jars[*]}") --packages $(IFS=,; echo "${packages[*]}") --repositories $(IFS=,; echo "${repositories[*]}") --exclude-packages $(IFS=,; echo "${excluded_packages[*]}")"
spark_args+=" ${additional_spark_args[*]}"

if [[ -n "$program" && "$program" == *.jar && -f "$program" ]]; then
  spark_args+=" $program"
else
  # The following part will result in an error message from Spark that requires a program but it will run fine
  spark_args+=" --class edu.ucr.cs.bdlab.beast.operations.Main . $program"
fi

# Loop through each remaining command-line argument
for arg in "$@"; do
  # Append the argument to the string
  spark_args+=" $arg"
done

eval spark-submit "${spark_args}"