#!/bin/bash
# gnucego-fix-final2.sh — 4 correções (sem build):
#  1. GRUB_DISTRIBUTOR: "GNUcego Exu (stable)" / "GNUcego Ogum (testing)" + patch 10_linux
#  2. LightDM: position=50% 50% (centro)
#  3. Plymouth: subtítulo sem "100% livre" + remover texto de status de baixo
#  4. Calamares pós-instalação: remover autologin do lightdm (login funcional)
# Copyright (C) 2026 GNUcego contributors — GPLv3+
set -e

C="$1"
DIST_NAME="$2"   # "Exu" ou "Ogum"
DIST_SUITE="$3"  # "stable" ou "testing"

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

echo "== fix final2: $(basename $C) ($DIST_NAME $DIST_SUITE) =="

# ---------------------------------------------------------------
# 1) GRUB: distribuidor sem "(estavel)" e sem "GNU/Linux" extra
# ---------------------------------------------------------------
GRUB="$C/etc/default/grub"
if [ -f "$GRUB" ]; then
    sed -i "s|^GRUB_DISTRIBUTOR=.*|GRUB_DISTRIBUTOR=\"GNUcego $DIST_NAME ($DIST_SUITE)\"|" "$GRUB"
    echo "  1) GRUB_DISTRIBUTOR -> 'GNUcego $DIST_NAME ($DIST_SUITE)'"
fi

# patchear o 10_linux para reconhecer *GNU/Linux-libre* (evita " GNU/Linux" extra)
# na verdade aqui o DIST_NAME é Exu/Ogum, que NÃO casa com *GNU/Linux, então
# precisamos forçar o case a reconhecer o nosso distribuidor (evitar dup).
T10="$C/etc/grub.d/10_linux"
if [ -f "$T10" ]; then
    # adicionar GNUcego ao case (para o OS não ganhar " GNU/Linux" no final)
    if ! grep -q "GNUcego" "$T10"; then
        sed -i 's|    Ubuntu|Kubuntu|\*GNU/Linux)|    Ubuntu|Kubuntu|GNUcego\*|\*GNU/Linux)|' "$T10"
    fi
    echo "  1b) 10_linux patcheado (GNUcego*)"
fi

# ---------------------------------------------------------------
# 2) LightDM: posição centro
# ---------------------------------------------------------------
LG="$C/etc/lightdm/lightdm-gtk-greeter.conf"
if [ -f "$LG" ]; then
    sed -i "s|^position=.*|position=50% 50%|" "$LG"
    echo "  2) LightDM position=50% 50%"
fi

# ---------------------------------------------------------------
# 3) Plymouth: subtítulo sem "100% livre" + remover status de baixo
# ---------------------------------------------------------------
PLY="$C/usr/share/plymouth/themes/gnucego/gnucego.script"
if [ -f "$PLY" ]; then
    # subtítulo: remover " 100% livre"
    sed -i 's|Image.Text("GNU/Linux-libre 100% livre"|Image.Text("GNU/Linux-libre"|' "$PLY"
    # remover o texto de status de baixo (status_image/status_sprite)
    python3 - "$PLY" <<'PYEOF'
import sys, re
p = sys.argv[1]
txt = open(p).read()
# remover o bloco de status_image (o "esta iniciando"...)
txt = re.sub(r'\nstatus_image = Image\.Text\([^\n]*\);\n', '\n', txt)
txt = re.sub(r'\nstatus_sprite = Sprite\(status_image\);\n[^\n]*\n[^\n]*\n', '\n', txt)
# remover as linhas que referenciam status_ (se sobrar)
txt = "\n".join([l for l in txt.split("\n") if "status_image" not in l and "status_sprite" not in l])
open(p, 'w').write(txt)
print("  status de baixo removido")
PYEOF
    echo "  3) Plymouth: subtítulo corrigido + status removido"
fi

# ---------------------------------------------------------------
# 4) Calamares pós-instalação: remover autologin do lightdm (login funcional)
# ---------------------------------------------------------------
SP="$C/etc/calamares/modules/shellprocess.conf"
if [ -f "$SP" ]; then
    if ! grep -q "autologin-user" "$SP"; then
        # inserir a remoção do autologin ANTES do update-desktop-database
        python3 - "$SP" <<'PYEOF'
import sys
p = sys.argv[1]
txt = open(p).read()
add = '  - "sed -i \'/autologin-user=/d\' /etc/lightdm/lightdm.conf"\n  - "sed -i \'/autologin-session=/d\' /etc/lightdm/lightdm.conf"\n'
anchor = '  - "update-desktop-database'
if anchor in txt:
    txt = txt.replace(anchor, add + anchor)
else:
    # anexar antes do fechamento
    txt = txt.rstrip() + "\n" + add
open(p, 'w').write(txt)
print("  autologin removido no shellprocess")
PYEOF
        echo "  4) Calamares: autologin removido no pós-instalação"
    else
        echo "  4) Calamares: autologin já removido"
    fi
fi

echo "CONCLUÍDO: $C"
