#!/usr/bin/env bash
#
# Liga o "pre-commit" do git: antes de cada commit, roda os testes do
# worker e confere a sintaxe do PHP/JS. Se algo falhar, o commit é barrado.
#   ./scripts/instalar-hooks.sh
set -euo pipefail
RAIZ="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
[ -d "$RAIZ/.git" ] || { echo "Esta pasta não é um repositório git (rode 'git init' antes)."; exit 1; }
cat > "$RAIZ/.git/hooks/pre-commit" <<'H'
#!/usr/bin/env bash
cd "$(git rev-parse --show-toplevel)"
python3 -m unittest discover -s python-worker/tests -q || { echo "Testes do worker falharam - commit cancelado."; exit 1; }
for f in $(git diff --cached --name-only --diff-filter=ACM | grep -E '\.php$'); do php -l "$f" >/dev/null || exit 1; done
for f in $(git diff --cached --name-only --diff-filter=ACM | grep -E '\.js$'); do node --check "$f" || exit 1; done
H
chmod +x "$RAIZ/.git/hooks/pre-commit"
echo "pre-commit instalado."
