#!/bin/bash
set -euo pipefail

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

TAG=$(git describe --tags `git rev-list --tags --max-count=1` | cut -d'v' -f2)
CHART_VERSION=$(cat $SCRIPTPATH/../../config/helm/aws-node-termination-handler/Chart.yaml | grep 'appVersion:' | xargs | cut -d' ' -f2 | tr -d '[:space:]')
DEFAULT_VALUE=$(cat $SCRIPTPATH/../../config/helm/aws-node-termination-handler/values.yaml | grep 'tag:' | xargs | cut -d' ' -f2 | tr -d '[:space:]')

if [[ "$CHART_VERSION" != "$TAG" || "$DEFAULT_VALUE" != "v$TAG" ]]; then
    echo "NTH Version: $TAG"
    echo "❌ CHART VERSION: $CHART_VERSION"
    echo "❌ Values VERSION: $DEFAULT_VALUE"
    echo "❌ The values.yaml or Chart.yaml is not updated with the correct NTH version"
    exit 1
fi
echo "✅ Successfully checked that the Helm app versions are in-sync with the repo"
