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

function The_End
{
  tput rmacs
  tput sgr0
  echo "$1"
  exit
}

trap 'echo;The_End "Aborted."' 1 2 9 14 15

#--- Not executed as the script can run separately from the toolkit
#. `dirname $0`/charon_common
#. `dirname $0`/charon_common_menu

CHARONDIR=/opt/charon
ETCCHARON=/etc/charon.d
PREFFILE=${ETCCHARON}/CharonToolkit.config
HASPLMINI="/etc/hasplm/hasplm.ini"
HASPERRLOG="/var/hasplm/error.log"
FGORANGE="[38;5;208m"
SEMIGRAPH="enabled"
test "$1" = "-noalt" && SEMIGRAPH="disabled"

ACCREM="NEW"

#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
function Press_Enter
{
  echo
  echo -n "[44m[37mPress enter[0m"
  read ENTER
  tput cuu1;tput el
}

function display_header
{
  #get_preferences
  tput clear
  DISPV=`grep ^VERSION $0| cut -f2 -d'='`
  if test "${SEMIGRAPH}" = "enabled"
  then
    tput smacs
    echo -n "lq"
    tput rmacs
    echo -n "[44m[37mSTROMASYS - Midrange System Emulation[0m"
    tput smacs
    echo "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
    tput cup 0 71
    if test -n "${DISPV}"
    then
      tput rmacs
      echo "${FGBLUE}V${DISPV}[0m"
      tput smacs
    fi
    DISPHEAD=`echo "$1                                                                               " | cut -c1-76`
    echo -n "x "
    tput rmacs
    echo -n "${DISPHEAD}"
    tput smacs
    echo " x"
    echo "mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
    tput rmacs
  else
    echo -n "+-"
    echo -n "[44m[37mSTROMASYS - Midrange System Emulation[0m"
    echo "----------------------------------------+"
    tput cup 0 71
    test -n "${DISPV}" && echo "${FGBLUE}V${DISPV}[0m"
    DISPHEAD=`echo "$1                                                                               " | cut -c1-76`
    echo "| ${DISPHEAD} |"
    echo "+------------------------------------------------------------------------------+"
  fi
  PENDCHG=0
  test "${DEFaccremote}"         = "${accremote}"         || PENDCHG=1
  test "${DEFerrorlog}"          = "${errorlog}"          || PENDCHG=1
  test "${DEFrotatelog}"         = "${rotatelogs}"        || PENDCHG=1
  test "${DEFerror_log_maxsize}" = "${error_log_maxsize}" || PENDCHG=1
  test "${DEFzip_logs_days}"     = "${zip_logs_days}"     || PENDCHG=1
  test "${DEFdelete_logs_days}"  = "${delete_logs_days}"  || PENDCHG=1
  test "${DEFaccesstoremote}"    = "${accesstoremote}"    || PENDCHG=1
  test "${DEFaccessfromremote}"  = "${accessfromremote}"  || PENDCHG=1
  test "${DEFbind_local_only}"   = "${bind_local_only}"   || PENDCHG=1
  if test ${PENDCHG} -eq 1
  then
    tput cuu1
    tput hpa 60
    echo "$(tput smso)${FGORANGE} Pending changes [0m"
  fi


}

function hasp_ini_7old_default
{
  ACCREM="OLD"
  echo "Initializing '${HASPLMINI}' (7.x driver [with x<100]) ..."
  cat <<HASPINI7OLD >${HASPLMINI}
;*************************************************************************
;*
;* Sentinel License Manager configuration file
;*
;* Version 20.3 1.74357 at $(hostname)
;* $(date)
;*
;*************************************************************************



[SERVER]
name = $(hostname)
idle_session_timeout_mins = 720
pagerefresh = 3
linesperpage = 20
ACCremote = 0
enablehaspc2v = 0
old_files_delete_days = 90

enabledetach = 0
reservedseats = 0
reservedpercent = 0
detachmaxdays = 14
commuter_delete_days = 7
disable_um = 0

requestlog = 0
loglocal = 0
logremote = 0
logadmin = 0
errorlog = 0
rotatelogs = 0
access_log_maxsize = 0 ;kB
error_log_maxsize = 0 ;kB
zip_logs_days = 0
delete_logs_days = 0
pidfile = 0
passacc = 0

accessfromremote = 1
accesstoremote = 1
bind_local_only = 0  ; 0=all adapters, 1=localhost only


[UPDATE]
download_url = www.safenet-inc.com/hasp/language_packs/end-user
update_host = www3.safenet-inc.com
language_url = /hasp/language_packs/end-user/


[REMOTE]
broadcastsearch = 1
aggressive = 0
serversearchinterval = 30


[ACCESS]


[USERS]


[VENDORS]


[EMS]
emsurl = http://localhost:8080
emsurl = http://127.0.0.1:8080


[LOGPARAMETERS]
text = {timestamp} {clientaddr}:{clientport} {clientid} {method} {url} {function}({functionparams}) result({statuscode}){newline}

HASPINI7OLD
  chown root:root ${HASPLMINI}
  chmod 600 ${HASPLMINI}
}

