#!/bin/bash
# gnucego-fix-3pontos.sh — 3 correções:
#  1. Reinstalar network-manager-gnome (ícone de wifi)
#  2. Barra de título do GNUcego-Dark PRETA (gradiente, sem bar.png)
#  3. "Linux" -> "GNU/Linux" nos locales pt, pt_BR, en* do mate-desktop
# Copyright (C) 2026 GNUcego contributors — GPLv3+
set -e

C="$1"
[ -d "$C" ] || { echo "uso: $0 <chroot>"; exit 1; }

echo "== fix 3 pontos: $C =="

# ---------------------------------------------------------------
# 1) network-manager-gnome (ícone de wifi na bandeja)
# ---------------------------------------------------------------
echo "  [1/3] network-manager-gnome (wifi)..."
systemd-nspawn -D "$C" bash -c "
  apt-get update -qq 2>&1 | tail -1
  DEBIAN_FRONTEND=noninteractive apt-get install -y network-manager-gnome 2>&1 | tail -3
" 2>/dev/null

# ---------------------------------------------------------------
# 2) barra de título do GNUcego-Dark: preta (trocar bar.png por gradiente)
# ---------------------------------------------------------------
echo "  [2/3] barra de título GNUcego-Dark (preta)..."
META="$C/usr/share/themes/GNUcego-Dark/metacity-1/metacity-theme-3.xml"
if [ -f "$META" ]; then
    # substituir o bloco titlebar_fill_focused (image bar.png) por gradiente preto
    python3 - "$META" <<'PYEOF'
import sys, re
path = sys.argv[1]
txt = open(path).read()
# bloco titlebar_fill_focused: trocar <image filename="bar.png".../> por gradiente preto
old_focused = re.search(r'<draw_ops name="titlebar_fill_focused">.*?</draw_ops>', txt, re.DOTALL)
if old_focused:
    new_focused = '''<draw_ops name="titlebar_fill_focused">
\t<gradient type="vertical" x="0" y="0" width="width" height="height">
\t\t<color value="blend/#000000/#1a1a1a/0.4" />
\t\t<color value="blend/#000000/#1a1a1a/0.11" />
\t\t<color value="#000000" />
\t\t<color value="#000000" />
\t</gradient>
</draw_ops>'''
    txt = txt.replace(old_focused.group(0), new_focused)

# também o _alt (para dialogos focado)
old_alt = re.search(r'<draw_ops name="titlebar_fill_focused_alt">.*?</draw_ops>', txt, re.DOTALL)
if old_alt:
    new_alt = '''<draw_ops name="titlebar_fill_focused_alt">
\t<gradient type="vertical" x="0" y="0" width="width" height="height">
\t\t<color value="blend/#000000/#333333/0.6" />
\t\t<color value="blend/#000000/#333333/0.12" />
\t\t<color value="#000000" />
\t</gradient>
</draw_ops>'''
    txt = txt.replace(old_alt.group(0), new_alt)

# mesma coisa para unfocused
old_unfocused = re.search(r'<draw_ops name="titlebar_fill_unfocused">.*?</draw_ops>', txt, re.DOTALL)
if old_unfocused:
    new_unfocused = '''<draw_ops name="titlebar_fill_unfocused">
\t<gradient type="vertical" x="0" y="0" width="width" height="height">
\t\t<color value="blend/#000000/#111111/0.3" />
\t\t<color value="#000000" />
\t</gradient>
</draw_ops>'''
    txt = txt.replace(old_unfocused.group(0), new_unfocused)

open(path, 'w').write(txt)
print("  titlebar do Dark -> gradiente preto")
PYEOF
fi

# ---------------------------------------------------------------
# 3) "Linux" -> "GNU/Linux" nos locales pt, pt_BR, en* do mate-desktop
# ---------------------------------------------------------------
echo "  [3/3] GNU/Linux nos locales..."
python3 - "$C" <<'PYEOF'
import sys, subprocess, os, tempfile
chroot = sys.argv[1]

# msgids em inglês (originais do binário)
msg1 = "MATE provides an intuitive and attractive desktop to Linux users using traditional metaphors."
msg1g = "MATE provides an intuitive and attractive desktop to GNU/Linux users using traditional metaphors."
msg2 = "GNOME 2 was the most popular Linux desktop but it's no longer available... MATE is here to provide that same desktop to you!"
msg2g = "GNOME 2 was the most popular GNU/Linux desktop but it's no longer available... MATE is here to provide that same desktop to you!"

locales_dir = os.path.join(chroot, "usr/share/locale")
for locale in sorted(os.listdir(locales_dir)):
    mo = os.path.join(locales_dir, locale, "LC_MESSAGES", "mate-desktop.mo")
    if not os.path.isfile(mo):
        continue
    # descompilar
    po = subprocess.run(["msgunfmt", mo], capture_output=True, text=True).stdout
    if "Linux" not in po:
        continue
    # trocar "Linux" -> "GNU/Linux" nas linhas relevantes (msgstr e msgid dessas frases)
    # substituir "Linux users" -> "GNU/Linux users", "popular Linux desktop" -> "popular GNU/Linux desktop"
    newpo = po
    newpo = newpo.replace("GNU/Linux", "GNU/Linux")  # garantir idempotencia
    # trocar qualquer "Linux" solto por "GNU/Linux" APENAS nas frases do "Sobre"
    newpo = newpo.replace("to Linux users using", "to GNU/Linux users using")
    newpo = newpo.replace("popular Linux desktop", "popular GNU/Linux desktop")
    # traduções pt-pt "utilizadores do sistema Linux" e "mundo Linux"
    newpo = newpo.replace("sistema Linux", "sistema GNU/Linux")
    newpo = newpo.replace("mundo Linux", "mundo GNU/Linux")
    newpo = newpo.replace("do Linux", "do GNU/Linux")
    newpo = newpo.replace("de Linux", "de GNU/Linux")
    if newpo != po:
        tmp = tempfile.NamedTemporaryFile(suffix=".po", delete=False, mode="w")
        tmp.write(newpo)
        tmp.close()
        subprocess.run(["msgfmt", tmp.name, "-o", mo], check=True)
        os.unlink(tmp.name)
        print(f"  {locale}: GNU/Linux aplicado")
PYEOF

echo "CONCLUÍDO: $C"
