#!/bin/sh
#-------------------------------------------------------------------------------
# menusetup
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2022 STROMASYS.
# All rights reserved.
#
#set -x
VERSION=1.33

if test "${PWD}" != "/opt/charon/utils"
then
  echo "Error: 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-PAR 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"
DBUS_SESSION_BUS_ADDRESS=""
PATH=\$PATH:/opt/charon/utils
[[ -n "\$LS_COLORS" ]] && LS_COLORS="\$LS_COLORS:*.cfg=0;34;106:*.dsk=0;45;37"
export PATH DBUS_SESSION_BUS_ADDRESS 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-PAR 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

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

echo
echo "${FGBLUE}Recommended color settings[0m: white background and black foreground"
Press_Enter

tput bold
echo "Mandatory executables and packages:"
tput sgr0
note_install_package
CHK=1

YUMINS=/tmp/menusetup.$$
if test -x /usr/bin/dnf
then
  dnf list installed >${YUMINS}
else
  yum list installed >${YUMINS}
fi

for PE in P:perl P:at P:ed P:usbutils E:sendmail E:base64 P:bc
do
  P=`echo ${PE} | cut -f2 -d':'`
  case `echo ${PE} | cut -f1 -d':'`
  in
    "P")
      echo -n "- Package '${P}': "
      if test -n "`grep ^${P}\\. ${YUMINS}`"
      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
for PE in P:telnet P:putty P:mailx E:ssh E:rsh P:expect P:evince P:postfix E:firefox P:vim
do
  P=`echo ${PE} | cut -f2 -d':'`
  case `echo ${PE} | cut -f1 -d':'`
  in
    "P")
      echo -n "- Package '${P}': "
      if test -n "`grep ^${P}\\. ${YUMINS}`"
      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 recommended tools...[0m"
fi

rm -f ${YUMINS} 2>/dev/null

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}Preferences can be defined within The Charon-PAR 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-PAR 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
#--- 'wall' alert messages are disabled by default
WALLOFFFILE="/root/.charonwalloff"
touch ${WALLOFFFILE}
echo
echo "[1m${FGBLUE}The Charon-PAR 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
