#!/bin/bash

set -euo pipefail

die() {
  echo "ERROR: $*" >&2
  exit 1
}

request_confirmation() {
  if [[ "${FORCE_UPGRADE}" = "true" ]] ; then
    echo "Forcing as requested via environment variable FORCE_UPGRADE."
    return
  fi

  while true; do
    echo -n "Should the upgrade continue? (yes/no): "
    read -r answer
    case "${answer,,}" in
      yes)
        echo "Continue as requested."
        break
        ;;
      no)
        die "Aborted as requested."
        ;;
      * )
        echo "Please answer 'yes' or 'no'."
        ;;
    esac
    unset answer
  done
}

if [[ -z "${FORCE_UPGRADE}" ]]; then
  echo "ERROR: Missing mandatory environment variable '\$FORCE_UPGRADE', exiting."
fi

echo "If necessary place your customtt/patchtt files to /ngcp-fallback/etc/ngcp-config/ directory."
echo "When all the required files are copied, continue the upgrade."
echo "if you don't use customtt/patchtt files just continue the upgrade."
request_confirmation
