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

function The_End
{
  tput rmacs
  tput sgr0
  echo "$1"
  exit
}

trap 'echo;The_End "Aborted."' 1 2 9 14 15

. `dirname $0`/charon_common
. `dirname $0`/charon_common_menu
LOGFLIST="${CHARONDIR}/utils/charon_logchk.list"
BOOTLIST="${CHARONDIR}/utils/charon_gstart.boot"

#-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
function set_default_editor
{
  echo
  echo "Please select the default editor for the [32m[1mcharacter[0m user interface (CUI)."
  echo "Selecting 'none' will force the utility to ask you the editor each time"
  echo "a file has to be edited."
  ED="none "
  test -x /bin/nano      && ED="${ED}nano "
  test -x /bin/vi        && ED="${ED}vi "
  test -x /usr/bin/vim   && ED="${ED}vim "
  echo "[1mAvailable options[0m: ${FGBLUE}${ED}[0m"
  while test 1
  do
    echo -n "${FGBLUEBOLD}Your choice [`basename ${CUIEDIT}`] (q to quit): [0m"
    read ANS
    ANS=`echo "${ANS}" | tr -d [:blank:]`
    test -z "${ANS}" && ANS=`basename ${CUIEDIT}`
    case "${ANS}"
    in
      q|Q)
        break
        ;;
      *)
        if test -n "`echo ${ED} | grep -w ${ANS}`"
        then
          CUIEDIT=${ANS}
          test "${ANS}" = "none" || CUIEDIT=`which ${ANS}`
          set_preferences
          break
        else
          tput cuu1;tput el
        fi
        ;;
    esac
  done

  echo
  echo "Please select the default editor for the [32m[1mgraphical[0m user interface (GUI)."
  echo "Selecting 'none' will force the default CUI editor (if not set to 'none')."
  ED="none "
  test -x /usr/bin/gedit && ED="${ED}gedit "
  test -x /usr/bin/emacs && ED="${ED}emacs "
  test -x /usr/bin/gvim  && ED="${ED}gvim "
  echo "[1mAvailable options[0m: ${FGBLUE}${ED}[0m"
  while test 1
  do 
    echo -n "${FGBLUEBOLD}Your choice [`basename ${GUIEDIT}`] (q to quit): [0m"
    read ANS
    ANS=`echo "${ANS}" | tr -d [:blank:]`
    test -z "${ANS}" && ANS=`basename ${GUIEDIT}`
    case "${ANS}"
    in
      q|Q)
        break
        ;;
      *)
        if test -n "`echo ${ED} | grep -w ${ANS}`"
        then
          GUIEDIT=${ANS}
          test "${ANS}" = "none" || GUIEDIT=`which ${ANS}`
          set_preferences
          break
        else
          tput cuu1;tput el
        fi
        ;;
    esac
  done

  if test "${CUIEDIT}" = "none" -a "${GUIEDIT}" = "none"
  then
    SELEDIT="N/A"
    set_preferences
  else
    echo
    echo "${FGBLUEBOLD}Available editor modes selection: [0m"
    echo "1 - always ask"
    echo "2 - automatic"
    SELEDITD="1"
    test "${SELEDIT}" = "auto" && SELEDITD="2"
    while test 1
    do 
      echo -n "${FGBLUEBOLD}Please select the mode [default is ${SELEDITD}] (q to quit): [0m"
      read ANS
      ANS=`echo "${ANS}" | tr -d [:blank:]`
      test -z "${ANS}" && ANS=${SELEDITD}
      case "${ANS}"
      in
        q|Q)
          break
          ;;
        1)
          SELEDIT="always_ask"
          set_preferences
          break
          ;;
        2)
          SELEDIT="auto"
          set_preferences
          break
          ;;
        *)
          tput cuu1;tput el
          ;;
      esac
    done
  fi
}

