#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu_alertsmgt
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: AXP/VAX + PAR
#set -x
VERSION=2.9

. `dirname $0`/charon_common
. `dirname $0`/charon_common_menu

display_header "Alerts management"
echo

CRONFILE=/etc/cron.d/charon

if test `id -u` -ne 0
then
  echo "[31mERROR[0m: must be root !"
  Press_Enter
  echo
  exit 1
fi

#-----------------------------------------------------------------------------
# Files and values definition
#-----------------------------------------------------------------------------
test -d ${CHARONDIR}/utils/events || mkdir ${CHARONDIR}/utils/events
ALCMDFILE="${CHARONDIR}/utils/charon_check.alertcmd"
ALERTLOG=${CHARONDIR}/log/charon_alerts.log
test -e ${ALERTLOG} || touch ${ALERTLOG}

LOGEVENT0="None"
LOGEVENT1="INFORMATION + WARNING + ERROR"
LOGEVENT2="WARNING + ERROR"
LOGEVENT3="ERROR"

#-----------------------------------------------------------------------------
# Pre-init & checks
#-----------------------------------------------------------------------------
if test ! -x /opt/charon/utils/charon_check.alertcmd
then
  cp -f charon_check.alertcmd.example charon_check.alertcmd
  chmod 744 charon_check.alertcmd
  echo "Mail script initialized."
  echo
fi

if test ! -e ${MAILTOFILE}
then
  cat >${MAILTOFILE} <<EOF
#---------------------------------------------------------------
# CHARON alertcmd mail recipients list / one recipient per line
#---------------------------------------------------------------
EOF
fi

#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
function set_mail_mode
{
  echo
  MAILMODE=$(cat ${MAILMODEFILE} 2>/dev/null)
  test "${MAILMODE}" != "TEXT" && MAILMODE="HTML"
  case "${MAILMODE}"
  in
    "TEXT")
      echo HTML >${MAILMODEFILE}
      test $? = 0 && echo "Switched to HTML alerts" || echo "Failed to copy!"
      ;;
    "HTML")
      echo TEXT >${MAILMODEFILE}
      test $? = 0 && echo "Switched to TEXT alerts" || echo "Failed to copy!"
      ;;
  esac
  echo
  if test -e ${MAILTOFILE}
  then
    if test `grep -v ^# ${MAILTOFILE} | wc -l` -gt 0
    then
      while test 1
      do
        MAILTO=`grep -v ^# ${MAILTOFILE} | sort -u | tr "\n" ","|sed "s=,$==g"`
        echo -n "Do you want to send a test mail to ${MAILTO} (y/n) ? "
        read ANS
        echo
        case "${ANS}"
        in
          y)
            TM="${CHARONDIR}/utils/templates/TESTMAIL.mail"
            if test -s ${TM}
            then
              echo "Sending mail, please check your mailbox."
              ${CHARONDIR}/utils/charon_check.alertcmd TESTMAIL
            else
              tput bold
              echo "The test mail template does not exist. Please check"
              tput sgr0
              echo "Excepted file: ${TM}"
            fi
            sleep 2
            break
            ;;
          n)
            break
            ;;
          *)
            tput cuu1;tput el
            ;;
        esac
      done
    fi
  fi
}

function set_recipients_list
{
  echo
  tput bold;tput smul
  echo "Current list of recipients"
  tput rmul;tput sgr0
  if test `grep -v ^# ${MAILTOFILE} | wc -l` -eq 0
  then
    echo "Empty."
  else
    grep -v ^# ${MAILTOFILE}
  fi

  ask_editor
  if test -n "${EDI}"
  then
    echo
    echo "Updating file using '`basename ${EDI}`'..."
    if test -z "`echo ${EDI}|grep vim`"
    then
      ${EDI} ${MAILTOFILE} 2>/dev/null
    else
      ${EDI} -u ${CHARONDIR}/utils/charonrecipient.vimrc ${MAILTOFILE} 2>/dev/null
    fi
    >${MAILTOFILE}.tmp
    cat ${MAILTOFILE} | while read L
    do
      if test -n "$L"
      then
        if test "$L" = "support@stromasys.com" -o "$L" = "platinum@stromasys.com"
        then
          echo "${BGORANGE} WARNING [0m The recipient '$L' is not allowed. Rejected."
        else
          echo $L >>${MAILTOFILE}.tmp
        fi
      fi
    done
    diff ${MAILTOFILE}.tmp ${MAILTOFILE} >/dev/null 2>&1
    test $? -ne 0 && Press_Enter
    mv -f ${MAILTOFILE}.tmp ${MAILTOFILE}
  fi
}

