#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: AXP/VAX + PAR
#
#set -x
VERSION=2.5
test $TERM = "putty" && export TERM=xterm

FGORANGE="[38;5;208m"
BGORANGE="[30;48;5;208m"

#=============================================================================
# Function: Press_Enter
#=============================================================================
function Press_Enter
{
  echo
  echo -n "[44m[37mPress enter[0m"
  read ENTER
  tput cuu1;tput ed
}

IS_SYSTEMD=0
which systemctl >/dev/null 2>&1 && IS_SYSTEMD=1
if test ${IS_SYSTEMD} = 0
then
  tput bold
  echo "Operating systems with 'systemd' only are supported."
  tput sgr0
  exit
fi

if test $(id -u) -ne 0
then
  if test -n "$(grep ^ENABLESUDO=1$ ${PREFFILE} 2>/dev/null)"
  then
    sudo $0
    exit
  fi
fi

#--- Remove /etc/sysctl.d/charon.conf file if any (before V2.0)
if test -e /etc/sysctl.d/charon.conf
then
  if test -n "$(head -1 /etc/sysctl.d/charon.conf|grep '^#-- Updated on')"
  then
    rm -f /etc/sysctl.d/charon.conf 2>/dev/null
  fi
fi

#--- Check if 'atd' service is enabled
ATDX=0
systemctl -q is-enabled atd 2>/dev/null
if test $? -ne 0
then
  echo "${BGORANGE} WARNING $(tput sgr0)"
  echo "${FGORANGE}'atd' service is not enabled. Please enable it.$(tput sgr0)"
  ATDX=1
fi

#--- Check if 'atd' service is active
systemctl -q is-active atd 2>/dev/null
if test $? -ne 0
then
  test ${ATDX} -eq 1 && echo
  tput bold
  echo "${BGORANGE} WARNING $(tput sgr0)"
  echo "${FGORANGE}'atd' service is not running. Please start it.$(tput sgr0)"
  tput sgr0
  ATDX=1
fi

test ${ATDX} -eq 1 && Press_Enter

export PERL5LIB=${PERL5LIB}:/opt/charon/utils
RUNMENU="./menu_mod.pl -menu=charon_menumain.dat"

#--- Check if crontab is updated (no more root's crontab but /etc/cron.d/charon)
crontab -l 2>/dev/null | grep -v '#' | grep -q charon_
if test $? -eq 0
then
  tput bold
  echo "${BGORANGE} WARNING $(tput sgr0)"
  echo "${FGORANGE}The recurring jobs in the toolkit are no more located in the root's crontab.$(tput sgr0)"
  echo "Please delete or comment the related entries (using 'crontab -e') then use the"
  echo "'Manage recurring jobs' option from the 'menu' to create these entries in"
  echo "the '/etc/cron.d/charon' file."
fi

