#!/bin/bash
#TODO: this script is deprecated since MRS 9.2.0. Clean it when MRS 9.1.0 is out of support.

SCRIPT_DIR=$(cd "`dirname $(readlink -nf "$0")`"; pwd -P)
. "${SCRIPT_DIR}/RevoTracing"

trace "$*"

# script parameters
fifoToSpark=$1   # 1
shift
fifoFromSpark=$1 # 2
shift
dfNames=$1    # 3
shift
xdfUuids=$1    # 4
shift
clusterGUIDDir=$1  # 5
shift
user=$1   # 6
shift
rSessionPid=$1  # 7
shift
appId=$1  # 8
shift
wait=$1  # 9
shift
# parameter check for forward compatibility
# checking whether loggingLevel is missing
# if it is missing, give default value zero
if [ "$#" -eq 0 ]; then
    loggingLevel=0
else
    loggingLevel=$1
fi



# build Spark shell id, and get shell pid if already running
SHELL_PREFIX="scaleR-spark-won2r0FHOA"
SHELL_ID="${SHELL_PREFIX}-${user}-${rSessionPid}-${appId}"
if [ "${wait}" == "FALSE" ] || [ "${wait}" == "false" ]; then
    SHELL_ID="${SHELL_ID}-${jobId}"
fi

SHELL_PID=$(pgrep -f "${SHELL_ID}")

if [ -z "${SHELL_PID}" ]; then
    # For sparklyr backend, we only can base on yarn to query it.
    YARN_LOG=`yarn application -list 2>/dev/null`
    if [[ ${YARN_LOG} != *"${SHELL_ID}"* ]]; then
        # If no spark is running, then just need to remove obj in R.
        echo "No corresponding Spark App is alive. Removing the object in R."
        exit 0
    fi
fi

# create command id only for jobFifoFromSpark communication
commandId=$(mktemp -u FILENAMEXXXXXX | sed 's/FILENAME//g')

jobFifoFromSpark=${fifoFromSpark}-${commandId}
rm -f "$jobFifoFromSpark"
mkfifo "$jobFifoFromSpark"

SPARK_COMMAND="remove-data-frame dfNames=${dfNames} xdfUuids=${xdfUuids} clusterGUIDDir=${clusterGUIDDir} loggingLevel=$loggingLevel commandId=$commandId"

trace "command for Spark: ${SPARK_COMMAND}"
# Redirect namepipe as file descriptor 4 to avoid blocking the script for handling the case: spark crashes before fifo is read. 
# Cannot simply use & to unblock because it can cause zombie HPALauncher process
exec 4<> ${fifoToSpark}
echo "${SPARK_COMMAND}" >&4
trace "command sent, waiting for response"

# wait for spark to finish
SPARK_RESPONSE=$(cat $jobFifoFromSpark)
rm -f "$jobFifoFromSpark"
trace "response from Spark: ${SPARK_RESPONSE}"
if [[ "${SPARK_RESPONSE}" =~ "ERROR" ]]; then
    while read -r line; do
        echo "$line"
    done <<<"${SPARK_RESPONSE}"
    exit 1
fi