function set_mailfrom
{
  MAILFROM=`cat ${MAILFRFILE} 2>/dev/null`
  echo
  while test 1
  do
    echo -n "Enter the mail sender (From:) name [${MAILFROM}] ('r' to reset): "
    read ANS
    case "${ANS}"
    in
      "")
        break
        ;;
      "r")
        MAILFROM=""
        echo "${MAILFROM}" >${MAILFRFILE}
        break
        ;;
      *)
        MAILFROM="${ANS}"
        echo "${MAILFROM}" >${MAILFRFILE}
        break
        ;;
    esac
  done
}

function set_mail_tag
{
  echo
  get_preferences
  echo "$(tput smul)Note$(tput rmul): the tag is used in email alerts subject between brackets"
  echo
  echo -n "$(tput bold)Current tag$(tput sgr0): "
  if test "${MAILTAG:-CHARON}" = "CHARON"
  then
    echo "[36m${MAILTAG}[0m (default)"
  else
    echo "[36m${MAILTAG}[0m"
  fi
  echo
  echo "Please enter new tag, maximum 20 alphanumeric characters, minus and underscore"
  echo "allowed (leave blank to keep the actual tag)"
  echo
  while test 1
  do
    echo -n "$(tput bold)Enter new tag$(tput sgr0): "
    read ANS
    if test -z "${ANS}"
    then
      break
    else
      if test -n "$(echo "${ANS}" | tr -d [:alnum:]-)"
      then
        echo "${BGORANGE} WARNING [0m Characters not allowed found."
        Press_Enter
        tput cuu1;tput cuu1;tput cuu1;tput ed
      else
        if test $(echo -n "${ANS}" | wc -c) -gt 20
        then
          echo "${BGORANGE} WARNING [0m 20 characters maximum allowed."
          Press_Enter
          tput cuu1;tput cuu1;tput cuu1;tput ed
        else
          MAILTAG=${ANS}
          break
        fi
      fi
    fi
  done
  set_preferences
}

function set_mail_footer
{
  cat <<EOF

[1mMail footer can contain the following HTML tags:[0m
- new line: <br>
- bold: <b> </b>
- fonts: <font ...> </font>

In case the mail format is switched to TEXT, these tags will be removed.

EOF

  ask_editor
  test -e ${MAILFTFILE} || touch ${MAILFTFILE}
  if test -n "${EDI}"
  then
    echo
    echo "Updating file using '`basename ${EDI}`'..."
    ${EDI} ${MAILFTFILE} 2>/dev/null
  fi
}

function reinstall_script
{
  cp ${ALCMDFILE}.example ${ALCMDFILE}
  if test $? = 0
  then
    echo "${FGBLUE}Script reinstalled/initialized from example provided.[0m"
  else
    tput bold
    echo "[31mFailed to copy script example! Please check or contact support.[0m"
    tput sgr0
  fi
}

function set_alert_script
{
  echo
  if test ! -e ${ALCMDFILE}
  then
    if test -e ${ALCMDFILE}.example
    then
      reinstall_script
    fi
  else
    if test -e ${ALCMDFILE}.example
    then
      if test -n "`diff ${ALCMDFILE} ${ALCMDFILE}.example`"
      then
        while test 1
        do
          echo "Do you want to reinstall the alert script from the kit ?"
          echo -n "If you customized it, it will be saved and overwritten (y/n) : "
          read ANS
          case "${ANS}"
          in
            y)
              echo
              mkdir -p ${CHARONDIR}/archive 2>/dev/null
              ADAT=`date +%Y%m%d-%H%M%S`
              echo "${FGBLUE}Archiving current alert script file to[0m"
              echo "${CHARONDIR}/archive/`basename ${ALCMDFILE}`.${ADAT}"

              cp ${ALCMDFILE} ${CHARONDIR}/archive/`basename ${ALCMDFILE}`.${ADAT}
              test $? = 0 && echo "Done." || echo "Failed!"
              echo
              reinstall_script
              break
              ;;
            n)
              break
              ;;
            *)
              tput cuu1
              tput cuu1;tput el
              ;;
          esac
        done
      fi
    fi
  fi

  chmod u+x ${ALCMDFILE}

  while test 1
  do
    echo -n "Do you want to edit the alert script (y/n) ? "
    read ANS
    case "${ANS}"
    in
      y|Y)
        ask_editor
        if test -n "${EDI}"
        then
          echo
          echo "Updating file using '`basename ${EDI}`'..."
          ${EDI} ${ALCMDFILE} 2>/dev/null
        fi
        break
        ;;
      n|N)
        break
        ;;
      *)
        tput cuu1;tput ed
        ;;
    esac
  done
}

