#!/bin/bash
# gnucego-fix-final.sh — 3 correções finais:
#  1. Calamares: Name "Install System" -> "Instalar GNUcego"
#  2. /etc/hosts: adicionar "127.0.0.1 gnucego" (erro do sudo)
#  3. GNUcego-Dark: borda da janela PRETA (#000000)
# Copyright (C) 2026 GNUcego contributors — GPLv3+
set -e

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

echo "== fix final: $C =="

# ---------------------------------------------------------------
# 1) Calamares: nome em português
# ---------------------------------------------------------------
CAL="$C/usr/share/applications/calamares.desktop"
if [ -f "$CAL" ]; then
    sed -i 's/^Name=Install System$/Name=Instalar GNUcego/' "$CAL"
    sed -i 's/^GenericName=System Installer$/GenericName=Instalador do GNUcego/' "$CAL"
    # também no atalho do desktop
    for f in "$C/home/gnucego/Desktop/instalar-gnucego.desktop" "$C/etc/skel/Desktop/instalar-gnucego.desktop"; do
        [ -f "$f" ] && sed -i 's/^Name=Install System$/Name=Instalar GNUcego/; s/^GenericName=System Installer$/GenericName=Instalador do GNUcego/' "$f"
    done
    echo "  1) calamares.desktop -> 'Instalar GNUcego'"
fi

# ---------------------------------------------------------------
# 2) /etc/hosts: hostname gnucego
# ---------------------------------------------------------------
HOSTS="$C/etc/hosts"
if [ -f "$HOSTS" ]; then
    if ! grep -qE "127\.0\.0\.1[[:space:]]+gnucego" "$HOSTS"; then
        # inserir após a linha localhost
        sed -i '/^127\.0\.0\.1[[:space:]]*localhost$/a 127.0.0.1\tgnucego' "$HOSTS"
        echo "  2) /etc/hosts: adicionado '127.0.0.1 gnucego'"
    else
        echo "  2) /etc/hosts: já tem gnucego"
    fi
fi

# ---------------------------------------------------------------
# 3) GNUcego-Dark: borda da janela preta
# ---------------------------------------------------------------
META="$C/usr/share/themes/GNUcego-Dark/metacity-1/metacity-theme-3.xml"
if [ -f "$META" ]; then
    sed -i 's|<constant name="C_border_focused" value="gtk:bg\[NORMAL\]" />|<constant name="C_border_focused" value="#000000" />|' "$META"
    sed -i 's|<constant name="C_border_unfocused" value="gtk:bg\[NORMAL\]" />|<constant name="C_border_unfocused" value="#000000" />|' "$META"
    echo "  3) GNUcego-Dark: borda da janela -> preta"
fi

echo "CONCLUÍDO: $C"
