#!/bin/bash
######################################################################
#
#  Name: boomSRVSTAT
#
#  Description: Prints status of boom server using URL "/srv/html/mon".
#
#
#  Author: Solmsdorf (equensWorldline)
#  Version: 1.00
#  Date: 13.12.2023
#
#  Input parameter: 
#
#  Return: 0 = Success
#          1 = Error (>>curl -k ${SRV_URL}:${SRV_PORT}/${SRV_STATPAG}<< failed (response ${stat})!)
#
#
#  Note: requires configuration settings (SRV_URL, SRV_PORT) inside boom_env_ssl.cnf
#
#  History:
#
#     date    |name| reason
#  ----------------------------------------------------------------
#  13/12/2023 | ps | (1.00) Initial Release
#
######################################################################

NAME=$(basename $0)
PID=$$

CNFFIL="boom_env_ssl.cnf"

fil=${0}
fil_short=$(basename ${fil})
if [ `echo ${fil} | egrep "^\/" | wc -l` -eq 0 ]
then
# path not starting with "/"
  if [ `echo ${fil} | egrep "^\.\." | wc -l` -ge 1 ] || [ `echo ${fil} | egrep "\/" | wc -l` -ge 1 ]
  then
# path starting with ".."
    pd=$(realpath ${fil})
    pd=$(dirname ${pd})
    fil="${pd}/${fil_short}"
  else
# path not starting with ".."
    pd=$(pwd)
    fil="${pd}/${fil_short}"
  fi
fi
WORKDIR=$(dirname ${fil})

source "${WORKDIR}/${CNFFIL}"


user=$(id |awk -F"=" '{print $2}' |awk -F"(" '{print $2}' | awk -F")" '{print $1}')

function ShowT()
{
  local timstmp=$(date "+%d.%m.%Y %T")

   echo -e "${timstmp} ${1}: ${2}"
}


# INIT+++++++++++++++++++++++++++++++++++++++

stat=""

# MAIN++++++++++++++++++++++++++++++++++++++++++

res=$(curl -w %{http_code} -s -k ${SRV_URL}:${SRV_PORT}/${SRV_STATPAG})
stat=$(echo ${res##*Policy})
if [ ${stat} -ne 200 ]
then
  ShowT ERROR "(${PID}) [${user}] - calling >>curl -k ${SRV_URL}:${SRV_PORT}/${SRV_STATPAG}<< failed (response ${stat})!"
  exit 1
else
## output without last 4 CHARs (response code) but with new lines
  echo "${res:0:-4}"
fi

exit 0

