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

. $(dirname $0)/charon_common
. $(dirname $0)/charon_common_menu

display_header "License server management web interface"
echo
EXCODE=0

if test ! -x /opt/license-server/license_server
then
  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=1
else
  if test -z "${DISPLAY}"
  then
    echo "${FGORANGE}WARNING[0m: \$DISPLAY is empty, cannot open the license server management"
    echo "         web interface"
    EXCODE=2
  else
    if test -z "$(which firefox 2>/dev/null)"
    then
      echo "Firefox is not installed, cannot open the license server management web interface!"
      EXCODE=3
    else
      FIRLOG=${CHARONDIR}/log/$(basename $0).log
      PORT=$(grep -w ^http_port /opt/license-server/config.ini 2>/dev/null | cut -f2 -d'=' | tr -dc [:digit:])
      /usr/bin/firefox localhost:${PORT:-8084} 2>${FIRLOG}
      RET=$?
      if test ${RET} -ne 0
      then
        if test -n "$(grep 'Running Firefox as root' ${FIRLOG} | grep 'not supported')"
        then
          #--- extracting the user in the error output file ("who am i" could also work)
          FIRUSR=$(sed "s=\(^.* owned by \)\(.*\)\(\..$\)=\2=g" ${FIRLOG})
          su ${FIRUSR} -c "/usr/bin/firefox localhost:${PORT:-8084}"
        else
          echo "Firefox failed to execute (code ${RET}):"
          cat ${FIRLOG}
          EXCODE=4
        fi
      fi
    fi
  fi
fi

echo
exit ${EXCODE:-0}