function set_alertslimitation
{
  while test 1
  do
    MAXDP=$(echo ${MAXALERTSINTERVAL}/60 | bc)
    echo -n "Interval in minutes [${MAXDP}] (0=disabled): "
    read ANS
    test -z "${ANS}" && ANS=${MAXDP}
    if test -n "$(echo ${ANS} | tr -d [:digit:])"
    then
      echo "${FGORANGE}The value must be numeric only.[0m"
    else
      ANS=$(echo ${ANS} | bc)
      if test ${ANS} -lt 0
      then
        echo "${FGORANGE}The value must be greater or equivalent to 0.[0m"
      elif test ${ANS} -gt 120
      then
        echo "${FGORANGE}The value must be lower or equivalent to 120.[0m"
      else
        MAXALERTSINTERVAL=$(echo ${ANS}*60 | bc)
        set_preferences
        break
      fi
    fi
  done
  if test ${MAXALERTSINTERVAL} -gt 0
  then
    while test 1
    do
      echo -n "Number of alerts during the interval [${MAXALERTSCOUNT}] : "
      read ANS
      test -z "${ANS}" && ANS=${MAXALERTSCOUNT}
      if test -n "$(echo ${ANS} | tr -d [:digit:])"
      then
        echo "${FGORANGE}The value must be numeric only.[0m"
      else
        ANS=$(echo ${ANS} | bc)
        if test ${ANS} -lt 5
        then
          echo "${FGORANGE}The value must be greater or equivalent to 5.[0m"
        elif test ${ANS} -gt 100
        then
          echo "${FGORANGE}The value must be lower or equivalent to 100.[0m"
        else
          MAXALERTSCOUNT=${ANS}
          set_preferences
          break
        fi
      fi
    done
  fi
}

