#!/bin/bash
# Anexa as strings "Sobre" com "GNU/Linux" a TODOS os .mo ingleses (en*) do
# mate-desktop, para o texto em inglês também exibir "GNU/Linux".
set -u
CHROOT="$1"; C="$CHROOT"

msg1="MATE provides an intuitive and attractive desktop to Linux users using traditional metaphors."
msg1t="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!"
msg2t="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!"

for mo in "$C"/usr/share/locale/en*/LC_MESSAGES/mate-desktop.mo; do
    [ -f "$mo" ] || continue
    # pula se já tem GNU/Linux
    if strings "$mo" 2>/dev/null | grep -q "GNU/Linux"; then continue; fi
    tmpdir=$(mktemp -d)
    po="$tmpdir/mate-desktop.po"
    msgunfmt "$mo" -o "$po" 2>/dev/null || { rm -rf "$tmpdir"; continue; }
    # se o .mo ja tem a msgid "Linux" (sem GNU), substitui msgstr; se nao, anexa
    if grep -q "Linux users using traditional" "$po" || grep -q "popular Linux desktop" "$po"; then
        # substituir "Linux" por "GNU/Linux" nas linhas de msgid/msgstr dessas duas frases
        perl -0pi -e 's/to Linux users using traditional metaphors\./to GNU\/Linux users using traditional metaphors./g; s/popular Linux desktop/popular GNU\/Linux desktop/g' "$po"
    else
        cat >> "$po" <<POEOF

# GNUcego: substitui "Linux" por "GNU/Linux" no Sobre (mate-about)
msgid "$msg1"
msgstr "$msg1t"

msgid "$msg2"
msgstr "$msg2t"
POEOF
    fi
    msgfmt "$po" -o "$mo" 2>/dev/null
    echo "  $(basename "$(dirname "$(dirname "$mo")")"): GNU/Linux aplicado"
    rm -rf "$tmpdir"
done
echo "CONCLUÍDO"
