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

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

LOGFLIST="${CHARONDIR}/utils/charon_logchk.list"
BOOTLIST="${CHARONDIR}/utils/charon_gstart.boot"

while test 1
do
  test "$1" = "cmd" || display_header "Edit configuration files"
  if test `id -u` -ne 0
  then
    tput bold
    echo "Must be root !"
    tput sgr0
    Press_Enter
    echo
    exit 1
  fi

  if test "$1" != "cmd"
  then
    display_guestslist -num
    echo
  fi

  NGUESTS=`grep -v ^# ${BOOTLIST} | wc -l`
  if test ${NGUESTS:-0} -eq 0
  then 
    tput bold
    echo "Guests list is empty."
    tput sgr0
    break 
  fi

  if test ${NGUESTS:-0} -eq 1
  then
    ANS=1
  else
    if test "$1" = "cmd"
    then
      ANS=$2
    else
      printf "Select the guest you want to edit the configuration file (q to quit): "
      read ANS
      test "${ANS}" = "q" && break
    fi
  fi

  if test -n "${ANS}"
  then
    if test -z "`echo ${ANS} | tr -d [0123456789]`"
    then
      if test ${ANS} -lt 1 -o ${ANS} -gt `grep -v ^# ${BOOTLIST} | wc -l`
      then
        tput bold
        echo "Incorrect value, out of range."
        tput sgr0
        sleep 3
      else
        echo
        EXE=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f1 -d';'`
        CFG=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f2 -d';'`
        get_logfile ${CFG}
        BCK_LOGF=${LOGF}
        BCK_ROTA=${ROTA}

        tput bold
        echo "Note: changes will be taken into account on service/guest restart"
        tput sgr0
        ask_editor noquit
        if test -n "${EDI}"
        then
          if test -z "`echo ${EDI}|grep vim`"
          then
            ${EDI} ${CFG} 2>/dev/null
          else
            ${EDI} -u ${CHARONDIR}/utils/charoncfg.vimrc ${CFG} 2>/dev/null
          fi
          while test 1
          do
            CPID=`ps -ef | grep "${EDI} ${CFG}" | grep -v grep | awk '{print $2}'`
            test -z "${CPID}" && break
            sleep 2
          done

          if test "$1" != "cmd"
          then
            echo
            display_header "Edit configuration files"
            echo
          fi
          echo "[44m[37m Configuration description [0m"
          echo "${FGBLUE}Configuration file:[0m ${CFG}"
          if test `grep -c "^#INFO:" ${CFG}` -gt 0
          then
            echo
            grep "^#INFO:" ${CFG} | sed "s=^#INFO:==g"
            echo
          fi

          HWM=`grep "^[ ]*model" ${CFG} | awk '{print $2}' | tr -d "'\"" | tr -d " "`
          if test -z "${HWM}"
          then
            echo "[31m[1mThe 'model' parameter was not found in the configuration file.[0m"
            echo "Please check."
          else
            EMUCPU=`grep "^${HWM}=" ${CHARONDIR}/utils/charon_hwmodel.cpus 2>/dev/null | cut -f2 -d'='`
            if test -n "${EMUCPU}"
            then
              echo "${FGBLUE}Model:[0m ${HWM} with ${EMUCPU} CPU"
            else
              echo "${FGBLUE}Model:[0m [31m${HWM}[0m"
              echo "[1mThis 'model' is unknown. Please check.[0m"
            fi
          fi        
          echo

          get_logfile ${CFG}
          echo -n "${FGBLUE}Log file is set to:[0m ${LOGF}"
          test ${ROTA} = 1 && echo " ([36mrotating log file[0m)" || echo " (plain file)"
          if test -z "`echo ${LOGF} | grep '/'`"
          then
            echo "[41m[37m ERROR [0m The full path is not specified."
            echo "        Please update the configuration file."
            test "$1" = "cmd" || Press_Enter
          else
            if test ! -e ${LOGF}
            then
              CHKD=`dirname ${LOGF}`
              if test ! -d ${CHKD}
              then
                echo "Making directory ${CHKD} ..."
                mkdir -p ${CHKD}
              else
                touch ${LOGF} 2>/dev/null
                if test $? = 0
                then
                  rm -f ${LOGF}
                else
                  echo "[41m[37m ERROR [0m The path or the filename is incorrect."
                  echo "        Please update the configuration file."
                  test "$1" = "cmd" || Press_Enter
                fi
              fi
            fi
          fi

          if test ${LOGF} != ${BCK_LOGF} -o ${ROTA} != ${BCK_ROTA}
          then
            echo "[46m[30m WARNING [0m"
            echo " The log file name or its mode has changed !"
            echo " Please update the ${FGBLUE}log monitoring service definition file[0m:"
            echo " Use 'Manage monitored guests logs' option and 'Update log files list'"
            echo " then \"Manage 'systemd' services\" option. Finally restart the service."
            echo " ${FGBLUE}Expected log file in \"ExecStart=\" and \"ExecStop=\" lines is:[0m"
            echo " [1m${LOGF}[0m"
            test "$1" = "cmd" || Press_Enter
          fi

          if test -d ${LOGF}
          then
            echo "[46m[30m WARNING [0m"
            echo " The log file is defined as a folder."
            echo " Rotating log files are supported starting with CHARON-AXP or CHARON-VAX V4.7"
            test "$1" = "cmd" || Press_Enter
          fi

          echo

          if test -n "`grep PBXGA ${CFG}`"
          then
            if test -n "`cat ${CFG} | grep -P '^[ \t]*load[ \t]+PBXGA'`"
            then
              echo "[46m[30m WARNING [0m"
              echo " The PBXGA defined in the configuration file is not compatible with the"
              echo " Linux Toolkit as it starts Charon virtual machines in the background."
              echo " To start this virtual machine, the following command will have to be executed"
              echo " from a terminal session on the Linux server graphical console:"
              tput bold
              echo " # ${EXE} ${CFG}"
              tput sgr0
              test "$1" = "cmd" || Press_Enter
            fi
          fi

          echo
          check_vdisk_files ${CFG} display

          echo
          check_network_settings ${CFG} display

          #-- Displays an alert if virtual machines are not in separate folders
          ( for LINE in `grep -v ^# ${BOOTLIST}`
          do
            dirname `echo ${LINE} | cut -f2 -d';'`
          done ) | sort -u >${BOOTLIST}.tmp.$$
          NBFOLD=`cat ${BOOTLIST}.tmp.$$ | wc -l`
          NBGUES=`grep -v ^# ${BOOTLIST} | wc -l`
          rm -f ${BOOTLIST}.tmp.$$ 2>/dev/null
          if test ${NBFOLD} -ne ${NBGUES}
          then
            echo
            echo "[41m[37m[1m WARNING [0m There are virtual machines using the same folder !"
            echo "          This will cause files lock. Please update accordingly."
          fi

          test "$1" = "cmd" || Press_Enter
        fi
        test ${NGUESTS:-0} -eq 1 -o "$1" = "cmd" && break
      fi
    else
      tput bold
      echo "Incorrect value, must be numeric."
      tput sgr0
      sleep 3
    fi
  fi
done

exit
