#!/bin/bash

usage()
{
  printf "Usage: %s " "unraid_notify.sh [-e event] [-s subject] [-d description] [-i normal|warning|alert] [-m message] [-t]
  create a notification
  use -e to specify the event
  use -s to specify a subject 
  use -d to specify a short description
  use -i to specify the severity
  use -m to specify a message (long description)
  use -x to create a single notification ticket
  use -t to force send email only (for testing)
  all options are optional
"
  exit 0
}

set_variable()
{
  local varname=$1
  shift
  if [ -z "${!varname}" ]; then
    eval "$varname=\"$@\""
  else
    echo "Error: $varname already set"
    usage
  fi
}

####################################################################
# Main script starts here

if [[ $1 == "" ]]; then
    usage;
    exit;
else
  while getopts ':e:s:d:i:m:p:?h' c
  do
    case $c in
      e) set_variable NOTIF_EVENT "$OPTARG" ;;
      s) set_variable NOTIF_SUBJECT "$OPTARG" ;;
      d) set_variable NOTIF_DESCRIPTION "$OPTARG" ;;
      m) set_variable NOTIF_MESSAGE "$OPTARG" ;;
      i) set_variable NOTIF_PRIORITY "$OPTARG" ;;
      h|?) usage ;;
      *)  usage ;;
      esac
  done
fi
set_variable PROGNAME "$(basename "$0")"
set_variable NOTIF_COMMAND "/usr/local/emhttp/webGui/scripts/notify"
    
eval "$NOTIF_COMMAND${NOTIF_PRIORITY:+ -i '$NOTIF_PRIORITY'}${NOTIF_SUBJECT:+ -s '$NOTIF_SUBJECT'}${NOTIF_DESCRIPTION:+ -d '$NOTIF_DESCRIPTION'}${NOTIF_MESSAGE:+ -m '$NOTIF_MESSAGE'}${NOTIF_EVENT:+ -e '$NOTIF_EVENT'}"
