#!/bin/bash
#-------------------------------------------------------------------------------
# charon_menu_VEesxibind
#-------------------------------------------------------------------------------
# Copyright (C) 2013-2023 STROMASYS.
# All rights reserved.
# Products: AXP/VAX + PAR
#-------------------------------------------------------------------------------
#
VERSION=2.1

. /opt/charon/utils/charon_common
. /opt/charon/utils/charon_common_menu

EXCODE=0
ESXIBIND="${ETCCHARON}/CharonToolkit.esxibind"

while test 1
do
  if test $(id -u) -ne 0
  then
    display_header "VMware authentication setup"
    echo "[31mERROR[0m: must be root !"
    EXCODE=1
    break
  fi

  if test ! -x /opt/license-server/esxi_bind
  then
    display_header "VMware authentication setup"
    echo "[31mERROR[0m: esxi_bind executable cannot be found in /opt/license-server folder !"
    EXCODE=7
    break
  fi

  if test -x /opt/license-server/license_server
  then
    SVCSTA=$(systemctl is-active licensed)
    if test "${SVCSTA}" = "active"
    then
      #--- Loop execute and retry
      DOEXECRETRY=1
      while test ${DOEXECRETRY} = 1
      do
        DOCONFIRM=0
        #--- Loop enter and correct
        while test ${DOCONFIRM} = 0
        do
          display_header "VMware authentication setup"
          cat <<EOF
You will be prompted for IP address, username and password.
Corrections can be made before validating your input.

EOF
          DEFADDR=$(grep ^ADDR= ${ESXIBIND} 2>/dev/null | cut -f2 -d'=')
          DEFUSER=$(grep ^USER= ${ESXIBIND} 2>/dev/null | cut -f2 -d'=')

          while test 1
          do
            if test -n "${DEFADDR}"
            then
              echo -n "${FGBLUE}IP address [${DEFADDR}] (q to quit):[0m "
            else
              echo -n "${FGBLUE}IP address (q to quit):[0m "
            fi
            read ESXADDR
            test -z "${ESXADDR}" && ESXADDR=${DEFADDR}
            test "${ESXADDR}" = "q" && break 4
            test "${ESXADDR}" = "Q" && break 4
            tput cuu1;tput el
            if test -n "${ESXADDR}"
            then
              echo "${FGBLUE}IP address:[0m ${ESXADDR}"
              DEFADDR=${ESXADDR}
              echo "ADDR=${DEFADDR}"  >${ESXIBIND}
              echo "USER=${DEFUSER}" >>${ESXIBIND}
              break
            fi
          done

          while test 1
          do
            if test -n "${DEFUSER}"
            then
              echo -n "${FGBLUE}Username [${DEFUSER}] (q to quit):[0m "
            else
              echo -n "${FGBLUE}Username (q to quit):[0m "
            fi
            read ESXUSER
            test -z "${ESXUSER}" && ESXUSER=${DEFUSER}
            test "${ESXUSER}" = "q" && break 4
            test "${ESXUSER}" = "Q" && break 4
            tput cuu1;tput el
            if test -n "${ESXUSER}"
            then
              echo "${FGBLUE}Username:[0m ${ESXUSER}"
              DEFUSER=${ESXUSER}
              echo "ADDR=${DEFADDR}"  >${ESXIBIND}
              echo "USER=${DEFUSER}" >>${ESXIBIND}
              break
            fi
          done

          while test 1
          do
            echo -n "${FGBLUE}Password (q to quit):[0m "
            stty -echo
            read ESXPASS
            stty echo; echo
            test "${ESXPASS}" = "q" && break 4
            test "${ESXPASS}" = "Q" && break 4
            tput cuu1;tput el
            if test -n "${ESXPASS}"
            then
              echo "${FGBLUE}Password:[0m *****"
              break
            fi
          done

          echo
          while test 1
          do
            echo -n "${FGBLUE}Please confirm (y, n, q to quit):[0m "
            read ANS
            case "${ANS}"
            in
              y|Y)
                DOCONFIRM=1
                break
                ;;
              n|N)
                break
                ;;
              q|Q)
                break 4
                ;;
              *)
                tput cuu1;tput el
                ;;
            esac
          done
        done
        #--- Execute the esxi_bind command
        echo
        /opt/license-server/esxi_bind -a "${ESXADDR}" -u "${ESXUSER}" -p "${ESXPASS}"
        RET=$?
        if test ${RET} -eq 0
        then
          echo "Success."
          DOEXECRETRY=0
        else
          echo "${FGORANGE}WARNING[0m: esxi_bind command failed with error code ${RET}"
          echo
          while test 1
          do
            echo -n "${FGBLUE}Do you want to retry with new parameters (y/n):[0m "
            read ANS
            case "${ANS}"
            in
              y|Y)
                break
                ;;
              n|N)
                DOEXECRETRY=0
                break
                ;;
              *)
                tput cuu1;tput el
                ;;
            esac
          done
        fi
      done
      break
    else
      echo
      echo "${FGORANGE}WARNING[0m: 'licensed' service is not active, state is '${SVCSTA}'"
      echo "         Please ensure you are running this utility on the license server."
      echo "         If yes, please check the service state."
      EXCODE=2
      break
    fi
  else
    echo
    echo "${FGORANGE}WARNING[0m: License server package is not installed (VE license) !"
    echo "         Please ensure you are running this utility on the license server."
    EXCODE=2
    break
  fi
done

echo
exit ${EXCODE:-0}
