#!/usr/bin/env bash
#
# check_env_usage.sh
# Loops over .env line by line, extracts each ENV_VAR key,
# then uses 'git grep -i' to see if that key is referenced in the code.
# If it is found, log the key to 'required_vars.log'.

ENV_FILE=".env"
LOG_FILE="required_vars.log"

# Start fresh
> "$LOG_FILE"

while IFS= read -r line; do
  # Skip empty lines or lines without '='
  [[ -z "$line" || "$line" != *=* ]] && continue

  # Extract the key (left of the '='), stripping whitespace
  KEY=$(echo "$line" | cut -d= -f1 | xargs)

  # If KEY is empty (e.g. malformed line), skip
  [[ -z "$KEY" ]] && continue

  # Use git grep -i for a case-insensitive search
  if git grep -iq "$KEY"; then
    echo "$KEY" >> "$LOG_FILE"
  fi
done < "$ENV_FILE"

echo "Done. 'required_vars.log' now contains any .env keys found in code."