#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu_VElicensed
#-------------------------------------------------------------------------------
# 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
EXCODE=0

while test 1
do
  display_header "License service management (licensed)"
  if test $(id -u) -ne 0
  then
    echo "[31mERROR[0m: must be root !"
    EXCODE=1
    break
  fi

  if test -x /opt/license-server/license_server
  then
    tput bold
    echo "Service status:"
    tput sgr0
    /usr/bin/systemctl --no-pager --lines 0 status licensed
    /usr/bin/systemctl -q is-enabled licensed
    if test $? -ne 0
    then
      tput bold
      echo "Service is not enabled. Enabling it now..."
      tput sgr0
      /usr/bin/systemctl enable licensed
    fi
    echo

    SVCSTA=$(systemctl is-active licensed)
    if test "${SVCSTA}" = "active"
    then
      while test 1
      do
        echo -n "Do you want to restart the service (y/n) (q to quit) ? "
        read ANS
        case "${ANS}"
        in
          y|Y)
            /usr/bin/systemctl restart licensed
            break
            ;;
          n|N|q|Q)
            break 2
            ;;
          *)
            break
            ;;
        esac
      done
    fi
  else
    echo
    echo "${FGORANGE}WARNING[0m: License server package is not installed (VE license) !"
    echo "         Please ensure you are running this utility on the license server."
    EXCODE=2
    break
  fi
done

echo
exit ${EXCODE:-0}
