#!/usr/bin/env bash

set -e

DIRNAME=$(dirname ${BASH_SOURCE[0]})
source ${DIRNAME}/../conf/insightedge-env.sh

#Because of SPARK-21642, the driver url is composed from hostname instead of ip. This is a workaround - setting SPARK_LOCAL_HOSTNAME to the submitting machine IP
if [[ -z "${SPARK_LOCAL_HOSTNAME}" ]]; then
    export SPARK_LOCAL_HOSTNAME="${XAP_NIC_ADDRESS}"
fi

SUBMIT_ARGS=( "$@" )
# get length of an array
length=${#SUBMIT_ARGS[@]}

# loop over arguments to find if in cluster deploy-mode
for (( i=0; i<${length}; i++ ));
do
  if [[ ${SUBMIT_ARGS[$i]} == "--deploy-mode" && ${SUBMIT_ARGS[$(($i + 1))]} == "cluster" ]]; then
    # In cluster mode, local env variables override remote machine env variables (spark JIRA SPARK-24456)
    # To work around this behavior:

    # 1. stop spark submit from sourcing spark-env
    export SPARK_ENV_LOADED=1

    # 2. empty i9e local classpaths env variable
    unset SPARK_DIST_CLASSPATH
  fi
done

"${SPARK_HOME}"/bin/spark-submit $@