function hasp_ini_7new_default
{
  ACCREM="NEW"
  echo "Initializing '${HASPLMINI}' (7.x driver [with x>99]) ..."
  cat <<HASPINI7NEW >${HASPLMINI}
;*************************************************************************
;*
;* Sentinel License Manager configuration file
;*
;* Version 23.3 1.96403 at zabbixclissp.stromasys.net
;* Wed, 29 Jun 2022 08:28:38 GMT
;*
;*************************************************************************



[SERVER]
name = zabbixclissp.stromasys.net
certificate =
privatekey =
idle_session_timeout_mins = 10
pagerefresh = 3
linesperpage = 20
accremote = 0
adminremote = 0
enablehaspc2v = 0
old_files_delete_days = 90

enabledetach = 0
reservedseats = 0
reservedpercent = 0
detachmaxdays = 14
commuter_delete_days = 7
disable_um = 0

requestlog = 0
loglocal = 0
logremote = 0
logadmin = 0
errorlog = 0
rotatelogs = 0
access_log_maxsize = 0 ;kB
error_log_maxsize = 0 ;kB
zip_logs_days = 0
delete_logs_days = 0
pidfile = 0
passacc = 0

accessfromremote = 0
accesstoremote = 0
bind_local_only = 0  ; 0=all adapters, 1=localhost only


[REMOTE]
broadcastsearch = 1
aggressive = 0
serversearchinterval = 30


[ACCESS]


[USERS]


[VENDORS]


[EMS]
emsurl = http://localhost:8080
emsurl = http://127.0.0.1:8080


[LOGPARAMETERS]
text = {timestamp} {clientaddr}:{clientport} {clientid} {method} {url} {function}({functionparams}) result({statuscode}){newline}

HASPINI7NEW
  chown root:root ${HASPLMINI}
  chmod 600 ${HASPLMINI}
}

function hasp_ini_new_default
{
  ACCREM="NEW"
  echo "Initializing '${HASPLMINI}' (7.100+ and 8.x drivers) ..."
  cat <<HASPINI8 >${HASPLMINI}
;*************************************************************************
;*
;* Sentinel License Manager configuration file
;*
;* Version 24.2 1.105209 at $(hostname)
;* $(date)
;*
;*************************************************************************



[SERVER]
name = $(hostname)
certificate = 
privatekey = 
idle_session_timeout_mins = 720
pagerefresh = 3
linesperpage = 20
accremote = 0
adminremote = 0
enablehaspc2v = 0
old_files_delete_days = 90

enabledetach = 0
reservedseats = 0
reservedpercent = 0
detachmaxdays = 14
commuter_delete_days = 7
disable_um = 0
identity_storage_encrypt = no

requestlog = 0
loglocal = 0
logremote = 0
logadmin = 0
errorlog = 0
rotatelogs = 0
access_log_maxsize = 0 ;kB
error_log_maxsize = 0 ;kB
zip_logs_days = 0
delete_logs_days = 0
pidfile = 0
passacc = 0

accessfromremote = anyone
accesstoremote = 1
bind_local_only = 0  ; 0=all adapters, 1=localhost only
id_public_addr = 
proxy = 0
proxy_host = 
proxy_port = 8080
proxy_username = 
proxy_password = 


[REMOTE]
broadcastsearch = 1
aggressive = 0
serversearchinterval = 30


[ACCESS]


[USERS]


[VENDORS]


[EMS]
emsurl = http://localhost:8080
emsurl = http://127.0.0.1:8080


[LOGPARAMETERS]
text = {timestamp} {clientaddr}:{clientport} {clientid} {method} {url} {function}({functionparams}) result({statuscode}){newline}

HASPINI8
  chown root:root ${HASPLMINI}
  chmod 600 ${HASPLMINI}
}

