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

. /opt/charon/utils/charon_common
. /opt/charon/utils/charon_common_menu

display_header "Edit license server configuration file"
CFG=/opt/license-server/config.ini

if test -s ${CFG}
then
  ask_editor
  if test -n "${EDI}"
  then
    if test -z "$(echo ${EDI}|grep vim)"
    then
      ${EDI} ${CFG} 2>/dev/null
    else
      ${EDI} -u ${CHARONDIR}/utils/charoncfg.vimrc ${CFG} 2>/dev/null
    fi
  fi
else
  echo "Configuration file '${CFG}' does not exist or is empty."
  Press_Enter
fi

SVCSTA=$(systemctl is-active licensed)
if test "${SVCSTA}" = "active"
then
  UPDCFG=$(date +%s -r ${CFG})
  STASVC=$(date +%s -d "$(systemctl show -p ExecMainStartTimestamp licensed | cut -f2 -d'=')")
  if test ${UPDCFG:-0} -gt ${STASVC:-0}
  then
    echo 
    echo "${FGORANGE}WARNING[0m: configuration file update time is greater than the 'licensed'"
    echo "         service startup time!"
    echo
    while test 1
    do
      echo -n "Do you want to restart the service now (y/n) ? "
      read ANS
      case "${ANS}"
      in
        y|Y)
          systemctl restart licensed
          sleep 1
          systemctl --no-pager --lines=8 status licensed
          Press_Enter
          break
          ;;
        n|N)
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  fi
else
  echo "INFO: 'licensed' service state is '${SVCSTA}'"
  sleep 1
fi

exit