function log_tail_on_startstop
{
  cat <<EOF

When a guest (virtual machine) is started or stopped, its log file is displayed
continuously until CTRL-C is pressed. This feature can be disabled, the log
file can still be displayed using the 'logtail' or 'logview' commands.

(!) This is valid for RHEL 7.x and CentOS 7.x and above (not for RHEL 6.x)

EOF

  CH="enable"
  test "${LOGTAILST}" = "true" && CH="disable"
  while test 1
  do 
    echo -n "${FGBLUEBOLD}Do you want to ${CH} this feature (y/n)? [n] [0m"
    read ANS
    test -z "${ANS}" && ANS="n"
    case "${ANS}"
    in
      y|Y)
        if test "${LOGTAILST}" = "true"
        then
          LOGTAILST="false"
        else
          LOGTAILST="true"
        fi
        set_preferences
        break
        ;;
      n|N)
        break
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done

  cat <<EOF

A "Press CTRL-C" line can be displayed periodically as a reminder when a
continuous log view is running either started automatically or on demand,
using the 'logtail'command for example.

EOF
  CH="enable"
  test "${LOGTAILCT}" = "enabled" && CH="disable"
  while test 1
  do 
    echo -n "${FGBLUEBOLD}Do you want to ${CH} this feature (y/n)? [n] [0m"
    read ANS
    test -z "${ANS}" && ANS="n"
    case "${ANS}"
    in
      y|Y)
        if test "${LOGTAILCT}" = "enabled"
        then
          LOGTAILCT="disabled"
        else
          LOGTAILCT="enabled"
        fi
        set_preferences
        break
        ;;
      n|N)
        break
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done

  cat <<EOF

The continuous log view can be stopped automatically if either the "Started"
or "Stopped" message is found in the log file. This is the default behavior.

EOF
  CH="enable"
  test ${AUTOSTOPLOGTAIL:-1} = 1 && CH="disable"
  while test 1
  do 
    echo -n "${FGBLUEBOLD}Do you want to ${CH} this feature (y/n)? [n] [0m"
    read ANS
    test -z "${ANS}" && ANS="n"
    case "${ANS}"
    in
      y|Y)
        if test ${AUTOSTOPLOGTAIL:-1} = 1
        then
          AUTOSTOPLOGTAIL=0
        else
          AUTOSTOPLOGTAIL=1
        fi
        set_preferences
        break
        ;;
      n|N)
        break
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done
}

function update_sysctl
{
    VMOV=`/sbin/sysctl -n vm.overcommit_memory 2>/dev/null`
    VMPA=`/sbin/sysctl -n vm.panic_on_oom 2>/dev/null`
    cat <<EOSYSCTL >/etc/sysctl.d/charon.conf
#-- Updated on `date +"%d-%b-%Y %H:%M:%S"` --
vm.overcommit_memory=${VMOV}
vm.panic_on_oom=${VMPA}
EOSYSCTL
}

function semigraphics_settings
{
  if test "${SEMIGRAPH}" = "enabled"
  then
    SEMIGRAPH="disabled"
  else
    SEMIGRAPH="enabled"
  fi
  set_preferences
}

function display_vm_separator
{
  echo
  while test 1
  do
    echo -n "Select 1=none, 2=blank line, 3=separation line, q=quit : "
    read ANS
    case "${ANS}"
    in
      1)
        LISTSEPAR="none"
        set_preferences
        break
        ;;
      2)
        LISTSEPAR="blank"
        set_preferences
        break
        ;;
      3)
        LISTSEPAR="line"
        set_preferences
        break
        ;;
      q)
        break
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done
}

function default_blue
{
  test ${DEFAULTBLUE:-0} = 0 && DEFAULTBLUE=1 || DEFAULTBLUE=0
  set_preferences
}

function set_sudo
{
  test ${ENABLESUDO:-0} = 0 && ENABLESUDO=1 || ENABLESUDO=0
  set_preferences
}

function console_mode
{
  case "$(cat ${CHARONDIR}/utils/kit.product 2>/dev/null)"
  in
    PAR)
      cat <<EOCMPAR

