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

test $TERM = "putty" && export TERM=xterm

FGORANGE="[38;5;208m"
BGORANGE="[30;48;5;208m"

if test "${PWD}" != "/opt/charon/utils"
then
  echo "[41m[37m ERROR [0m current directory is not set to '/opt/charon/utils'. Cannot continue."
  echo "The kit must be installed in this folder only."
  exit 1
fi

. $(dirname $0)/charon_common
. $(dirname $0)/charon_common_menu
for P in $(ls /etc/profile.d/charon* 2>/dev/null);do . $P;done
test -e /opt/charon/log || mkdir /opt/charon/log

function display_setup_header
{
  display_header "Charon-${KITP} Linux Toolkit - menu setup"
}

function manage_alias
{
  LINE="/etc/profile.d/charon_menu.sh"
  if test -e ${LINE}
  then
    echo "${FGBLUE}Updating the 'menu' alias...[0m"
  else
    echo "${FGBLUE}Creating the 'menu' alias...[0m"
  fi
  cat <<EOF >${LINE}
[[ -z "\$PS1" ]] && return
alias menu="${CHARONDIR}/utils/charon_menu"
alias logtail="${CHARONDIR}/utils/charon_cmdline taillogfile"
alias logview="${CHARONDIR}/utils/charon_cmdline viewlogvim"
alias vmlist="${CHARONDIR}/utils/charon_cmdline vmlist"
alias vmstart="${CHARONDIR}/utils/charon_cmdline vmstart"
alias vmconsole="${CHARONDIR}/utils/charon_cmdline vmconsole"
alias vmstop="${CHARONDIR}/utils/charon_cmdline vmstop"
alias vmcfg="${CHARONDIR}/utils/charon_cmdline vmcfg"
alias sacctui="${CHARONDIR}/utils/charon_menu_sacctui"
PATH=\$PATH:/opt/charon/utils
[[ -n "\$LS_COLORS" ]] && LS_COLORS="\$LS_COLORS:*.cfg=0;34;106:*.dsk=0;45;37"
export PATH LS_COLORS
EOF
  echo "Done."
  chmod 755 ${LINE}
}

function inform_alias
{
  cat <<EOF

Execute the following command or disconnect/reconnect for the 'menu',
'vmlist', 'logtail' and 'logview' aliases to be taken into account:

[1m. /etc/profile.d/charon_menu.sh[0m

$(tput bold)Then enter 'menu' to start...$(tput sgr0)

EOF
}

function note_install_package
{
  tput smul
  echo -n "Note"
  tput rmul
  if test -x /usr/bin/dnf
  then
    echo ": To install packages, use the command '# dnf install <package>'. You can"
  else
    echo ": To install packages, use the command '# yum install <package>'. You can"
  fi
  echo "      also perform installation from the installation disc, 'Packages'"
  echo "      folder."
  echo
}

display_setup_header
if test -z "$1"
then
  cat <<EOF

Welcome to the Charon-${KITP} Linux Toolkit menu setup.

${FGBLUE}This package provides the following functions:[0m
- Menu driven installation and upgrades
- Licensing: content display, updates, expiration check
- Automatic start and stop with clean shutdown for guests (emulated systems)
- Connection to guests console, console logs
- Edit configuration files
- Monitor guests log files for license key removal and informational, warning
  and error events

EOF
fi

if test $(id -u) -ne 0
then
  echo "[1m[31mRestriction: you must be root to continue.[0m"
  echo
  exit 1
fi

if test "$1" = "-a" -o "$1" = "--alias"
then
  manage_alias
  inform_alias
  exit
fi

test -e ${ETCCHARON} || mkdir ${ETCCHARON}

if test "${TERM}" = "vt100"
then
  cat <<EOF
Your current terminal is currently set to VT100. Using this mode you will
encounter erratic display (line drawing not correctly displayed)
The terminal must be set at least to vt200, xterm or dtterm.

[1m${FGBLUE}Please update the TERM value and restart the menu setup.[0m
EOF
  exit 1
fi

Press_Enter

CHK=1
if test -e /etc/redhat-release
then
  OS_RELEASE=/etc/redhat-release
else
  OS_RELEASE=/etc/system-release
fi
case "$(cat ${OS_RELEASE} | awk '{print $1}')"
in
  "Red")
    INS_MAJOR=$(cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.*\)\(\..*$\)=\2=g")
    ;;
  "CentOS")
    INS_MAJOR=$(cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.\)\(.*$\)=\2=g")
    ;;
  "Rocky")
    INS_MAJOR=$(cat ${OS_RELEASE} | sed "s=\(^.* release \)\(.\)\(.*$\)=\2=g")
    ;;
  *)
    CHK=0
    ;;
esac
test ${INS_MAJOR:-0} -le 6 && CHK=0
if test ${CHK} -eq 0
then
  echo "[41m[37m ERROR [0m This OS version is not supported:"
  cat ${OS_RELEASE}
  exit 42
fi

tput bold
echo "Mandatory executables and packages:"
tput sgr0
note_install_package

for PE in E:perl P:at P:ed P:usbutils E:sendmail E:base64 P:bc E:strings
do
  P=$(echo ${PE} | cut -f2 -d':')
  case $(echo ${PE} | cut -f1 -d':')
  in
    "P")
      echo -n "- Package '${P}': "
      rpm -q ${P} >/dev/null
      if test $? -eq 0
      then
        echo "installed."
      else
        echo "[31m[1mnot installed![0m."
        CHK=0
      fi
      ;;
    "E")
      if test -z "$(which ${P} 2>/dev/null)"
      then
        echo "- Executable '${P}': [31m[1mnot present![0m."
        CHK=0
      else
        echo "- Executable '${P}': present."
      fi
      ;;
  esac