#--- Start the menu (only one execution allowed at a time)
CHK=$(ps -eo pid,command | grep -w perl | grep "${RUNMENU}$")
if test -z "${CHK}"
then
  cd /opt/charon/utils
  . ./charon_common

  test -e ${ETCCHARON} || mkdir ${ETCCHARON}

  #--- Experimental mode flag file moved since 2.0
  if test -e /root/.charon.experimental
  then
    mv -f /root/.charon.experimental ${EXPERIMENTALMODE}
  fi

  #--- Added for kit AXP/VAX 1.58: preference file location was moved from /root to /opt/charon/utils
  if test -e /root/.charon.preferences
  then
    mv -f /root/.charon.preferences ${PREFFILE}
  fi

  #--- Added for kit AXP/VAX 1.61 and PAR 1.5: preference file location has moved to /etc/charon.d and some flag files are now in this common file
  if test -e /opt/charon/utils/charon.preferences
  then
    mv -f /opt/charon/utils/charon.preferences ${PREFFILE}
  fi
  if test -e ${ETCCHARON}/CharonToolkit.settings
  then
    mv -f ${ETCCHARON}/CharonToolkit.settings ${PREFFILE}
  fi

  get_preferences
  #--- Common parameters migration from old Toolkit versions
  if test -s /root/.charonlogevent
  then
    LOGSEVERITYALERT=$(cat /root/.charonlogevent)
    rm -f /root/.charonlogevent 2>/dev/null
  fi
  if test -e /root/.charon.less.warn.on.dongle.removal
  then
    WARNONDONGLEREMOVAL="reduced"
    rm -f /root/.charon.less.warn.on.dongle.removal 2>/dev/null
  fi
  if test -e /root/.charon.disable.monusb.immediate.alert
  then
    rm -f /root/.charon.disable.monusb.immediate.alert
  fi
  if test -e /root/.charonwalloff
  then
    WALLALERTS="disabled"
    rm -f /root/.charonwalloff
  fi

  #--- AXP/VAX specific parameters migration from old Toolkit versions (pre-2.0)
  if test "${KITP}" = "AXP/VAX"
  then
    if test -e /root/.charon.expiration.alertsfromlog
    then
      rm -f /root/.charon.expiration.alertsfromlog 2>/dev/null
    fi
    if test -e /root/.charon.hwspecwarning.ignore
    then
      IGNOREHWSPECWARN="enabled"
      rm -f /root/.charon.hwspecwarning.ignore 2>/dev/null
    fi
    if test -e /root/.charon.logmon.no.bugcheck
    then
      MONBUGCHECK="enabled"
      rm -f /root/.charon.logmon.no.bugcheck 2>/dev/null
    fi
    if test -e /root/.charon.logmon.no.cpuhalted
    then
      MONCPUHALTED="enabled"
      rm -f /root/.charon.logmon.no.cpuhalted 2>/dev/null
    fi
  fi

  set_preferences

  test -e ${CHARONDIR}/utils/charon_check.mailto     && mv -f ${CHARONDIR}/utils/charon_check.mailto     ${MAILTOFILE}
  test -e ${CHARONDIR}/utils/charon_check.mailfrom   && mv -f ${CHARONDIR}/utils/charon_check.mailfrom   ${MAILFRFILE}
  test -e ${CHARONDIR}/utils/charon_check.mailfooter && mv -f ${CHARONDIR}/utils/charon_check.mailfooter ${MAILFTFILE}
  test -e /root/.charon.recipient                    && mv -f /root/.charon.recipient                    ${MAILRECIPFILE}
  test -e /root/.charon.lastrecipient                && mv -f /root/.charon.lastrecipient                ${MAILLASTRECIPFILE}
  test -e /root/.charonmailmode                      && mv -f /root/.charonmailmode                      ${MAILMODEFILE}
  test -e /root/.charonkitfolder                     && mv -f /root/.charonkitfolder                     ${CHARONKITFOLDER}
  test -e /root/.charonlicfolder                     && mv -f /root/.charonlicfolder                     ${CHARONLICFOLDER}
  rm -f /root/.charon.last.vmused* 2>/dev/null

  #--- Configure the menu according to license type used (HASP and/or VE)
  . ${CHARONDIR}/utils/charon_setlicmenu

  #--- Check if alert command file has to be updated (upgrade)
  ALCMDFILE="${CHARONDIR}/utils/charon_check.alertcmd"
  if test -e ${ALCMDFILE}
  then
    if test -e ${ALCMDFILE}.example
    then
      if test -n "$(diff ${ALCMDFILE} ${ALCMDFILE}.example)"
      then
        #--- If alert script exists and if different from template (example)
        ALCMDP=$(grep -n charon_check.alertcmd ${ALCMDFILE} | cut -f1 -d:)
        if test ${ALCMDP:-0} -eq 3
        then
          #--- If current alert script is based on example and versions differ (upgrade case)
          #    then display a warning
          ALCMDV=$(grep ^VERSION= ${ALCMDFILE} | cut -f2 -d'=')
          ALEXAV=$(grep ^VERSION= ${ALCMDFILE}.example | cut -f2 -d'=')
          if test "${ALCMDV}" != "${ALEXAV}"
          then
            echo "${BGORANGE} WARNING $(tput sgr0)"
            echo "${FGORANGE}Alert script is installed but with previous version ${ALCMDV}$(tput sgr0)"
            echo "${FGORANGE}Please update the script to version ${ALEXAV} from the 'Alerts management'$(tput sgr0)"
            echo "${FGORANGE}menu option.$(tput sgr0)"
            Press_Enter
          fi
        fi
      fi
    fi
  else
    #--- If alert script does not exist, copy from template (example)
    if test -e ${ALCMDFILE}.example
    then
      cp -f ${ALCMDFILE}.example ${ALCMDFILE}
    fi
  fi

  rm -f /tmp/charon_menu_logchk.alert.version 2>/dev/null
  cat /opt/charon/utils/charon_gstart.boot 2>/dev/null | while read LINE
  do
    CFG=$(echo ${LINE} | cut -f2 -d';')
    CFG=$(basename ${CFG} | sed "s=\.cfg$==g")
    if test "$(systemctl is-active charon_logmon_${CFG}.service 2>/dev/null)" = "active"
    then
      PRC=$(systemctl show -p ExecMainPID charon_logmon_${CFG}.service 2>/dev/null | cut -f2 -d'=')
      if test -n "$(ps --ppid ${PRC} | grep -w tail)"
      then
        LOGVER=$(grep ^VERSION= ${CHARONDIR}/utils/charon_logchk | cut -f2 -d '=')
        if test "${LOGVER}" != "$(cat /var/run/charon_logchk.vm.${CFG} 2>/dev/null)"
        then
          touch /tmp/charon_menu_logchk.alert.version 2>/dev/null
        fi
      fi
    fi
  done
  if test -e /tmp/charon_menu_logchk.alert.version
  then
    echo "${BGORANGE} WARNING [0m"
    echo "${FGORANGE}Log monitoring script version is outdated. Please restart all log monitoring[0m"
    echo "${FGORANGE}services using 'Manage monitored logs and services' menu option.[0m"
    rm -f /tmp/charon_menu_logchk.alert.version 2>/dev/null
    Press_Enter
  fi

  #--- Check entries in the /etc/cron.d/charon file
  ${CHARONDIR}/utils/charon_menu_cron -check

  #--- Run the menu
  ${RUNMENU}
  cd - >/dev/null

else
  PID=$(echo ${CHK} | awk '{print $1}')
  STR=$(ps -hp ${PID} -o start)
  TTY=$(ps -hp ${PID} -o tty)
  echo "${BGORANGE} WARNING [0m"
  echo "${FGORANGE}Another process is already running the 'menu'.[0m"
  echo "Please disconnect this session first."
  echo "Pid: ${PID} on ${TTY}, started: ${STR}."
fi

exit
