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

. `dirname $0`/charon_common
. `dirname $0`/charon_common_menu

get_preferences

display_header "Generate Charon Report (for support cases)"

if test `id -u` -ne 0
then
  echo "[31mERROR[0m: must be root !"
  exit 1
fi

LASTREP=`ls -1tr ${CHARONDIR}/utils/CharonReport*.sh 2>/dev/null | tail -1`
if test -z "${LASTREP}"
then
  tput bold
  echo "Cannot find Charon Report script !"
  tput sgr0
  exit 2
fi

ABOUTME=$(grep "^# ..-...-.. V.*:" ${LASTREP} | tail -1)
RVERSION=$(echo ${ABOUTME} | awk '{print $3}' | tr -d 'V' | tr -d ':')
RVERMAJ=$(echo ${RVERSION} | cut -f1 -d'.')
RVERMIN=$(echo ${RVERSION} | cut -f2 -d'.')
REPORTMODE=""
if test ${RVERMAJ:-1} -eq 1
then
  if test ${RVERMIN:-0} -ge 45
  then
    #--- Ask if short or long version
    while test 1
    do
      tput cup 3 0;tput ed
      echo "[1mPlease select report type:[0m"
      echo "1 - long [default]"
      echo "2 - short"
      echo "q - quit"
      echo "? - help"
      echo
      echo -n "Choice [1]: "
      read ANS
      case "${ANS}"
      in
        1|"")
          REPORTMODE=""
          break
          ;;
        2)
          REPORTMODE="-s"
          break
          ;;
        q)
          exit
          ;;
        "?")
          cat <<EOF

[1mRemoved items in Charon Report short version[0m:
- UUID
- Top 20 processes consuming memory
- Network:
  - 'ip a' output,
  - configuration files if any (ifcfg-xxx),
  - 'netstat -r', 'netstat -an' and 'netstat -s' output,
  - chronyc (time sync)
- All installed packages list
- 'chronyc' and 'ntpd' config files
- 'journalctl' output
- '/var/log/messages*' files
EOF
          Press_Enter
          ;;
        *)
          tput cuu1;tput ed
          ;;
      esac
    done
  fi
fi
echo
${LASTREP} ${REPORTMODE}

case $?
in
  0)
    echo "Please send the resulting .zip file to Stromasys support team."
    ;;
  7)
    echo "${FGORANGE}Execution aborted.[0m"
    ;;
esac
echo
exit

