#!/usr/bin/env bash
# Revisa con PHP 5.6.40 real (imagen oficial) la sintaxis de los PHP que este
# trabajo tocó. `php -l` con PHP 7/8 no sirve: acepta sintaxis que 5.6 rechaza.
#
#   docker run --rm -v "C:\laragon\www\mbinv:/app" -w /app php:5.6-cli \
#     bash scripts/testing/hoja-lint-php56.sh
#
# Sin argumentos revisa los archivos del diff contra main; con argumentos
# revisa exactamente los que se le pasen.
set -u

if [ "$#" -gt 0 ]; then
  ARCHIVOS="$*"
elif [ -f /tmp/hoja-archivos.txt ]; then
  # La lista puede venir de PowerShell, con CRLF: el \r se pega al nombre y
  # ningún archivo existe.
  ARCHIVOS="$(tr -d '\r' < /tmp/hoja-archivos.txt)"
else
  ARCHIVOS="$(find src/MB/ProductoBundle/Clases/Hoja \
                   src/MB/ProductoBundle/Clases/Documento \
                   src/MB/ProductoBundle/Tests/Clases \
                   -name '*.php' 2>/dev/null)
             src/MB/ProductoBundle/Controller/HojaController.php
             src/MB/ProductoBundle/Controller/DocumentoController.php
             src/MB/ProductoBundle/Clases/Catalogos/CatalogoDatos.php"
fi

php -v | head -1
fallo=0
total=0
for f in $ARCHIVOS; do
  [ -f "$f" ] || continue
  total=$((total + 1))
  if ! php -l "$f" > /tmp/salida-lint 2>&1; then
    echo "-- $f"
    cat /tmp/salida-lint
    fallo=1
  fi
done

echo "archivos revisados: $total"
if [ "$fallo" -eq 0 ]; then
  echo "OK: sintaxis valida en PHP 5.6"
else
  echo "FALLO: hay sintaxis que PHP 5.6 no entiende"
fi
exit "$fallo"