function LoadDefault_hasplmini
{
  AKVMAJ=$(/usr/sbin/aksusbd -v 2>/dev/null | awk '{print $3}' | awk -F'.' '{print $1}')
  AKVMIN=$(/usr/sbin/aksusbd -v 2>/dev/null | awk '{print $3}' | awk -F'.' '{print $2}')
  if test -z "${AKVMAJ}"
  then
    AKSEXE=$(systemctl show -p ExecStart aksusbd | sed "s:^.* path=\(.*\)\( ; argv.*$\):\1:g")
    AKVMAJ=$(${AKSEXE} -v 2>/dev/null | awk '{print $3}' | awk -F'.' '{print $1}')
    AKVMIN=$(${AKSEXE} -v 2>/dev/null | awk '{print $3}' | awk -F'.' '{print $2}')
  fi
  case "${AKVMAJ:-0}"
  in
    7)
      if test ! -s ${HASPLMINI}
      then
        if test ${AKVMIN:-0} -ge 100
        then
          hasp_ini_7new_default
        else
          hasp_ini_7old_default
        fi
        CHANGE=1
      fi
      ;;
    8)
      if test ! -s ${HASPLMINI}
      then
        hasp_ini_new_default
        CHANGE=1
      fi
      ;;
    *)
      echo
      echo "${FGORANGE}WARNING[0m: cannot initialize hasplm.ini, wrong version of aksusbd"
      echo
      Press_Enter
      exit 1
      ;;
  esac
}

function Read_hasplmini
{
  #--- Read parameters/values from hasplm.ini
  accremote=$(grep -wi ^accremote ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFaccremote=${accremote}
  errorlog=$(grep -w ^errorlog ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFerrorlog=${errorlog}
  rotatelogs=$(grep -w ^rotatelogs ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFrotatelog=${rotatelogs}
  error_log_maxsize=$(grep -w ^error_log_maxsize ${HASPLMINI} | cut -f2 -d'=' | cut -f1 -d';' | tr -d [:space:])
  DEFerror_log_maxsize=${error_log_maxsize}
  error_log_maxsizecom=$(grep -w ^error_log_maxsize ${HASPLMINI} | cut -f2 -d';')
  zip_logs_days=$(grep -w ^zip_logs_days ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFzip_logs_days=${zip_logs_days}
  delete_logs_days=$(grep -w ^delete_logs_days ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFdelete_logs_days=${delete_logs_days}
  accesstoremote=$(grep -w ^accesstoremote ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFaccesstoremote=${accesstoremote}
  accessfromremote=$(grep -w ^accessfromremote ${HASPLMINI} | cut -f2 -d'=' | tr -d [:space:])
  DEFaccessfromremote=${accessfromremote}
  bind_local_only=$(grep -w ^bind_local_only ${HASPLMINI} | cut -f2 -d'=' | cut -f1 -d';' | tr -d [:space:])
  DEFbind_local_only=${bind_local_only}
  bind_local_onlycom=$(grep -w ^bind_local_only ${HASPLMINI} | cut -f2 -d';')
}

function display_sacc_accremote
{
  echo -n "${FGBLUEBOLD} 1[0m - Allow Remote Access to ACC: "
  case "${accremote}"
  in
    0)
      echo "[36mdisabled[0m (default)"
      ;;
    1)
      echo "${FGORANGE}enabled[0m"
      ;;
    http)
      echo "${FGORANGE}enabled / http[0m"
      ;;
    https)
      echo "${FGORANGE}enabled / https[0m"
      ;;
    *)
      echo "[31munknown state <${accremote}>[0m"
      ;;
  esac
}

function display_sacc_errorlog
{
  echo -n "${FGBLUEBOLD} 2[0m - Write an Error Log File: "
  case "${errorlog}"
  in
    0)
      echo "${FGORANGE}disabled[0m"
      ;;
    1)
      if test -s ${CHARONDIR}/utils/kit.version
      then
        if test ${rotatelogs:-0} -eq 0
        then
          if test "${MONHASPERROR}" = "enabled"
          then
            echo "[36menabled[0m (monitoring enabled)"
          else
            echo "[36menabled[0m (monitoring disabled)"
          fi
        else
          echo "[36menabled[0m (monitoring impossible with rotating log)"
        fi
      else
        echo "[36menabled[0m"
      fi
      ;;
    *)
      echo "[31munknown state <${errorlog}>[0m"
      ;;
  esac
}