function set_log_alert_level
{
  while test 1
  do
    cat <<EOF

[44m[37mAvailable levels[0m
0 = None
1 = INFORMATION + WARNING + ERROR
2 = WARNING + ERROR
3 = ERROR

EOF
    echo -n "Select the alerts level you want to receive from the log files [${LOGSEVERITYALERT}] : "
    read ANS
    echo
    test -z "${ANS}" && ANS=${LOGSEVERITYALERT}

    case "${ANS}"
    in
      0)
        LOGSEVERITYALERT=${ANS}
        set_preferences
        break
        ;;
      1|2|3)
        LOGSEVERITYALERT=${ANS}
        set_preferences
        CHK=`cat ${CRONFILE} 2>/dev/null | grep ${CHARONDIR}/utils/charon_logevent | grep -v ^#`
        if test -z "${CHK}"
        then
          tput bold
          echo "Schedule (crontab) not set for charon log events!"
          echo "Please update schedule using menu option 'Manage recursive jobs'"
          tput sgr0
          echo
          echo "Add a line as below to run every 1 minute:"
          echo
          echo "* * * * * root /opt/charon/utils/charon_logevent"
          Press_Enter
        fi
        break
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done
}

function tail_alertslog
{
  tput clear
  echo "[44m[37m Continuous view of ${ALERTLOG} [0m"
  echo "[31mPress CTRL-C to stop[0m"
  if test "${SEMIGRAPH}" = "enabled"
  then
    tput smacs
    echo "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
    tput rmacs
  else
    echo "--------------------------------------------------------------------------------"
  fi
  typeset -i L2=0
  typeset -i LN=0
  typeset -i TAILN=0
  L2=`grep -n ":ALERT:" ${ALERTLOG} | tail -2 | head -1 | cut -f1 -d:`
  if test ${L2} -eq 0
  then
    TAILN=0
  else
    LN=`wc -l ${ALERTLOG} | awk '{print $1}'`
    TAILN=${LN}-${L2}+1
  fi

  trap 'echo;echo "[44m[37m Continuous view of ${ALERTLOG} aborted [0m";sleep 1;echo' 1 2 9 14 15
  tail -Fn${TAILN} --retry ${ALERTLOG} | while read LINE
  do
    if test "`echo ${LINE} | cut -c1-6`" != "------"
    then
      if test "`echo ${LINE} | cut -f3 -d:`" = "ALERT"
      then
        LC="[44m[37m"
        test -n "`echo ${LINE} | grep -w started$`" && LC="[42m[30m"
        test -n "`echo ${LINE} | grep -w failed$`"  && LC="[40m[31m"
        test -n "`echo ${LINE} | grep -w stopped$`" && LC="[41m[37m"
        test -n "`echo ${LINE} | grep -w removed$`" && LC="[45m[37m"
        echo "${LC}${LINE}[0m"
      else
        echo "${LINE}" | sed -e "s=\&nbsp;==g" \
                             -e "s=<hr.*>=----------------------------------------=g" \
                             -e "s= INFORMATIONAL$= [36mINFORMATIONAL[0m=g" \
                             -e "s= CRITICAL$= [31m[1mCRITICAL[0m=g" \
                             -e "s= MAJOR$= [31mMAJOR[0m=g" \
                             -e "s= MINOR$= ${FGORANGE}MINOR[0m=g" \
                             -e "s= Minor$= ${FGORANGE}MINOR[0m=g" \
                             -e "s= WARN$= ${FGORANGE}WARNING[0m=g" \
                             -e "s= WARNING$= ${FGORANGE}WARNING[0m=g"  \
                             -e "s= ERROR$= [31mERROR[0m=g"
      fi
    else
      echo
    fi
  done
  trap 'exit' 1 2 9 14 15
}

function view_alerts
{
  echo
  if test -x ${CHARONDIR}/utils/charon_check.alertcmd
  then
    if test -z "`grep '>>${ALERTLOG}' ${CHARONDIR}/utils/charon_check.alertcmd`"
    then
      echo "[31m[1mThe alert script does not contain the required lines:"
      tput sgr0
      cat <<EOF
If you have not customized the script, please use the menu option
'Update/reinstall common alert script' and reinstall from the example
provided in the kit

EOF
      echo
      Press_Enter
      return
    fi
  else
    echo "[31m[1mThe alert script is not found or not executable:"
    tput sgr0
    echo 
    echo -n "# "
    tput bold
    echo "ls -l ${CHARONDIR}/utils/charon_check.alertcmd"
    tput sgr0
    ls -l ${CHARONDIR}/utils/charon_check.alertcmd
    echo
    Press_Enter
    return
  fi

  while test 1
  do
    while test 1
    do
      display_header "Alerts management"
      NBAL=`grep ":ALERT:" ${ALERTLOG} 2>/dev/null | wc -l`
      cat <<EOF

${FGBLUE}Number of alerts:[0m ${NBAL} at `date +"%d-%b-%Y %H:%M:%S"`

1 - Continuous view of alerts log
2 - View alerts log (using editor)
q - quit

EOF
      echo -n "Enter your choice: "
      read ANS
      case "${ANS}"
      in
        1)
          tail_alertslog
          break
          ;;
        2)
          if test -s ${ALERTLOG}
          then
            ask_editor
            if test -n "${EDI}"
            then
              ${EDI} ${ALERTLOG} 2>/dev/null
            fi        
          else
            echo "Log file is currently empty or does not exist."
            Press_Enter
          fi
          break
          ;;
        q)
          break 2
          ;;
        *)
          ;;
      esac
    done
  done
}

