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

. `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:
    #\${CHARONDIR}/utils/charon_gstop_expect \$1 \$2 root <password> "<prompt>" <TRU64/VMS>
    #\${CHARONDIR}/utils/charon_gstop_rsh \$1 \$2 <hostname> <TRU64/VMS>
    #\${CHARONDIR}/utils/charon_gstop_ssh \$1 \$2 <hostname> <TRU64/VMS>
    #/<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/sh" >${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 "[46m[30m 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 "${CHARON_EXE} -d ${CHARON_CFG}" | grep -v grep`
          if test -z "${CHK}"
          then
            echo "[46m[30m 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
}

while test 1
do
  display_header "Edit Virtual Machines stop script"
  if test `id -u` -ne 0
  then
    tput bold
    echo "Must be root !"
    tput sgr0
    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 Tru64 UNIX file
3 - Read how-to setup 'rsh' with OpenVMS file
4 - Read how-to setup 'ssh' with Tru64 UNIX file
5 - Read how-to setup 'ssh' with OpenVMS file

[1m6 - Edit the stop script[m
7 - 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_tru64.readme
        Press_Enter
        break
        ;;
      3)
        more ${CHARONDIR}/utils/charon_gstop_rsh_ovms.readme
        Press_Enter
        break
        ;;
      4)
        more ${CHARONDIR}/utils/charon_gstop_ssh_tru64.readme
        Press_Enter
        break
        ;;
      5)
        more ${CHARONDIR}/utils/charon_gstop_ssh_ovms.readme
        Press_Enter
        break
        ;;
      6)
        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
        ;;
      7)
        test_stopscript
        break
        ;;
      q|Q)
        break 2
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done

done

exit