function display_sacc_rotatelogs
{
  echo -n "${FGBLUEBOLD} 3[0m - Write Log Files Daily: "
  case "${rotatelogs}"
  in
    0)
      echo "[36mdisabled[0m"
      ;;
    1)
      if test ${errorlog:-0} -eq 0
      then
        echo "${FGORANGE}enabled[0m"
      else
        if test "${MONHASPERROR}" = "enabled"
        then
          echo "[31menabled[0m"
        else
          echo "[36menabled[0m"
        fi
      fi
      ;;
    *)
      echo "[31munknown state <${rotatelogs}>[0m"
      ;;
  esac
}

function display_sacc_error_log_maxsize
{
  echo -n "${FGBLUEBOLD} 4[0m - Size Limit (KB): "
  case "${error_log_maxsize}"
  in
    0)
      echo "${FGORANGE}disabled[0m"
      ;;
    *)
      echo "[36m$(printf "%'.0f\n" ${error_log_maxsize})[0m"
      ;;
  esac
}

function display_sacc_zip_logs_days
{
  echo -n "${FGBLUEBOLD} 5[0m - Days Before Compressing Log Files: "
  case "${zip_logs_days}"
  in
    0)
      echo "${FGORANGE}disabled[0m"
      ;;
    *)
      echo "[36m$(printf "%'.0f\n" ${zip_logs_days})[0m"
      ;;
  esac
}

function display_sacc_delete_logs_days
{
  echo -n "${FGBLUEBOLD} 6[0m - Days Before Deleting Log Files: "
  case "${delete_logs_days}"
  in
    0)
      echo "${FGORANGE}disabled[0m"
      ;;
    *)
      echo "[36m$(printf "%'.0f\n" ${delete_logs_days})[0m"
      ;;
  esac
}

function display_sacc_accesstoremote
{
  echo -n "${FGBLUEBOLD} 7[0m - Allow Access to Remote Licenses: "
  case "${accesstoremote}"
  in
    0)
      echo "[36mdisabled[0m"
      ;;
    1)
      echo "${FGORANGE}enabled[0m (default)"
      ;;
    *)
      echo "[31munknown state <${accesstoremote}>[0m"
      ;;
  esac
}

function display_sacc_accessfromremote
{
  echo -n "${FGBLUEBOLD} 8[0m - Allow Access from Remote Clients: "
  case "${accessfromremote}"
  in
    0)
      echo "[36mdisabled[0m"
      ;;
    1)
      echo "${FGORANGE}enabled[0m (default)"
      ;;
    anyone)
      echo "${FGORANGE}${accessfromremote}[0m (default)"
      ;;
    secure)
      echo "${FGORANGE}${accessfromremote}[0m"
      ;;
    *)
      echo "${FGORANGE}${accessfromremote}[0m"
      ;;
  esac
}

function display_sacc_bind_local_only
{
  echo -n "${FGBLUEBOLD} 9[0m - Network Visibility: "
  case "${bind_local_only}"
  in
    0)
      echo "[36mAll Network Adapters[0m"
      ;;
    1)
      echo "${FGORANGE}None (Local Access Only)[0m"
      ;;
    *)
      echo "[31munknown state <${bind_local_only}>[0m"
      ;;
  esac
}

function display_title
{
  if test -s ${PREFFILE}
  then
    SEMIGRAPH=$(grep ^SEMIGRAPH= ${PREFFILE} | cut -f2 -d'=')
    DEFAULTBLUE=$(grep ^DEFAULTBLUE= ${PREFFILE} | cut -f2 -d'=')
    MONHASPERROR=$(grep ^MONHASPERROR= ${PREFFILE} | cut -f2 -d'=')
    if test ${DEFAULTBLUE:-0} = 0
    then
      FGBLUE="[38;5;27m"
      FGBLUEBOLD="[38;5;32m"
    else
      FGBLUE="[34m"
      FGBLUEBOLD="[34m[1m"
    fi
  fi
  display_header "Sentinel Admin Control Center - Text user interface (configuration only)"
}

