#!/usr/bin/env bash
# save as find_env_issue.sh and run: bash find_env_issue.sh

PHP_BIN="/opt/lampp/bin/php"   # change if needed
ROOT="$(pwd)"
INPUT="$ROOT/.env.full"
OUT="$ROOT/.env"

> "$OUT"
n=0
while IFS= read -r line || [ -n "$line" ]; do
  n=$((n+1))
  printf "%s\n" "$line" >> "$OUT"
  "$PHP_BIN" -r "require 'vendor/autoload.php'; try { (new \CodeIgniter\Config\DotEnv(getcwd().'/.env'))->load(); echo 'OK\n'; } catch (Throwable \$e) { echo 'FAIL\n'.get_class(\$e).': '.\$e->getMessage().PHP_EOL; exit(1); }" >/tmp/env_check.log 2>&1
  if [ $? -ne 0 ]; then
    echo \"Problem detected when adding line $n:\"
    sed -n \"${n}p\" \"$INPUT\"
    echo
    echo \"See /tmp/env_check.log for full output.\"
    exit 0
  fi
done < "$INPUT"
echo "No problems found by incremental test (unexpected)."