#!/bin/bash
## TT#8405 Validate YML schema before the upgrade

set -e

if [ -z "${UPGRADE_VERSION}" ] ; then
  echo "ERROR: Missing mandatory environment variable '\$UPGRADE_VERSION', exiting." >&2
  exit 1
fi

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

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

  echo "REQUEST_CONFIRMATION"
}

echo "Checking possible YML schema corruption:"
if ngcpcfg --validate check ; then
  echo "All YML configuration files were validated successfully"
else
  echo "WARNING: YML configuration file(s) are invalid, see details above."
  echo "         mr6.5+ validates all YML config file before using them."
  echo "         Please proceed here if you know what you are doing ONLY!"

  request_confirmation
fi
