#! /bin/bash
set -euo pipefail

cd lang

# Fonction: s'assure que l'en-tête du .po contient un charset UTF-8 (et 8bit)
fix_po_header() {
  local f="$1"
  local base lang now pkg_version project_id

  base="$(basename "$f")"
  lang="${base#graphcomment-comment-system-}"
  lang="${lang%.po}"
  now="$(date -u +"%Y-%m-%d %H:%M%z")"

  # Lire la version depuis package.json (si présent)
  if [ -f ../package.json ]; then
    pkg_version="$(sed -n 's/.*"version":[[:space:]]*"\([^"]*\)".*/\1/p' ../package.json | head -n1 || true)"
  fi
  [ -z "${pkg_version:-}" ] && pkg_version="0.0.0"
  project_id="graphcomment-wp-plugin ${pkg_version}"

  # 1) Si l'en-tête gettext (msgid "") est absent, on le préfixe avec un header minimal valide
  if ! grep -q '^msgid ""$' "$f"; then
    {
      printf '%s\n' 'msgid ""'
      printf '%s\n' 'msgstr ""'
      printf '"Project-Id-Version: %s\\n"\n' "$project_id"
      printf '"Report-Msgid-Bugs-To: support@graphcomment.com\\n"\n'
      printf '"POT-Creation-Date: %s\\n"\n' "$now"
      printf '"PO-Revision-Date: %s\\n"\n' "$now"
      printf '"Last-Translator: \\n"\n'
      printf '"Language-Team: \\n"\n'
      printf '"Language: %s\\n"\n' "$lang"
      printf '"MIME-Version: 1.0\\n"\n'
      printf '"Content-Type: text/plain; charset=UTF-8\\n"\n'
      printf '"Content-Transfer-Encoding: 8bit\\n"\n'
      printf '\n'
    } | cat - "$f" > "$f.tmp" && mv "$f.tmp" "$f"
  fi

  # 2) Réécrire entièrement le premier header (entre msgid/msgstr et la ligne vide) de façon canonique
  awk -v now="$now" -v lang="$lang" -v project_id="$project_id" '
    BEGIN{state=0}
    # Début du bloc header
    state==0 && $0 ~ /^msgid ""$/ { print; state=1; next }
    state==1 && $0 ~ /^msgstr ""$/ {
      print
      print "\"Project-Id-Version: " project_id "\\n\""
      print "\"Report-Msgid-Bugs-To: support@graphcomment.com\\n\""
      print "\"POT-Creation-Date: " now "\\n\""
      print "\"PO-Revision-Date: " now "\\n\""
      print "\"Last-Translator: \\n\""
      print "\"Language-Team: \\n\""
      print "\"Language: " lang "\\n\""
      print "\"MIME-Version: 1.0\\n\""
      print "\"Content-Type: text/plain; charset=UTF-8\\n\""
      print "\"Content-Transfer-Encoding: 8bit\\n\""
      print ""
      state=2
      next
    }
    # Ignorer l ancien header tant que ce sont des lignes de chaîne (ou la ligne vide qui le termine)
    state==2 {
      if ($0=="") { state=3; next }
      if ($0 ~ /^"/) { next }
      state=3
    }
    { print }
  ' "$f" > "$f.tmp" && mv "$f.tmp" "$f"
}

# Corriger tous les .po
for f in *.po; do
  fix_po_header "$f"
done

# Valider (optionnel, non bloquant)
for f in *.po; do
  msgfmt --check --verbose "$f" -o /dev/null || true
done

# Compiler tous les .po -> .mo
for f in *.po; do
  msgfmt -o "${f%.po}.mo" "$f"
done

# Recopie des variantes régionales (si vous conservez ce modèle)
cp graphcomment-comment-system-fr.mo graphcomment-comment-system-fr_FR.mo
cp graphcomment-comment-system-fr.mo graphcomment-comment-system-fr_BE.mo
cp graphcomment-comment-system-fr.mo graphcomment-comment-system-fr_CA.mo

cp graphcomment-comment-system-fr.po graphcomment-comment-system-fr_FR.po
cp graphcomment-comment-system-fr.po graphcomment-comment-system-fr_BE.po
cp graphcomment-comment-system-fr.po graphcomment-comment-system-fr_CA.po

cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_AU.mo
cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_CA.mo
cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_NZ.mo
cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_ZA.mo
cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_GB.mo
cp graphcomment-comment-system-en.mo graphcomment-comment-system-en_US.mo

cp graphcomment-comment-system-en.po graphcomment-comment-system-en_AU.po
cp graphcomment-comment-system-en.po graphcomment-comment-system-en_CA.po
cp graphcomment-comment-system-en.po graphcomment-comment-system-en_NZ.po
cp graphcomment-comment-system-en.po graphcomment-comment-system-en_ZA.po
cp graphcomment-comment-system-en.po graphcomment-comment-system-en_GB.po
cp graphcomment-comment-system-en.po graphcomment-comment-system-en_US.po

cd ..