#-----------------------------------------------------------------------------
# Main menu
#-----------------------------------------------------------------------------
CHANGE=0
AKSVCDESC="Sentinel LDK Runtime Environment (aksusbd daemon)"
display_title
if test -x /usr/sbin/aksusbd
then
  CHK=1
  systemctl -q is-enabled aksusbd 2>/dev/null
  if test $? -ne 0
  then
    echo "${FGORANGE}WARNING[0m: 'aksusbd' service is not enabled !!"
    echo
    while test 1
    do
      echo -n "${FGBLUEBOLD}Do you want to enable it (y/n) ?[0m "
      read ANS
      case "${ANS}"
      in
        y|Y)
          systemctl enable aksusbd
          test $? -ne 0 && CHK=0
          break
          ;;
        n|N)
          CHK=0
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  fi
  systemctl -q is-active aksusbd 2>/dev/null
  if test $? -ne 0
  then
    echo "[31mERROR[0m: 'aksusbd' service is not running !!"
    echo
    while test 1
    do
      echo -n "${FGBLUEBOLD}Do you want to activate it (y/n) ?[0m "
      read ANS
      case "${ANS}"
      in
        y|Y)
          systemctl start aksusbd
          test $? -ne 0 && CHK=0
          break
          ;;
        n|N)
          CHK=0
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  fi
  if test ${CHK} = 0
  then
    echo "${AKSVCDESC} problem. Cannot continue."
    echo
    Press_Enter
    exit 1
  fi
else
  echo
  echo "${FGORANGE}WARNING[0m: aksusbd is not installed !!"
  echo
  Press_Enter
  exit 2
fi

LoadDefault_hasplmini
Read_hasplmini
AKSVCDESC=$(systemctl show -p Description aksusbd | sed 's:^Description=::g')

