#!/usr/bin/env bash

if ! [ -z $DISABLE_ADDITIONAL_CHECKS ]; then
    echo "Check is disabled"
    exit 0
fi
# -- Get all the envs from the current helmfile installation
ENVS=$(yq '.environments | keys | .[]' ./common/environments.yaml)

ALL_VALUES=$(find ./values -type f)

USED_VALUES=""
for ENV in $ENVS; do
    USED_VALUES="$(helmfile --log-level error -e  $ENV build | yq '.releases[].values[]'):$USED_VALUES"
    USED_VALUES="$(helmfile --log-level error -e $ENV build| yq '.releases[].secrets[]'):$USED_VALUES"
done

UNUSED_VALUES=""
for FILE in $ALL_VALUES; do
    if [[ ${USED_VALUES} != *"$FILE"* ]]; then
        UNUSED_VALUES="${FILE}\n${UNUSED_VALUES}"
    fi
done

if [ -z "${UNUSED_VALUES}" ]; then
    exit 0;
fi

printf "\n ** There are unused values in the repo ** \n"
printf "${UNUSED_VALUES}\n"
printf "Please remove them from the repo to keep it clean"
exit 1