# Set up cluster-specific variables
PARAVIEW_DIR={{paraviewInstallDir if paraviewInstallDir else "/opt/paraview/install"}}
PV_BATCH="${PARAVIEW_DIR}/bin/pvbatch"
VISUALIZER="pvw-visualizer.py"
GET_PORT_PYTHON_CMD='import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'
RC_PORT=`python -c "${GET_PORT_PYTHON_CMD}"`
echo ${RC_PORT} > /tmp/{{job._id}}.rc_port

# Enable interactors for pvbatch
export PV_ALLOW_BATCH_INTERACTION=1

# Run in MPI mode
MPIPROG="mpiexec"

# Need to adjust paths for Mac application install
case $PARAVIEW_DIR in
     *paraview.app) PV_BATCH="${PARAVIEW_DIR}/Contents/bin/pvbatch"
      MPIPROG="${PARAVIEW_DIR}/Contents/MacOS/mpiexec"
      ;;
esac

REVERSE="--reverse-connect-port ${RC_PORT}"

PROXIES="config/defaultProxies.json"
JOB_OUTPUT_DIR="{{ cluster.config.jobOutputDir if cluster.config.jobOutputDir else '$HOME'}}"

# Get the ip of this host
IPADDRESS={{ cluster.config.host }}

if [ -z "$IPADDRESS" ]; then
IPADDRESS=`hostname`
fi

WEBSOCKET_PORT={{ cluster.config.paraview.websocketPort }}

{% if cluster.type == 'trad' -%}
#WEBSOCKET_PORT=`python -c "${GET_PORT_PYTHON_CMD}"`
WEBSOCKET_PORT='9090'
# Create proxy entry
KEY="{{ sessionKey }}"
BODY='{"host": "'$IPADDRESS'", "port": '${WEBSOCKET_PORT}', "key": "'$KEY'"}'
curl --silent --show-error -o /dev/null -X POST -d "$BODY"  --header "Content-Type: application/json" {{ baseUrl }}/proxy
{% endif -%}

# Replace Visualizer script
{{ 'VISUALIZER="%s"' % scriptToRun if scriptToRun else '# No swap...' }}

# First run pvbatch
${MPIPROG} {{ '-n %s' % numberOfSlots if numberOfSlots else '-n 1'}} ${PV_BATCH} {{'--mesa-llvm' if not gpu else ''}} ${VISUALIZER} --timeout 3600 --host $IPADDRESS --port ${WEBSOCKET_PORT} \
{{ '--data %s' % dataDir if dataDir else ''}} \
{{ '--load-file %s' % fileName if fileName else '' }}

{% if cluster.type == 'trad' -%}
# Remove proxy entry
curl --silent --show-error -o /dev/null -X DELETE "{{ baseUrl }}/proxy/${KEY}"
{% endif -%}
