#!/bin/bash

[ -z "${NOTIFY_EMAIL}" ] && return 1
NOTIFY_STATE=${NOTIFY_STATE:-rf}

source $CLOUD_LIBEXEC_LOCATION/nethelper-client.sh

push_prepared () {
    [[ "${NOTIFY_STATE}" =~ p ]] || return 0
    data="PREPARED"
    helper_email "${NOTIFY_EMAIL}" "${GRID_GLOBAL_JOBID}" $data
}

push_connect () {
    [[ "${NOTIFY_STATE}" =~ r ]] || return 0
    data="READY $VM_ACCESS_METHOD://${VM_ACCESS_SOCKET} ${VM_ACCESS_USERNAME} ${VM_ACCESS_PASSWORD}"
    helper_email "${NOTIFY_EMAIL}" "${GRID_GLOBAL_JOBID}" $data
}

push_failed () {
    [[ "${NOTIFY_STATE}" =~ f ]] || return 0
    msg="$1"
    data="FAILED \"${msg// /\\ }\""
    helper_email "${NOTIFY_EMAIL}" "${GRID_GLOBAL_JOBID}" $data
}

push_finished () {
    [[ "${NOTIFY_STATE}" =~ e ]] || return 0
    data="FINISHED"
    helper_email "${NOTIFY_EMAIL}" "${GRID_GLOBAL_JOBID}" $data
}