function alert_simulator
{
  test -d ${CHARONDIR}/utils/events || mkdir ${CHARONDIR}/utils/events

  if test $(grep -v ^# ${MAILTOFILE} | wc -l) -eq 0
  then
    echo "${FGORANGE}Note[0m: No email recipient defined."
  fi

  ${CHARONDIR}/utils/charon_check.alertcmd TESTMAIL
  if test $? -eq 0
  then
    echo "Alert sent. Please check."
  else
    echo "${FGORANGE}Failed[0m"
  fi
  Press_Enter
}


#-----------------------------------------------------------------------------
# Main menu
#-----------------------------------------------------------------------------
while test 1
do
  get_preferences
  display_header "Alerts management"

  . `dirname $0`/charon_common reload
  MAILMODE=$(cat ${MAILMODEFILE} 2>/dev/null)
  test "${MAILMODE}" != "TEXT" && MAILMODE="HTML"

  echo -n " 1- Mail mode (text/html): "
  echo "[36m${MAILMODE}[0m"

  echo -n " 2- Mail recipients list: "
  if test `grep -v ^# ${MAILTOFILE} 2>/dev/null | wc -l` -eq 0
  then
    echo "Empty."
  else
    L1=0
    grep -v ^# ${MAILTOFILE} | head -4 | while read L
    do
      test ${L1} = 1 && tput hpa 26
      test ${L1} = 0 && L1=1
      echo "[36m${L}[0m"
    done
    if test `grep -v ^# ${MAILTOFILE} | wc -l` -gt 4
    then
      tput hpa 26
      echo "[36m(truncated...)[0m"
    fi
  fi

  echo -n " 3- Mail sender (optional): "
  MAILFROM=`cat ${MAILFRFILE} 2>/dev/null`
  if test -z "${MAILFROM}"
  then
    echo "(default)"
  else
    echo "[36m${MAILFROM}[0m"
  fi

  echo -n " 4- Mail subject tag: "
  if test "${MAILTAG:-CHARON}" = "CHARON"
  then
    echo "[36m${MAILTAG}[0m (default)"
  else
    echo "[36m${MAILTAG}[0m"
  fi

  echo -n " 5- Mail footer: "
  if test -s ${MAILFTFILE}
  then
    L1=0
    head -4 ${MAILFTFILE} | while read L
    do
      test ${L1} = 1 && tput hpa 17
      test ${L1} = 0 && L1=1
      echo "[36m$(echo ${L} | cut -c1-60)[0m"
    done
    if test `cat ${MAILFTFILE} | wc -l` -gt 4
    then
      tput hpa 17
      echo "[36m(truncated...)[0m"
    fi
  else
    echo "Not defined"
  fi

  echo -n " 6- 'wall' alert messages: "
  if test "${WALLALERTS}" = "disabled"
  then
    echo "disabled"
  else
    echo "[36menabled[0m"
  fi

  echo -n " 7- Update/reinstall alert script: "
  if test -e ${ALCMDFILE}
  then
    if test -e ${ALCMDFILE}.example
    then
      if test -n "`diff ${ALCMDFILE} ${ALCMDFILE}.example`"
      then
        ALCMDP=`grep -n charon_check.alertcmd ${ALCMDFILE} | cut -f1 -d:`
        if test ${ALCMDP:-0} -eq 3
        then
          ALCMDV=`grep ^VERSION= ${ALCMDFILE} | cut -f2 -d'='`
          echo "${FGORANGE}Installed but previous version ${ALCMDV}"
        else
          echo "${FGBLUE}Customized"
          tput sgr0
          tput hpa 36
          A1=`ls -l --full-time ${ALCMDFILE}`
          A2=`echo ${A1} | awk '{print $6,$7}' | awk -F '.' '{print $1}'`
          echo -n "Last update: ${FGBLUE}"
          date +"%a %d-%b-%Y %H:%M:%S" -d "${A2}"
        fi
      else
        ALCMDV=`grep ^VERSION= ${ALCMDFILE} | cut -f2 -d'='`
        echo "[36mInstalled from kit, script V${ALCMDV}"
      fi
    else
      echo "[36minstalled/customized"
      tput sgr0
      tput hpa 40
      A1=`ls -l --full-time ${ALCMDFILE}`
      A2=`echo ${A1} | awk '{print $6,$7}' | awk -F '.' '{print $1}'`
      echo -n "Last update: ${FGBLUE}"
      date +"%a %d-%b-%Y %H:%M:%S" -d "${A2}"
    fi
  else
    if test -e ${ALCMDFILE}.example
    then
      cp -f ${ALCMDFILE}.example ${ALCMDFILE}
      test $? = 0 && echo "[32mInstalled (just now)" || echo "${FGORANGE}Failed to install"
    else
      echo "${FGORANGE}[1mSource not found!"
    fi
  fi
  tput sgr0

  echo -n " 8- Guests log alert level: "
  eval LOGEVENT=\${LOGEVENT${LOGSEVERITYALERT}}
  test ${LOGSEVERITYALERT:-0} = 0 && echo -n "${FGORANGE}"
  echo "[36m${LOGEVENT}[0m"
  tput sgr0

  echo -n " 9- Reduce WARN msgs on dongle removal: "
  if test "${WARNONDONGLEREMOVAL}" = "reduced"
  then
    if test "${GLOBALLOGMON}" = "enabled"
    then
      echo "[36mreduced[0m"
    else
      echo "[90mdisabled (global monitoring)[0m"
    fi
  else
    echo "no (all messages)"
  fi

  echo -n "10- Alerts limitation: "
  if test ${MAXALERTSINTERVAL} = 0
  then
    echo "disabled"
  else
    MAXDP=$(echo ${MAXALERTSINTERVAL}/60 | bc)
    echo "[36mMax ${MAXALERTSCOUNT} alert$(test ${MAXALERTSCOUNT} -gt 1 && echo 's') in ${MAXDP} minutes[0m"
  fi     

  NBAL=`grep ":ALERT:" ${ALERTLOG} 2>/dev/null | wc -l`
  echo -n "11- View alerts history (${NBAL} alerts)"
  A1=`ls -l --full-time ${ALERTLOG} 2>/dev/null`
  if test -n "${A1}"
  then
    A2=`echo ${A1} | awk '{print $6,$7}' | awk -F '.' '{print $1}'`
    echo -n ". [36mLast update: "
    date +"%a %d-%b-%Y %H:%M:%S" -d "${A2}"
  else
    echo ""
  fi
  tput sgr0

  echo    "12- Alert simulator"

  echo -n "13- Alert on console connection/disconnection: "
  if test "${MONCONSOLECON}" = "enabled"
  then
    if test "${GLOBALLOGMON}" = "enabled"
    then
      echo -n "[36menabled[0m"
    else
      echo -n "[90mdisabled (global monitoring)[0m"
    fi
    if test -e ${MONUSBALTDISABLED}
    then
      echo ", ${FGORANGE}alerts paused[0m"
    else
      echo
    fi
  else
    echo "disabled"
  fi

  if test "${KITP}" = "AXP/VAX"
  then
    #--- Charon-AXP/VAX only
    echo -n "14- Hardware specs warning message: "
    if test "${IGNOREHWSPECWARN}" = "enabled"
    then
      echo "disabled"
    else
      if test "${GLOBALLOGMON}" = "enabled"
      then
        echo "[36menabled[0m"
      else
        echo "[90mdisabled (global monitoring)[0m"
      fi
    fi

    echo -n "15- Consider BUGCHECK msgs as WARNING: "
    if test "${MONBUGCHECK}" = "enabled"
    then
      if test "${GLOBALLOGMON}" = "enabled"
      then
        echo "[36menabled[0m"
      else
        echo "[90mdisabled (global monitoring)[0m"
      fi
    else
      echo "disabled"
    fi

    echo -n "16- Consider CPU Halted msgs as WARNING: "
    if test "${MONCPUHALTED}" = "enabled"
    then
      if test "${GLOBALLOGMON}" = "enabled"
      then
        echo "[36menabled[0m"
      else
        echo "[90mdisabled (global monitoring)[0m"
      fi
    else
      echo "disabled"
    fi

    echo -n "17- Alert on F6 key pressed/OPA0: "
    if test "${MONCONSOLEF6}" = "enabled"
    then
      if test "${GLOBALLOGMON}" = "enabled"
      then
        echo "[36menabled[0m"
      else
        echo "[90mdisabled (global monitoring)[0m"
      fi
    else
      echo "disabled"
    fi
  fi

  echo
  echo -n "${FGBLUEBOLD}Enter your choice ('q' to quit): [0m"
  read ANS
  case "${ANS}"
  in
    1)
      #--- Mail mode (text/html)
      set_mail_mode
      ;;
    2)
      #--- Mail recipients list
      set_recipients_list
      ;;
    3)
      #--- Mail sender
      set_mailfrom
      ;;
    4)
      #--- Set email subject tag
      set_mail_tag
      ;;
    5)
      #--- Create/Update alert mail footer
      set_mail_footer
      ;;
    6)
      #--- Enable/disable wall alert messages
      if test "${WALLALERTS}" = "enabled"
      then
        WALLALERTS="disabled"
      else
        WALLALERTS="enabled"
      fi
      set_preferences
      ;;
    7)
      #--- Update/reinstall alert script
      set_alert_script
      ;;
    8)
      #--- Select guests log alert level
      set_log_alert_level
      ;;
    9)
      if test "${WARNONDONGLEREMOVAL}" = "reduced"
      then
        WARNONDONGLEREMOVAL="all"
      else
        WARNONDONGLEREMOVAL="reduced"
        cat <<EOF12

