#!/bin/bash

set -e

aggregate_logs() {
  #copy all logs back to Logs dataset
  ${ANACONDA_ENV}/bin/python log_aggregation.py
}

trap aggregate_logs EXIT TERM SIGTERM

export CLASSPATH=$("${HADOOP_HOME}"/bin/hadoop classpath --glob)
export HADOOP_HDFS_HOME=${HADOOP_HOME}
export HADOOP_CONF_DIR=${HADOOP_HOME}/etc/hadoop
export HADOOP_CLIENT_OPTS="-Dfs.permissions.umask-mode=0007"
## The name of the environment variable must be the same
## as in HopsSSLSocketFactory in Hops project
export MATERIAL_DIRECTORY=${CERTS_DIR}

cd /app || exit

# Create empty output logs so that the job doesn't wait for them
touch logs/stdout.log
touch logs/stderr.log

if [ "$MOUNT_HOPSFS" == "true" ]; then
  /bin/bash -c start_python_job_with_hopsfsmount.sh
else
  # Copy the executable python program from HopsFS and run it
  ${ANACONDA_ENV}/bin/python download_file.py ${APP_PATH}

  # Copy all files
  IFS=',' read -r -a array <<<"${APP_FILES}"
  for file in "${array[@]}"; do
    echo "Copying $file"
    ${ANACONDA_ENV}/bin/python download_file.py ${file}
  done

  if [[ "$APP_FILE" == *.py ]]; then
    # Python file
    ${ANACONDA_ENV}/bin/python "${APP_FILE}" ${APP_ARGS} 1> >(tee -a logs/stdout.log) 2> >(tee -a logs/stderr.log >&2)
  elif [[ "$APP_FILE" == *.ipynb ]]; then
    "Detected notebook file, running using papermill. Note that papermill expected args in the following format " | tee -a /app/logs/stderr.log
    "(-p arg1 value1 -p arg2 value2) as opposed to python jobs (-arg1 value1 -arg2 value2)." | tee -a /app/logs/stderr.log
    if [[ -z "${APP_ARGS}" ]]; then
      echo "No arguments provided" | tee -a /app/logs/stderr.log
    else
      echo "Found args: ${APP_ARGS}" | tee -a /app/logs/stderr.log
    fi

    # Remove path from file
    APP_FILE_BASENAME="${APP_FILE##*/}"
    # Remove notebook extension .ipynb
    APP_FILE_BASENAME="${APP_FILE_BASENAME%.ipynb}"

    "The basename is ${APP_FILE_BASENAME}" | tee -a /app/logs/stderr.log
        
    # Build output notebook name
    # output filename = input baseline name + .ipynb
    OUT_FILE=${APP_FILE_BASENAME}-output.ipynb
    "Notebook output will be written to ${OUT_FILE}" | tee -a /app/logs/stderr.log

    "fwedohfodhjlfhsdhfjdshofdsnlvnsdjknbavkjdbsvkjbfcdksvbdkcsbvcmdbsakjsdbavjbfhdsbvkdacbsvncxznmabvfjhbajhvkdfsvcjzxbacmncxsvbhdacsbvkjdsn\n" | tee -a /app/logs/stderr.log

    # Run notebook with Papermill
    # Usage: papermill [ARGUMENTS] [OPTIONS] NOTEBOOK_PATH [OUTPUT_PATH]
    # ${ANACONDA_ENV}/bin/python -m papermill ${APP_ARGS} "${APP_FILE}" "/app/logs/${OUT_FILE}" 1> >(tee -a logs/stdout.log) 2> >(tee -a logs/stderr.log >&2)

  else
    echo "File extension is not '.ipynb' or '.py'" | tee -a /app/logs/stderr.log
    exit 1
  fi

fi

