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

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

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

function addvm_stopscript
{
  grep -v "^#" ${BOOTLIST} | while read LINE
  do
    CFG=`echo ${LINE} | cut -f2 -d';'`
    grep -q "${CFG})" ${GUESTSTOP}
    if test $? -gt 0
    then
      ed ${GUESTSTOP} <<EOF >/dev/null 2>&1
/^in$
a
  ${CFG})
    #-- Uncomment and complete one of the following lines:
    #---- MPEiX
    #\${CHARONDIR}/utils/charon_gstop_expect \$1 \$2 X X X MPEIX
    #
    #---- HP-UX (update password, prompt and hostname)
    #\${CHARONDIR}/utils/charon_gstop_expect \$1 \$2 root <password> "<prompt>" HPUX
    #\${CHARONDIR}/utils/charon_gstop_rsh \$1 \$2 <hostname> HPUX
    #\${CHARONDIR}/utils/charon_gstop_ssh \$1 \$2 <hostname> HPUX
    #
    #---- Custom script
    #/<path>/<myscript>
    ;;
.
w
q
EOF
    fi
  done
}

function build_stopscript
{
  if test -s ${BOOTLIST}
  then
    if test ! -e ${GUESTSTOP}
    then
      echo "Initializing custom guest stop script..."

      echo "#!/bin/bash" >${GUESTSTOP}
      echo "#" >>${GUESTSTOP}
      echo "# Parameter \$1: contains full path to cfg file" >>${GUESTSTOP}
      echo "# Parameter \$2: contains full path to emulator exe file" >>${GUESTSTOP}
      echo "#" >>${GUESTSTOP}
      echo "#  Important notes:" >>${GUESTSTOP}
      echo "#  - comments & commands must be on separate lines" >>${GUESTSTOP}
      echo "#  - respect the structure of the file with case/in/esac" >>${GUESTSTOP}
      echo "#  - only place your commands between the selection and the ';;' line" >>${GUESTSTOP}
      echo "#" >>${GUESTSTOP}
      echo ". /opt/charon/utils/charon_common" >>${GUESTSTOP}
      echo "#" >>${GUESTSTOP}
      echo "case \"\$1\"" >>${GUESTSTOP}
      echo "in" >>${GUESTSTOP}
      echo "  *)" >>${GUESTSTOP}
      echo "    echo \"Invalid parameter '\$1'\"" >>${GUESTSTOP}
      echo "    exit 1" >>${GUESTSTOP}        
      echo "    ;;" >>${GUESTSTOP}        
      echo "esac" >>${GUESTSTOP}        
      echo "Done."
      chmod u+x ${GUESTSTOP}
    fi
    addvm_stopscript
  else
    echo "There is no virtual machine (guest) defined."
    echo "It is not necessary then to define a stop script"
    Press_Enter
    echo
    exit 1
  fi
}

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

    display_guestslist -num
    echo
    echo "${BGORANGE} WARNING [0m"
    echo "Executing the stop script could have a dramatic impact on your system."
    echo
    printf "Select the guest you want to test (q to quit): "
    read ANS
    test "${ANS}" = "q" && break

    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
          CHARON_EXE=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f1 -d';'`
          CHARON_CFG=`grep -v ^# ${BOOTLIST} | head -${ANS} | tail -1 | cut -f2 -d';'`
          CHK=`ps -ef | grep -w ${CHARON_EXE} | grep "\-f ${CHARON_CFG}"`

          if test -z "${CHK}"
          then
            echo "${BGORANGE} WARNING [0m"
            echo "The virtual machine is not running. Cannot execute stop script."
            Press_Enter
          else
            if test -x ${CHARONDIR}/utils/charon_gstart.stop
            then
              ${CHARONDIR}/utils/charon_gstart.stop ${CHARON_CFG} ${CHARON_EXE}
              RET=$?
              if test ${RET} = 0
              then
                echo "[42m[30m SUCCESS [0m"
              else
                echo "[41m[37m ERROR [0m"
                echo "Stop script returned error code ${RET}"
              fi
              Press_Enter
            else
              echo "[41m[37m ERROR [0m"
              echo "${CHARONDIR}/utils/charon_gstart.stop script not found or not executable."
            fi
          fi
        fi
      else
        tput bold
        echo "Incorrect value, must be numeric."
        tput sgr0
        sleep 3
      fi
    fi
  done
}

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
if test ${INS_MAJOR:-0} -le 6
then
  echo "[41m[37m ERROR [0m This OS version is not supported:"
  cat ${OS_RELEASE}
  exit 42
fi

while test 1
do
  display_header "Edit Virtual Machines stop script"
  if test `id -u` -ne 0
  then
    echo "[41m[37m ERROR [0m must be root !"
    Press_Enter
    echo
    exit 1
  fi
  echo

  cat <<EOF
[45m[37m Important notes: [0m
- Comments & commands must be on separate lines
- Respect the structure of the file with case/in/esac
- Only place your commands between the selection and the ';;' line
- Some scripts are provided to ease the setup of 'rsh', 'ssh' and 'expect'.
  Users can obviously use their own commands to shutdown their systems.

[44m[37m Available options [0m
1 - Read how-to setup 'expect' file
2 - Read how-to setup 'rsh' with HP-UX file
3 - Read how-to setup 'ssh' with HP-UX file

[1m4 - Edit the stop script[0m
5 - Stop script test

EOF

  while test 1
  do
    echo -n "Select your option or 'q' to quit: "
    read ANS
    case "${ANS}"
    in
      1)
        more ${CHARONDIR}/utils/charon_gstop_expect.readme
        Press_Enter
        break
        ;;
      2)
        more ${CHARONDIR}/utils/charon_gstop_rsh_hpux.readme
        Press_Enter
        break
        ;;
      3)
        if test ${INS_MAJOR:-0} -lt 9
        then
          more ${CHARONDIR}/utils/charon_gstop_ssh_hpux.readme
        else
          more ${CHARONDIR}/utils/charon_gstop_ssh_hpux_Linux9.readme
        fi
        Press_Enter
        break
        ;;
      4)
        build_stopscript
        ask_editor noquit
        if test -n "${EDI}"
        then
          if test -z "`echo ${EDI}|grep vim`"
          then
            ${EDI} ${GUESTSTOP} 2>/dev/null
          else
            ${EDI} -u ${CHARONDIR}/utils/charonscript.vimrc ${GUESTSTOP} 2>/dev/null
          fi
        fi
        break
        ;;
      5)
        test_stopscript
        break
        ;;
      q|Q)
        break 2
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done

done

exit
