#!/bin/sh

#ALERT_EMAIL_TO="mahendra.patil@naaptol.com;naaptolreport@gmail.com"
#ALERT_EMAIL_SUBJECT="REPORT"
#ALERT_EMAIL_FROM="tech@naaptol.com"
#ALERT_EMAIL_MESSAGE
#ALERT_MESSAGE_FILE
#IS_ALERT_MESSAGE_FILE_ATTACHMENT

#############################################ALERT Functions defined Here START#####################################
ALERT_ROOT_PATH="/opt/anm/scripts/alert/"
ALERT_EMAIL_SUPPORT="mahendra.patil@naaptol.com"
ALERT_EMAIL_LAYOUT_DEFAULT="Y"


function inform_error()
{
  echo -e "${ERROR_MSG}"
 
  if [ "${ALERT_EMAIL_LAYOUT_DEFAULT}" = "Y" ]; then 
     ALERT_EMAIL_MESSAGE="Error while ${ALERT_EMAIL_SUBJECT} ON  $(date +%Y-%m-%d-%H-%M-%S) <BR/> Error : ${ERROR_MSG}<BR>Please check the logs for further analysis."
  else
    ALERT_EMAIL_MESSAGE="${ERROR_MSG}"
  fi

  inform
 
  print_end_header

  exit 0
}

function inform()
{
   if [ "${ALERT_EMAIL_LAYOUT_DEFAULT}" = "Y" ]; then 
	ALERT_EMAIL_SUBJECT="$ALERT_EMAIL_SUBJECT ON $(date +%Y-%m-%d-%H-%M-%S)"
 
	ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE <BR/><BR/> <i>Note : If any issues or suggetions,"
	ALERT_EMAIL_MESSAGE="$ALERT_EMAIL_MESSAGE then inform us at <a href='mailto:${ALERT_EMAIL_SUPPORT}'>${ALERT_EMAIL_SUPPORT}</a></i> "
   fi

   ##################################################SEND EMAIL OF STATUS START######################

   if [ ! -d "${ALERT_ROOT_PATH}" -o ! -f ${ALERT_ROOT_PATH}/alert.conf  ]; then
        echo -e "\n\nalert configuration not exist so not able to triggeer mail\n"
   else
        . ${ALERT_ROOT_PATH}/alert.conf

	if [ -z "${ALERT_MESSAGE_FILE}" ]; then
           ${ALERT_ROOT_PATH}/sendEmail -s "${MAIL_SERVER}"  -f "${ALERT_EMAIL_FROM}" -t "${ALERT_EMAIL_TO}" -u "${ALERT_EMAIL_SUBJECT}"  -o  message-content-type=html  -m "${ALERT_EMAIL_MESSAGE}"
        else

          if [ "${IS_ALERT_MESSAGE_FILE_ATTACHMENT}" = "Y" ]; then
             if [ "${ALERT_EMAIL_LAYOUT_DEFAULT}" = "Y" ]; then 
                ALERT_EMAIL_MESSAGE="Attachment contains $ALERT_EMAIL_SUBJECT $ALERT_EMAIL_MESSAGE"
	     fi
             ${ALERT_ROOT_PATH}/sendEmail -s "${MAIL_SERVER}"  -f "${ALERT_EMAIL_FROM}" -t "${ALERT_EMAIL_TO}" -u "${ALERT_EMAIL_SUBJECT}"  -o  message-content-type=html  -m "${ALERT_EMAIL_MESSAGE}" -a ${ALERT_MESSAGE_FILE}
          else
              ${ALERT_ROOT_PATH}/sendEmail -s "${MAIL_SERVER}"  -f "${ALERT_EMAIL_FROM}" -t "${ALERT_EMAIL_TO}" -u "${ALERT_EMAIL_SUBJECT}"  -o  message-content-type=html  -o message-file=${ALERT_MESSAGE_FILE}
          fi

        fi
   fi

   ##################################################SEND EMAIL OF STATUS END######################

}

function setCurrentHome()
{
 #Changing Directory to Current Shell Directory
 CURRENT_SHELL_HOME="$(dirname $0)"
 cd "${CURRENT_SHELL_HOME}"
}
#############################################ALERT Functions defined Here END#####################################