The following messages will not generate an alert:

- :0000024D:licenseman(1830): Unable to log in to the key "XXXXXXXX", feature X.
- :0000024D:licenseman(1830): HASP runtime (7): Sentinel protection key not available.
- :0000024D:licenseman(1830): Failed to login at the Sentinel HASP key: "XXXXXXXX".

EOF12
        Press_Enter
      fi
      set_preferences
      ;;
    10)
      #--- Alerts limitation
      set_alertslimitation
      ;;
    11)
      #--- View alerts history
      view_alerts
      ;;
    12)
      #--- Alerts simulator
      alert_simulator
      ;;
    13)
      #--- Console connection/disconnection
      if test "${MONCONSOLECON}" = "enabled"
      then
        MONCONSOLECON="disabled"
      else
        MONCONSOLECON="enabled"
      fi
      set_preferences
      ;;
    14)
      if test "${KITP}" = "AXP/VAX"
      then
        #--- Hardware specifications warning messages
        if test "${IGNOREHWSPECWARN}" = "enabled"
        then
          IGNOREHWSPECWARN="disabled"
        else
          IGNOREHWSPECWARN="enabled"
          echo
          echo "Messages could be still present in the log file depending on Charon"
          echo "version but alerts won't be sent."
          echo
          Press_Enter
        fi
        set_preferences
      else
        tput cuu1;tput el
      fi
      ;;
    15)
      if test "${KITP}" = "AXP/VAX"
      then
        if test "${MONBUGCHECK}" = "enabled"
        then
          MONBUGCHECK="disabled"
        else
          MONBUGCHECK="enabled"
        fi
        set_preferences
      else
        tput cuu1;tput el
      fi
      ;;
    16)
      if test "${KITP}" = "AXP/VAX"
      then
        if test "${MONCPUHALTED}" = "enabled"
        then
          MONCPUHALTED="disabled"
        else
          MONCPUHALTED="enabled"
        fi
        set_preferences
      else
        tput cuu1;tput el
      fi
      ;;
    17)
      if test "${KITP}" = "AXP/VAX"
      then
        if test "${MONCONSOLEF6}" = "enabled"
        then
          MONCONSOLEF6="disabled"
        else
          MONCONSOLEF6="enabled"
        fi
        set_preferences
      fi
      ;;
    q|Q)
      break
      ;;
    *)
      tput cuu1;tput el
      ;;
  esac
done

exit
