#!/bin/sh
#-------------------------------------------------------------------------------
# charon_menu_editchkrun
#-------------------------------------------------------------------------------
# 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"
GUESTCHKR="${CHARONDIR}/utils/charon_gstart.chkrun"
LOGFLIST="${CHARONDIR}/utils/charon_logchk.list"

function addvm_chkrunscript
{
  grep -v "^#" ${BOOTLIST} | while read LINE
  do
    CFG=`echo ${LINE} | cut -f2 -d';'`
    grep -q "${CFG})" ${GUESTCHKR}
    if test $? -gt 0
    then
      if test -n "`grep '^TITLE=Charon-PAR' ${CHARONDIR}/utils/charon_menumain.dat`"
      then
        #--- Charon-PAR
        ed ${GUESTCHKR} <<EOFPAR >/dev/null 2>&1
/^in$
a
  ${CFG})
    #-- Uncomment and complete one of the following groups:
    #
    #--- Group1: HP-UX example with ssh (replace <guest> by guest hostname):
    #ssh -o ConnectTimeout=2 <guest> "uptime|cut -f1 -d','" 2>/dev/null
    #RETVAL=\$?
    #
    #--- Group2: HP-UX example with rsh (replace <guest> by guest hostname):
    #rsh <guest> "uptime|cut -f1 -d','" 2>/dev/null
    #RETVAL=\$?
    #
    #--- Group3: MPE/iX example with ping (replace <guest> by guest hostname):
    #ping -c3 -W1 -q <guest> >/dev/null 2>&1
    #RETVAL=\$?
    #test \${RETVAL} = 0 && echo "Alive." || echo "No response to ping"
    #
    #--- Group4: Custom script:
    #/<path>/<myscript>
    #RETVAL=\$?
    #
    ;;
.
w
q
EOFPAR
      else
        #--- Charon-AXP & Charon-VAX
        ed ${GUESTCHKR} <<EOFAXP >/dev/null 2>&1
/^in$
a
  ${CFG})
    #-- Uncomment and complete one of the following lines:
    #
    #--- Tru64 example with ssh (replace <guest> by guest hostname):
    #ssh -o ConnectTimeout=2 <guest> "uptime|cut -f1 -d','" 2>/dev/null
    #
    #--- OpenVMS example with ssh (replace <guest> by guest hostname):
    #ssh -o ConnectTimeout=2 system@<guest> "write sys\$output \"Booted ''f\$getsyi(\"boottime\")'\"" 2>/dev/null
    #
    #--- Custom script:
    #/<path>/<myscript>
    #
    RETVAL=\$?
    ;;
.
w
q
EOFAXP
      fi
    fi
  done
}

function build_chkrunscript
{
  if test -s ${BOOTLIST}
  then
    if test ! -e ${GUESTCHKR}
    then
      echo "Initializing custom guest check/run script..."

      echo "#!/bin/sh" >${GUESTCHKR}
      echo "#" >>${GUESTCHKR}
      echo "# Parameter \$1: contains full path to cfg file" >>${GUESTCHKR}
      echo "#" >>${GUESTCHKR}
      echo "#  Important notes:" >>${GUESTCHKR}
      echo "#  - comments & commands must be on separate lines" >>${GUESTCHKR}
      echo "#  - respect the structure of the file with case/in/esac" >>${GUESTCHKR}
      echo "#  - leave the 'RETVAL' lines untouched" >>${GUESTCHKR}
      echo "#  - only place your commands between the selection and the ';;' line" >>${GUESTCHKR}
      echo "#" >>${GUESTCHKR}
      #echo ". /opt/charon/utils/charon_common" >>${GUESTCHKR}
      echo "#" >>${GUESTCHKR}
      echo "case \"\$1\"" >>${GUESTCHKR}
      echo "in" >>${GUESTCHKR}
      echo "  *)" >>${GUESTCHKR}
      echo "    echo \"Case not set for '\$1'\"" >>${GUESTCHKR}
      echo "    RETVAL=1" >>${GUESTCHKR}        
      echo "    ;;" >>${GUESTCHKR}        
      echo "esac" >>${GUESTCHKR}        
      echo "echo \"==RETVAL=\${RETVAL}==\"" >>${GUESTCHKR}
      echo "exit ${RETVAL}" >>${GUESTCHKR}
      echo "Done."
      chmod u+x ${GUESTCHKR}
    fi
    addvm_chkrunscript
  else
    echo "There is no virtual machine (guest) defined."
    echo "It is not necessary then to define a check/run script"
    Press_Enter
    echo
    exit 1
  fi
}


while test 1
do
  display_header "Edit Virtual Machines check/run 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
- let the 'RETVAL' lines untouched

[44m[37m Available options [0m
EOF
  echo "[1m1 - Edit the check/run script[m"
  test -x ${GUESTCHKR} && echo -n "[1m"
  echo "2[0m - Virtual machines list (check script execution)"
  echo ""

  while test 1
  do
    echo -n "Select your option or 'q' to quit: "
    read ANS
    case "${ANS}"
    in
      1)
        build_chkrunscript
        ask_editor noquit
        if test -n "${EDI}"
        then
          if test -z "`echo ${EDI}|grep vim`"
          then
            ${EDI} ${GUESTCHKR} 2>/dev/null
          else
            ${EDI} -u ${CHARONDIR}/utils/charonscript.vimrc ${GUESTCHKR} 2>/dev/null
          fi
        fi
        break
        ;;
      2)
        NGUESTS=`grep -v ^# ${BOOTLIST} | wc -l`
        if test ${NGUESTS:-0} -eq 0
        then 
          tput bold
          echo "Guests list is empty."
          tput sgr0
        else
          if test -x ${CHARONDIR}/utils/charon_gstart.chkrun
          then
            display_guestslist
          else
            echo "${CHARONDIR}/utils/charon_gstart.chkrun script not found or not executable."
          fi
        fi
        Press_Enter
        break
        ;;
      q|Q)
        break 2
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done
done

exit