[7mSet console access mode to either:[0m
1 - "[36mAlways ask[0m" : will ask for putty, telnet or xhpterm if installed and
                            if a DISPLAY is defined
2 - "[36mputty[0m"      : if 'putty' and 'telnet' are installed and a DISPLAY is
                   defined, will force to 'putty'. If no DISPLAY, will force
                   to 'telnet'
3 - "[36mtelnet[0m"     : if 'telnet' is installed, will force to 'telnet'
4 - "[36mxhpterm[0m"    : if 'xhpterm' is installed, will force to 'xhpterm'

q - quit

EOCMPAR
      ;;
    *)
      cat <<EOCMAXP

[7mSet console access mode to either:[0m
1 - "[36mAlways ask[0m" : if 'putty' and 'telnet' are installed and a DISPLAY is
                   defined, will ask for one or the other
2 - "[36mputty[0m"      : if 'putty' and 'telnet' are installed and a DISPLAY is
                   defined, will force to 'putty'. If no DISPLAY, will force
                   to 'telnet'
3 - "[36mtelnet[0m"     : if 'telnet' is installed, will force to 'telnet'

q - quit

EOCMAXP
      ;;
  esac

  case "${CONSOLEMODE}"
  in
    "putty")   DEFCMOD=2;;
    "telnet")  DEFCMOD=3;;
    "xhpterm") DEFCMOD=4;;
    *)         DEFCMOD=1;;
  esac

  while test 1
  do
    echo -n "Console access mode [${DEFCMOD}] : "
    read ANS
    test -z "${ANS}" && ANS=${DEFCMOD}
    case "${ANS}"
    in
      1)
        CONSOLEMODE="always_ask"
        set_preferences
        break
        ;;
      2)
        if test -e /usr/bin/putty
        then
          CONSOLEMODE="putty"
          set_preferences
          break
        else
          tput bold
          echo "[41m Error [0m 'putty' is not installed !"
          tput sgr0
          echo
        fi
        ;;
      3)
        if test -e /usr/bin/telnet
        then
          CONSOLEMODE="telnet"
          set_preferences
          break
        else
          tput bold
          echo "[41m Error [0m 'telnet' is not installed !"
          tput sgr0
          echo
        fi
        ;;
      4)
        if test "$(cat ${CHARONDIR}/utils/kit.product 2>/dev/null)" = "PAR"
        then
          OK=1
          rpm -q xhpterm-free >/dev/null
          if test $? -ne 0
          then
            tput bold
            echo "${BGORANGE} Warning [0m 'xhpterm' is not installed. Please install the package provided."
            tput sgr0
            OK=0
            echo
          fi
          rpm -q xorg-x11-fonts-misc >/dev/null
          if test $? -ne 0
          then
            tput bold
            echo "${BGORANGE} Warning [0m 'xorg-x11-fonts-misc' is not installed."
            echo "          If needed please install this package."
            tput sgr0
            OK=0
            echo
          fi
          test ${OK} -eq 0 && Press_Enter

          CONSOLEMODE="xhpterm"

          echo
          echo "[7mPlease specify the font used[0m"
          echo "Recommended: '10x20' and '6x13' if 10x20 does not work"
          tput sgr0
          DEFFONT=${XHPTERMFONTS}
          while test 1
          do
            echo -n "Font [${DEFFONT}] : "
            read ANS
            test -z "${ANS}" && ANS=${DEFFONT}
            if test -x /usr/bin/fc-list
            then
              if test -z "$(/usr/bin/fc-list | grep "/${ANS}\.")"
              then
                echo "[41m Error [0m The font '${ANS}' is not installed."
                echo
              else
                break
              fi
            fi
          done
          XHPTERMFONTS=${ANS}

          echo
          DEFMAPCR=${XHPTERMMAPCR}
          while test 1
          do
            echo -n "Map the Block-Mode Enter key (y/n) [${DEFMAPCR}] : "
            read ANS
            test -z "${ANS}" && ANS=${DEFMAPCR}
            case "${ANS}"
            in
              y|Y|n|N)
                ANS=$(echo ${ANS} | tr [:upper:] [:lower:])
                if test ! -x /usr/bin/xmodmap
                then
                  echo "${BGORANGE} Warning [0m 'xmodamp' command is not available."
                  echo "Please install the 'xorg-x11-server-utils' package."
                  echo
                  Press_Enter
                fi
                break
                ;;
              *)
                tput cuu1
                tput ed
                ;;
            esac
          done
          XHPTERMMAPCR=${ANS}

          set_preferences
          break
        else
          tput cuu1
          tput ed
        fi
        ;;
      q)
        break
        ;;
      *)
        tput cuu1
        tput ed
        ;;
    esac
  done
}


