#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2024 3mdeb <contact@3mdeb.com>
#
# SPDX-License-Identifier: Apache-2.0

source $DTS_ENV
source $DTS_FUNCS

trap : 2
trap : 3

while : ; do
  # Each time the main menu is displayed, check for DES credentials and export
  # them, if file exists
  if [ -e "${SE_credential_file}" ]; then
    export CLOUDSEND_LOGS_URL=$(sed -n '1p' < ${SE_credential_file} | tr -d '\n')
    export CLOUDSEND_DOWNLOAD_URL=$(sed -n '2p' < ${SE_credential_file} | tr -d '\n')
    export CLOUDSEND_PASSWORD=$(sed -n '3p' < ${SE_credential_file} | tr -d '\n')
    export USER_DETAILS="$CLOUDSEND_DOWNLOAD_URL:$CLOUDSEND_PASSWORD"
    export DES_IS_LOGGED="true"
  else
    export CLOUDSEND_LOGS_URL="$BASE_CLOUDSEND_LOGS_URL"
    export CLOUDSEND_PASSWORD="$BASE_CLOUDSEND_PASSWORD"
    unset CLOUDSEND_DOWNLOAD_URL
    unset DES_IS_LOGGED
  fi

  show_header
  show_des_credentials
  show_ssh_info
  show_menu

  echo
  read OPTION
  echo

  case ${OPTION} in
    ${HCL_REPORT_OPT})
      print_disclaimer
      read -p "Do you want to support Dasharo development by sending us logs with your hardware configuration? [N/y] "
      case ${REPLY} in
          yes|y|Y|Yes|YES)
          export SEND_LOGS="true"
          echo "Thank you for contributing to the Dasharo development!"
          ;;
          *)
          export SEND_LOGS="false"
          echo "Logs will be saved in root directory."
          echo "Please consider supporting Dasharo by sending the logs next time."
          ;;
      esac
      if [ "${SEND_LOGS}" == "true" ]; then
          # DEPLOY_REPORT variable is used in dasharo-hcl-report to determine
          # which logs should be printed in the terminal, in the future whole
          # dts scripting should get some LOGLEVEL and maybe dumping working
          # logs to file
          export DEPLOY_REPORT="false"
          check_network_connection && ${CMD_DASHARO_HCL_REPORT} && logs_sent="1"
      else
          export DEPLOY_REPORT="false"
          ${CMD_DASHARO_HCL_REPORT}
      fi
      read -p "Press ENTER to continue."
      ;;
    ${DASHARO_FIRM_OPT})
      if ! check_if_dasharo; then
        if check_network_connection; then
          echo "Preparing ..."
          if [ -z "${logs_sent}" ]; then
            export SEND_LOGS="true"
            export DEPLOY_REPORT="true"
            if ! ${CMD_DASHARO_HCL_REPORT}; then
              echo -e "Unable to connect to cloud.3mdeb.com for submitting the
                        \rHCL report. Please recheck your internet connection."
            else
              logs_sent="1"
            fi
          fi
        fi

        if [ -n "${logs_sent}" ]; then
          ${CMD_DASHARO_DEPLOY} install
        fi
      else
        # For NovaCustom TGL laptops with Dasharo version lower than 1.3.0,
        # we shall run the ec_transition script instead. See:
        # https://docs.dasharo.com/variants/novacustom_nv4x_tgl/releases/#v130-2022-10-18
        if [ "$SYSTEM_VENDOR" = "Notebook" ]; then
            case "$SYSTEM_MODEL" in
              "NS50_70MU"|"NV4XMB,ME,MZ")
                compare_versions $DASHARO_VERSION 1.3.0
                if [ $? -eq 1 ]; then
                # For Dasharo version lesser than 1.3.0
                  print_warning "Detected NovaCustom hardware with version < 1.3.0"
                  print_warning "Need to perform EC transition after which the platform will turn off"
                  print_warning "Then, please power it on and proceed with update again"
                  print_warning "EC transition procedure will start in 5 seconds"
                  sleep 5
                  ${CMD_EC_TRANSITION}
                  error_check "Could not perform EC transition"
                fi
                # Continue with regular update process for Dasharo version
                #  greater or equal 1.3.0
                ;;
            esac
        fi

        # Use regular update process for everything else
        ${CMD_DASHARO_DEPLOY} update
      fi
      read -p "Press ENTER to continue."
      ;;
    ${REST_FIRM_OPT})
      if check_if_dasharo; then
        ${CMD_DASHARO_DEPLOY} restore
      fi
      read -p "Press ENTER to continue."
      ;;
    ${DES_KEYS_OPT})
      echo ""
      read -p "Enter logs key:                " 'TMP_CLOUDSEND_LOGS_URL'
      echo ""
      read -p "Enter firmware download key:   " 'TMP_CLOUDSEND_DOWNLOAD_URL'
      echo ""
      read -p "Enter password:                " 'TMP_CLOUDSEND_PASSWORD'

      echo ${TMP_CLOUDSEND_LOGS_URL} > ${SE_credential_file}
      echo ${TMP_CLOUDSEND_DOWNLOAD_URL} >> ${SE_credential_file}
      echo ${TMP_CLOUDSEND_PASSWORD} >> ${SE_credential_file}

      print_ok "Dasharo DES credentials have been saved"
      echo "Verifying Dasharo DES credentials..."

      check_se_creds
      if [ $? -eq 0 ]; then
         print_ok "Verification of the Dasharo DES was successful. They are valid and will be used."
      else
        echo -e "Something may be wrong with the DES credentials. Please use option 4 to change the DES keys
                  \rand make sure that there is no typo."
        rm ${SE_credential_file}
        export CLOUDSEND_LOGS_URL="$BASE_CLOUDSEND_LOGS_URL"
        export CLOUDSEND_PASSWORD="$BASE_CLOUDSEND_PASSWORD"
        unset CLOUDSEND_DOWNLOAD_URL
        unset DES_IS_LOGGED
      fi
      read -p "Press ENTER to continue."
      ;;
    ${SSH_OPT_UP} | ${SSH_OPT_LOW})
      if systemctl is-active sshd.service > /dev/null 2>&1; then
        print_ok "Turning off the SSH server..."
        systemctl stop sshd.service
      else
        print_warning "Starting SSH server!"
        print_warning "Now you can log in into the system using root account."
        print_warning "Stopping server will not drop all connected sessions."
        systemctl start sshd.service
        print_ok "Listening on IPs: $(ip -br -f inet a show scope global | grep UP | awk '{ print $3 }' | tr '\n' ' ')"
      fi
      read -p "Press ENTER to continue."
      ;;
    ${SHELL_OPT_UP} | ${SHELL_OPT_LOW})
      echo "Entering shell, to leave type exit and press Enter or press LCtrl+D"
      echo ""
      ${CMD_SHELL}
      ;;
    ${POWEROFF_OPT_UP} | ${POWEROFF_OPT_LOW})
      ${CMD_POWEROFF}
      ;;
    ${REBOOT_OPT_UP} | ${REBOOT_OPT_LOW})
      ${CMD_REBOOT}
      ;;
    *)
      ;;
  esac
done