while test 1
do
  display_title
  if test `id -u` -ne 0
  then
    echo
    tput bold
    echo "Must be root !"
    tput sgr0
    Press_Enter
    echo
    exit 1
  fi

  tput bold
  echo "Basic settings"
  tput sgr0

  #-1-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Allow Remote Access to ACC"
  display_sacc_accremote

  #-2-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write an Error Log File" -> 0 or 1
  display_sacc_errorlog

  #-3-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write Log Files Daily" -> 0 or 1 (forced to 0 here)
  display_sacc_rotatelogs

  #-4-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write an Error Log File"/"Size Limit (KB)" -> 0 (disabled) or size in Kb
  display_sacc_error_log_maxsize

  #-5-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Days Before Compressing Log Files" -> 0 (disabled) or number of days
  display_sacc_zip_logs_days

  #-6-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Days Before Deleting Log Files" -> 0 (disabled) or number of days
  display_sacc_delete_logs_days

  echo
  tput bold
  echo "Access to Remote License Managers"
  tput sgr0

  #-7-- SACC -> "Configuration" option -> "Access to Remote License Managers" tab -> "Allow Access to Remote Licenses" -> 0 or 1
  display_sacc_accesstoremote

  #-8-- SACC -> "Configuration" option -> "Access from Remote Clients" tab -> "Allow Access from Remote Clients" -> 0 or 1 for 7x; '0', 'secure', 'split' or 'anyone' for 8x 
  display_sacc_accessfromremote

  echo
  tput bold
  echo "Network"
  tput sgr0

  #-9-- SACC -> "Configuration" option -> "Network" tab -> "Network Visibility"
  #            (If set to "None", keys containing FQDN-protected licenses will be disabled.)
  display_sacc_bind_local_only

  echo
  tput bold
  echo "Administration"
  tput sgr0
  echo "${FGBLUEBOLD}10[0m - ${FGORANGE}Edit[0m the '${HASPLMINI}' configuration file"
  echo "${FGBLUEBOLD}11[0m - [31mReset[0m to factory default settings"
  if test ${errorlog} -eq 1
  then
    LASTERRORLOG=$(find /var/hasplm -name "*error.log" -printf "%T+ %p\n" 2>/dev/null | sort | tail -1 | awk '{print $2}')
    if test -n "${LASTERRORLOG}"
    then
      echo "${FGBLUEBOLD}12[0m - View latest error log"
    fi
  fi

  echo
  if test ${PENDCHG} -eq 1
  then
    echo -n "${FGBLUEBOLD}Enter your choice ('q' to apply changes and quit): [0m"
  else
    echo -n "${FGBLUEBOLD}Enter your choice ('q' to quit): [0m"
  fi
  read ANS
  echo
  case "${ANS}"
  in
    1)
      #-1-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Allow Remote Access to ACC"
      if test "${ACCREM:-NEW}" = "OLD"
      then
        if test ${accremote} -eq 0
        then
          accremote=1
        else
          accremote=0
        fi
      else
        echo "$(tput bold)You selected$(tput sgr0):"
        display_sacc_accremote
        echo
        tput bold
        echo "Available options:"
        tput sgr0
        echo "0 - Disabled (default)"
        echo "1 - http"
        echo "2 - https"
        echo
        case "${accremote}"
        in
          0)     DEFANS=0;;
          http)  DEFANS=1;;
          https) DEFANS=2;;
          *)     DEFANS=2;;
        esac
        while test 1
        do
          echo -n "${FGBLUEBOLD}Enter your choice [${DEFANS}]: [0m"
          read ANS
          case "${ANS}"
          in
            0)
              accremote=0
              break
              ;;
            1)
              accremote=1
              break
              ;;
            2)
              accremote="https"
              break
              ;;
            "")
              break
              ;;
            *)
              tput cuu1;tput el
              ;;
          esac
        done
      fi
      ;;
    2)
      #-2-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write an Error Log File" -> 0 or 1
      if test ${errorlog} -eq 0
      then
        errorlog=1
      else
        errorlog=0
      fi
      ;;
    3)
      #-3-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write Log Files Daily" -> 0 or 1 (forced to 0 here)
      if test ${rotatelogs} -eq 0
      then
        rotatelogs=1
      else
        rotatelogs=0
      fi
      ;;
    4)
      #-4-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Write an Error Log File"/"Size Limit (KB)" -> 0 (disabled) or size in Kb
      echo "$(tput bold)You selected$(tput sgr0):"
      display_sacc_error_log_maxsize
      echo
      tput bold
      echo "Available options:"
      tput sgr0
      echo "0 - Disabled"
      echo "any numeric value (in Kb)"
      echo
      DEFANS=${error_log_maxsize}
      while test 1
      do
        echo -n "${FGBLUEBOLD}Enter your choice [${DEFANS}]: [0m"
        read ANS
        test -z "${ANS}" && ANS=${DEFANS:-0}
        if test -n "$(echo ${ANS} | tr -d [:digit:])"
        then
          tput cuu1;tput el
        else
          error_log_maxsize=${ANS}
          break
        fi
      done
      ;;
    5)
      #-5-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Days Before Compressing Log Files" -> 0 (disabled) or number of days
      echo "$(tput bold)You selected$(tput sgr0):"
      display_sacc_zip_logs_days
      echo
      tput bold
      echo "Available options:"
      tput sgr0
      echo "0 - Disabled"
      echo "any numeric value (in days)"
      echo
      DEFANS=${zip_logs_days}
      while test 1
      do
        echo -n "${FGBLUEBOLD}Enter your choice [${DEFANS}]: [0m"
        read ANS
        test -z "${ANS}" && ANS=${DEFANS:-0}
        if test -n "$(echo ${ANS} | tr -d [:digit:])"
        then
          tput cuu1;tput el
        else
          zip_logs_days=${ANS}
          break
        fi
      done
      ;;
    6)
      #-6-- SACC -> "Configuration" option -> "Basic Settings" tab -> "Days Before Deleting Log Files" -> 0 (disabled) or number of days
      echo "$(tput bold)You selected$(tput sgr0):"
      display_sacc_delete_logs_days
      echo
      tput bold
      echo "Available options:"
      tput sgr0
      echo "0 - Disabled"
      echo "any numeric value (in days)"
      echo
      DEFANS=${delete_logs_days}
      while test 1
      do
        echo -n "${FGBLUEBOLD}Enter your choice [${DEFANS}]: [0m"
        read ANS
        test -z "${ANS}" && ANS=${DEFANS:-0}
        if test -n "$(echo ${ANS} | tr -d [:digit:])"
        then
          tput cuu1;tput el
        else
          delete_logs_days=${ANS}
          break
        fi
      done
      ;;
    7)
      #-7-- SACC -> "Configuration" option -> "Access to Remote License Managers" tab -> "Allow Access to Remote Licenses" -> 0 or 1
      if test ${accesstoremote} -eq 0
      then
        accesstoremote=1
      else
        accesstoremote=0
      fi
      ;;
    8)
      #-8-- SACC -> "Configuration" option -> "Access from Remote Clients" tab -> "Allow Access from Remote Clients" -> 0 or 1 for 7x, 
      #              '0' (no one) or 'secure' or 'split' or 'anyone' for 8x
      echo "$(tput bold)You selected$(tput sgr0):"
      display_sacc_accessfromremote
      echo
      tput bold
      echo "Available options:"
      tput sgr0
      if test "${ACCREM:-NEW}" = "OLD"
      then
        if test ${accessfromremote} -eq 0
        then
          accessfromremote=1
        else
          accessfromremote=0
        fi
      else
        echo "0 - No one"
        echo "1 - Identifiable clients only"
        echo "2 - Anyone, but cloud licenses require identity"
        echo "3 - Anyone, and cloud licenses can be consumed without identity"
        echo
        case "${accessfromremote}"
        in
          0)       DEFANS=0;;
          secure)  DEFANS=1;;
          split)   DEFANS=2;;
          secure)  DEFANS=3;;
          *)       DEFANS=3;;
        esac
        while test 1
        do
          echo -n "${FGBLUEBOLD}Enter your choice [${DEFANS}]: [0m"
          read ANS
          case "${ANS}"
          in
            0)
              accessfromremote=0
              break
              ;;
            1)
              accessfromremote="secure"
              break
              ;;
            2)
              accessfromremote="split"
              break
              ;;
            3)
              accessfromremote="anyone"
              break
              ;;
            "")
              break
              ;;
            *)
              tput cuu1;tput el
              ;;
          esac
        done
      fi
      ;;
    9)
      #-9-- SACC -> "Configuration" option -> "Network" tab -> "Network Visibility"
      if test ${bind_local_only} -eq 0
      then
        bind_local_only=1
        echo "${FGORANGE}WARNING[0m: keys containing FQDN-protected licenses will be disabled!"
        echo
        Press_Enter
      else
        bind_local_only=0
      fi
      ;;
    10)
      #10-- Edit the configuration file
      LASTUPD=$(date +%s -r ${HASPLMINI})
      if test -x /usr/bin/vim
      then
        /usr/bin/vim ${HASPLMINI}
      else
        /usr/bin/vi ${HASPLMINI}
      fi
      if test ${LASTUPD} -ne $(date +%s -r ${HASPLMINI})
      then
        echo "Restarting the ${AKSVCDESC} service..."
        systemctl restart aksusbd
        sleep 2
        Read_hasplmini
      fi
      ;;
    11)
      #11-- Reset the configuration file
      echo
      echo "${FGORANGE}[1mThis will reset the configuration to default ![0m"
      echo
      while test 1
      do
        echo -n "${FGBLUEBOLD}Please confirm (y/n): [0m"
        read ANS
        case "${ANS}"
        in
          y|Y)
            mv -f ${HASPLMINI} ${HASPLMINI}.sav 2>/dev/null
            LoadDefault_hasplmini
            echo "Restarting the ${AKSVCDESC} service..."
            systemctl restart aksusbd
            sleep 2
            Read_hasplmini
            break
            ;;
          n|N)
            break
            ;;
          *)
            tput cuu1;tput el
            ;;
        esac
      done
      ;;
    12)
      #12-- View latest error log
      if test ${errorlog} -eq 1
      then
        LASTERRORLOG=$(find /var/hasplm -name "*error.log" -printf "%T+ %p\n" 2>/dev/null | sort | tail -1 | awk '{print $2}')
        if test -n "${LASTERRORLOG}"
        then
          if test -x /usr/bin/vim
          then
            /usr/bin/vim -R ${LASTERRORLOG}
          else
            /usr/bin/vi -R ${LASTERRORLOG}
          fi
        fi
      fi
      ;;
    q|Q)
      test "${DEFaccremote}"         = "${accremote}"         || CHANGE=1
      test "${DEFerrorlog}"          = "${errorlog}"          || CHANGE=1
      test "${DEFrotatelog}"         = "${rotatelogs}"        || CHANGE=1
      test "${DEFerror_log_maxsize}" = "${error_log_maxsize}" || CHANGE=1
      test "${DEFzip_logs_days}"     = "${zip_logs_days}"     || CHANGE=1
      test "${DEFdelete_logs_days}"  = "${delete_logs_days}"  || CHANGE=1
      test "${DEFaccesstoremote}"    = "${accesstoremote}"    || CHANGE=1
      test "${DEFaccessfromremote}"  = "${accessfromremote}"  || CHANGE=1
      test "${DEFbind_local_only}"   = "${bind_local_only}"   || CHANGE=1

      if test ${CHANGE} -eq 1
      then
        echo "${FGORANGE}[1mA change in the configuration has been detected.[0m"
        echo "A restart of the ${AKSVCDESC}"
        echo "service is needed. This can disrupt temporarily connection to the license(s)."
        echo
        while test 1
        do
          echo -n "${FGBLUEBOLD}Please confirm (y/n): [0m"
          read ANS
          case "${ANS}"
          in
            y|Y)
              #--- ask for confirmation and tell the aksusbd service will restart with possible temporary loss of license connection
              echo "Updating the ${HASPLMINI} file..."
              if test "${ACCREM:-NEW}" = "OLD"
              then
                cat ${HASPLMINI} | sed \
                  -e "s:\(^ACCremote\)\(.*\)$:\1 = ${accremote}:g" \
                  -e "s:\(^errorlog\)\(.*\)$:\1 = ${errorlog}:g" \
                  -e "s:\(^rotatelogs\)\(.*\)$:\1 = ${rotatelogs}:g" \
                  -e "s:\(^error_log_maxsize\)\(.*\)$:\1 = ${error_log_maxsize} ;${error_log_maxsizecom}:g" \
                  -e "s:\(^zip_logs_days\)\(.*\)$:\1 = ${zip_logs_days}:g" \
                  -e "s:\(^delete_logs_days\)\(.*\)$:\1 = ${delete_logs_days}:g" \
                  -e "s:\(^accesstoremote\)\(.*\)$:\1 = ${accesstoremote}:g" \
                  -e "s:\(^accessfromremote\)\(.*\)$:\1 = ${accessfromremote}:g" \
                  -e "s:\(^bind_local_only\)\(.*\)$:\1 = ${bind_local_only} ;${bind_local_onlycom}:g" \
                  >/tmp/hasplm.ini
                  mv -f /tmp/hasplm.ini ${HASPLMINI}
              else
                cat ${HASPLMINI} | sed \
                  -e "s:\(^accremote\)\(.*\)$:\1 = ${accremote}:g" \
                  -e "s:\(^errorlog\)\(.*\)$:\1 = ${errorlog}:g" \
                  -e "s:\(^rotatelogs\)\(.*\)$:\1 = ${rotatelogs}:g" \
                  -e "s:\(^error_log_maxsize\)\(.*\)$:\1 = ${error_log_maxsize} ;${error_log_maxsizecom}:g" \
                  -e "s:\(^zip_logs_days\)\(.*\)$:\1 = ${zip_logs_days}:g" \
                  -e "s:\(^delete_logs_days\)\(.*\)$:\1 = ${delete_logs_days}:g" \
                  -e "s:\(^accesstoremote\)\(.*\)$:\1 = ${accesstoremote}:g" \
                  -e "s:\(^accessfromremote\)\(.*\)$:\1 = ${accessfromremote}:g" \
                  -e "s:\(^bind_local_only\)\(.*\)$:\1 = ${bind_local_only} ;${bind_local_onlycom}:g" \
                  >/tmp/hasplm.ini
                  mv -f /tmp/hasplm.ini ${HASPLMINI}
              fi
              systemctl restart aksusbd
              break
              ;;
            n|N)
              break
              ;;
            *)
              tput cuu1;tput el
              ;;
          esac
        done
      else
        echo "No change in the configuration."
      fi
      Press_Enter
      break
      ;;
  esac
done

exit