done
if test ${CHK} = 0
then
  echo "[1mPlease install the missing mandatory tools and restart this script to complete installation.[0m"
  exit 1
fi

tput sgr0
if test ! -e /usr/bin/telnet
then
  cat <<EON

${FGBLUE}
$(tput smacs)lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk$(tput rmacs)
$(tput smacs)x$(tput rmacs)[31mNote${FGBLUE}: 'telnet' is a recommended package, if you don't install it you will not$(tput smacs)x$(tput rmacs)
$(tput smacs)x$(tput rmacs)      be able to connect from this server to the CHARON virtual machine(s)   $(tput smacs)x$(tput rmacs)
$(tput smacs)x$(tput rmacs)      console(s)                                                             $(tput smacs)x$(tput rmacs)
$(tput smacs)mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj$(tput rmacs)
[0m

EON
fi

echo
tput bold
echo "Recommended executables and packages:"
tput sgr0

CHK=1
PACKMAIL="mailx"
test ${INS_MAJOR:-0} -ge 9 && PACKMAIL="s-nail"

for PE in P:telnet P:putty P:${PACKMAIL} E:ssh E:rsh P:expect P:evince P:postfix E:firefox E:vim
do
  P=$(echo ${PE} | cut -f2 -d':')
  case $(echo ${PE} | cut -f1 -d':')
  in
    "P")
      echo -n "- Package '${P}': "
      rpm -q ${P} >/dev/null
      if test $? -eq 0
      then
        echo "installed."
      else
        echo "${FGORANGE}not installed![0m."
        CHK=0
      fi
      ;;
    "E")
      if test -z "$(which ${P} 2>/dev/null)"
      then
        echo "- Executable '${P}': [31m[1mnot present![0m."
        CHK=0
      else
        echo "- Executable '${P}': present."
      fi
      ;;
  esac
done
if test ${CHK} = 0
then
  echo "[1mPlease install the missing recommended tools if needed...[0m"
fi

if test -e /usr/bin/telnet
then
  echo
  tput bold
  echo "To connect to the guests consoles, it is recommended to force mode to"
  echo "'character' either at telnet prompt or using the .telnetrc file."
  tput sgr0
  if test ! -e ${HOME}/.telnetrc
  then
    while test 1
    do
      echo -n "Do you want to create the .telnetrc file with character mode (y/n) ? "
      read ANS
      case "${ANS}"
      in
        y)
          echo "DEFAULT" >${HOME}/.telnetrc
          echo "	mode char" >>${HOME}/.telnetrc
          break
          ;;
        n)         
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  else
    echo
    echo "The ${HOME}/.telnetrc file already exists, please update it manually."
  fi
  echo
  Press_Enter
fi

echo
manage_alias
Press_Enter

display_setup_header
echo
echo "[1m${FGBLUE}Monitoring needs to be configured within The Charon-${KITP} Linux Toolkit[0m"
echo "Please define your settings (this can also be done later)."
sleep 3
/opt/charon/utils/charon_menu_settings
echo "Done."

#display_setup_header
#echo
#echo "[1m${FGBLUE}Preferences can be defined within The Charon-${KITP} Linux Toolkit[0m"
#echo "Please define yours (this can also be done later)."
#sleep 3
#/opt/charon/utils/charon_menu_preferences
#echo "Done."

display_setup_header
echo
echo "[1m${FGBLUE}The Charon-${KITP} Linux Toolkit uses crontab to schedule recursive jobs.[0m"
echo "Jobs will be installed..."
sleep 3
/opt/charon/utils/charon_menu_cron -setup
echo "Done."

display_setup_header
echo
echo "[1m${FGBLUE}The Charon-${KITP} Linux Toolkit uses sendmail (by default) to send alerts.[0m"
echo "You will now be prompted to select alert mode, recipients list, etc..."
Press_Enter
/opt/charon/utils/charon_menu_alertsmgt

display_setup_header

inform_alias

exit
