#!/bin/bash
# gnucego-adapt-all.sh — adapta UM chroot de DE às correções GNUcego (idempotente).
# Reaplica TUDO que foi feito no MATE para qualquer DE (GNOME/i3wm/cinnamon).
# 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 "== adaptando $(basename $C) ($DIST_NAME $DIST_SUITE) =="
GN=/media/wellton/backup/debian/gnucego

# 1) remover apps GNOME (showtime/loupe/ptyxis/papers/rygel/etc.) + instalar VLC
echo "  [1/6] remover apps GNOME + instalar VLC..."
systemd-nspawn -D "$C" bash -c "
  apt-get update -qq 2>&1 | tail -1
  DEBIAN_FRONTEND=noninteractive apt-get remove -y \
    showtime loupe ptyxis orca papers papers-common rygel-localsearch rygel-playbin \
    gnome-maps gnome-weather gnome-connections gnome-calendar gnome-clocks \
    gnome-contacts gnome-characters gnome-font-viewer gnome-logs gnome-text-editor \
    gnome-disk-utility baobab simple-scan gnome-snapshot gnome-tour gnome-system-monitor \
    resources totem eog evince cheese gnome-terminal gedit file-roller \
    libnautilus-extension4 librygel-* libtotem-plparser* 2>&1 | tail -2
  DEBIAN_FRONTEND=noninteractive apt-get -y autoremove 2>&1 | tail -1
  DEBIAN_FRONTEND=noninteractive apt-get install -y vlc 2>&1 | tail -2
  # network-manager applet (nome difere exu/ogum)
  DEBIAN_FRONTEND=noninteractive apt-get install -y network-manager-gnome 2>/dev/null || DEBIAN_FRONTEND=noninteractive apt-get install -y network-manager-applet 2>/dev/null
" 2>/dev/null

# 2) instalar mate-themes (BlackMATE/TraditionalOk, base do tema GNUcego) + criar temas
echo "  [2/6] mate-themes + temas GNUcego..."
systemd-nspawn -D "$C" bash -c "
  apt-get update -qq 2>&1 | tail -1
  DEBIAN_FRONTEND=noninteractive apt-get install -y mate-themes 2>&1 | tail -2
" 2>/dev/null
if [ -f "$GN/gnucego-create-theme.sh" ]; then
    bash "$GN/gnucego-create-theme.sh" "$C" 2>/dev/null
fi
if [ -f "$GN/gnucego-create-white-theme.sh" ]; then
    bash "$GN/gnucego-create-white-theme.sh" "$C" 2>/dev/null
fi

# 3) GRUB + LightDM + Plymouth + Calamares-autologin (fix final2)
echo "  [3/6] GRUB/LightDM/Plymouth/Calamares..."
if [ -f "$GN/gnucego-fix-final2.sh" ]; then
    bash "$GN/gnucego-fix-final2.sh" "$C" "$DIST_NAME" "$DIST_SUITE" 2>/dev/null
fi

# 4) borda preta do GNUcego-Dark (fix-final.sh parte 3)
echo "  [4/6] borda preta + nome Calamares + hosts..."
if [ -f "$GN/gnucego-fix-final.sh" ]; then
    bash "$GN/gnucego-fix-final.sh" "$C" 2>/dev/null
fi

# 5) "Linux" -> "GNU/Linux" nos locales (fix-3pontos parte 3)
echo "  [5/6] GNU/Linux nos locales..."
if [ -f "$GN/gnucego-fix-3pontos.sh" ]; then
    bash "$GN/gnucego-fix-3pontos.sh" "$C" 2>/dev/null
fi

# 6) regenerar grub.cfg
echo "  [6/6] regenerar grub.cfg..."
mount --bind /proc "$C/proc" 2>/dev/null
mount --bind /sys "$C/sys" 2>/dev/null
mount --bind /dev "$C/dev" 2>/dev/null
mount --bind /run "$C/run" 2>/dev/null
chroot "$C" grub-mkconfig -o /boot/grub/grub.cfg >/dev/null 2>&1
umount "$C/run" "$C/dev" "$C/sys" "$C/proc" 2>/dev/null

echo "CONCLUÍDO: $(basename $C)"
