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

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

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

function addvm_prestartscript
{
  grep -v "^#" ${BOOTLIST} | while read LINE
  do
    CFG=`echo ${LINE} | cut -f2 -d';'`
    grep -q "${CFG})" ${GUESTPRESTART}
    if test $? -gt 0
    then
      ed ${GUESTPRESTART} <<EOF >/dev/null 2>&1
/^in$
a
  ${CFG})
    ;;
.
w
q
EOF
    fi
  done
}

function build_prestartscript
{
  if test -s ${BOOTLIST}
  then
    if test ! -e ${GUESTPRESTART}
    then
      echo "Initializing custom guest pre-start script..."

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


while test 1
do
  display_header "Edit Virtual Machines pre-start 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
- do not add any 'exit' command in the script unless you want the virtual
  machine not to be started if the script fails

[44m[37m Available options [0m
[1m1 - Edit the pre-start script[m

EOF

  while test 1
  do
    echo -n "Select your option or 'q' to quit: "
    read ANS
    case "${ANS}"
    in
      1)
        build_prestartscript
        ask_editor noquit
        if test -n "${EDI}"
        then
          if test -z "`echo ${EDI}|grep vim`"
          then
            ${EDI} ${GUESTPRESTART} 2>/dev/null
          else
            ${EDI} -u ${CHARONDIR}/utils/charonscript.vimrc ${GUESTPRESTART} 2>/dev/null
          fi
        fi
        break
        ;;
      q|Q)
        break 2
        ;;
      *)
        tput cuu1;tput el
        ;;
    esac
  done

done

exit