#-----------------------------------------------------------------------------
# Main menu
#-----------------------------------------------------------------------------
while test 1
do
  display_header "Preferences"
  if test `id -u` -ne 0
  then
    echo
    echo "[31mERROR[0m: must be root !"
    Press_Enter
    echo
    exit 1
  fi

  #--- get the user's preferences (function is in charon_common script)
  get_preferences

  #--- display the options
  echo " 1 - Default editor:"
  echo "       Character User Interface (CUI) = [36m`basename ${CUIEDIT}`[0m"
  echo "       Graphical User Interface (GUI) = [36m`basename ${GUIEDIT}`[0m"
  echo "       Selection mode = [36m${SELEDIT}[0m"

  echo " 2 - Continuous log view:"
  echo "       Automatically start with guest start: [36m${LOGTAILST}[0m"
  echo "       Press CTRL-C line display/reminder: [36m${LOGTAILCT}[0m"
  if test ${AUTOSTOPLOGTAIL:-1} = 1
  then
    echo "       Automatically stop continuous log view: [36mYes[0m"
  else
    echo "       Automatically stop continuous log view: No[0m"
  fi
  echo -n "       "
  if test "${SEMIGRAPH}" = "enabled"
  then
    tput smacs
    echo -n "m"
    tput rmacs
  else
    echo -n "+"
  fi
  echo " [90mwhen started interactively from the 'menu' or with 'vmstart'.[0m"

  echo -n " 3 - Semi-graphics (line drawing): "
  test "${SEMIGRAPH}" = "enabled" && echo -n "[36m" || echo -n "[31m"
  echo "${SEMIGRAPH}[0m"

  echo -n " 4 - Display guests separator: "
  case "${LISTSEPAR}"
  in
    ""|"none") echo "None";;
    "blank")   echo "[36mBlank line[0m";;
    "line")    echo "[36mSeparation line[0m";;
  esac

  echo -n " 5 - Blue color adjust for white/black background: "
  if test ${DEFAULTBLUE:-0} = 0
  then
    echo "[38;5;27mNORMAL[0m - [38;5;32mBOLD[0m"
  else
    echo "${FGBLUE}NORMAL[0m - ${FGBLUEBOLD}BOLD[0m"
  fi

  echo -n " 6 - Access 'menu' using 'sudo': "
  test ${ENABLESUDO:-0} = 0 && echo "[31mDisabled[0m" || echo "[36mEnabled[0m"

  echo -n " 7 - Console access mode: "
  if test ! -e /usr/bin/putty
  then
    if test -e /usr/bin/telnet
    then
      echo "[31m'putty' not installed, using 'telnet'[0m"
      CONSOLEMODE="telnet"
      set_preferences
    else
      echo "[31mNor 'putty' nor 'telnet' are installed[0m"
      CONSOLEMODE="always_ask"
      set_preferences
    fi
  else
    case "${CONSOLEMODE}"
    in
      "always_ask")
        echo "[36mAlways ask[0m"
        ;;
      "telnet")
        echo "[36m'telnet' preferred[0m"
        ;;
      "putty")
        echo "[36m'putty' preferred when possible[0m"
        ;;
      "xhpterm")
        echo "[36m'xhpterm' preferred when possible[0m"
        echo -n "                          Font: [36m${XHPTERMFONTS}[0m, Map enter=[36m"
        if test "${XHPTERMMAPCR}" = "y"
        then
          echo "yes[0m"
        else
          echo "no[0m"
        fi
        ;;
      *)
        echo "[36mAlways ask[0m"
        CONSOLEMODE="always_ask"
        set_preferences
        ;;
    esac
  fi

  echo
  echo -n "${FGBLUEBOLD}Enter your choice ('q' to quit): [0m"
  read ANS
  case "${ANS}"
  in
    1)
      #--- Default editor
      set_default_editor
      ;;
    2)
      #--- Log tail when a guest is started/stopped
      log_tail_on_startstop
      ;;
    3)
      #--- Semi-graphics
      semigraphics_settings
      ;;
    4)
      #--- Display VM separator
      display_vm_separator
      ;;
    5)
      #--- Default blue color for white or black background
      default_blue
      ;;
    6)
      #--- Enable/disable 'sudo'
      set_sudo
      ;;
    7)
      #--- Console access mode
      console_mode
      ;;
    q|Q)
      break
      ;;
  esac
done

exit
