#!/usr/bin/env sh
#
# 自动 hook:i18n 完整性。
# 在 commit 前,如果改动触及 lang/、使用 $lang() 的 .vue/.js,或 lang-bundle.json,
# 就跑一次审计,真问题会拦下 commit。
#
# 启用方法:
#   1) 项目根已有 .husky/pre-commit(本脚本)
#   2) 把这个 commit-msg / pre-commit 加入 husky 钩子(本仓库用 simple-git-hooks
#      或 husky 都行,本脚本不依赖特定框架)
#
# 也可以在 CI 跑:
#   python scripts/i18n_audit.py --strict

set -e

# 检测本次 diff 是否触动相关文件
changed=$(git diff --cached --name-only HEAD)

relevant=$(echo "$changed" | grep -E '(lang/|lang-bundle\.json|\.vue$|\.js$)' | grep -v 'scripts/i18n_audit\|scripts/audit\|scripts/I18N_AUDIT' || true)

if [ -z "$relevant" ]; then
  echo "[pre-commit] No lang-touching files; skip i18n audit."
  exit 0
fi

echo "[pre-commit] Running i18n audit on changed files:"
echo "$relevant" | sed 's/^/  /'

cd "$(git rev-parse --show-toplevel)"

if command -v python >/dev/null 2>&1; then
  if python scripts/i18n_audit.py --strict; then
    echo "[pre-commit] i18n OK"
    exit 0
  else
    echo "[pre-commit] i18n FAILED — 修复方法见 scripts/I18N_AUDIT.md"
    exit 1
  fi
else
  echo "[pre-commit] python not available; skipping"
  exit 0
fi
