#!/bin/bash
# gnucego-i3wm-provision.sh — limpa e configura o chroot i3wm GNUcego.
# Copyright (C) 2026 GNUcego contributors — GPLv3+
#
# Faz:
#  1. Remove apps/sessão GNOME (sobras do clone do chroot GNOME).
#  2. Config i3 correta (set $mod Mod4 + wallpaper morcego + atalhos GNU).
#  3. Wallpaper do morcego correto.
#  4. .xinitrc (startx).
#
# Uso: gnucego-i3wm-provision.sh <chroot>
set -e

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

echo "== GNUcego i3wm provision: $C =="

# ---------------------------------------------------------------
# 1) remover apps/sessão GNOME
# ---------------------------------------------------------------
GNOME_PKGS="gnome-shell gnome-shell-common gnome-session gnome-session-bin \
gnome-session-common gnome-session-xsession nautilus nautilus-data \
nautilus-extension-gnome-terminal gnome-terminal gnome-terminal-data \
gnome-software gnome-software-common gnome-software-plugin-deb \
gnome-control-center gnome-control-center-data gnome-online-accounts \
gnome-remote-desktop gnome-user-docs gnome-user-share gnome-browser-connector \
gnome-sushi gdm3 xdg-desktop-portal-gnome"

KEEP="network-manager network-manager-gnome wpasupplicant system-config-printer \
system-config-printer-common system-config-printer-udev xdg-user-dirs-gtk \
power-profiles-daemon bluez evolution-data-server mobile-broadband-provider-info \
wireless-regdb ibus ibus-data ibus-gtk ibus-gtk3 ibus-gtk4 im-config appstream \
apt-config-icons dconf-cli python3-certifi python3-chardet python3-charset-normalizer \
python3-idna python3-requests python3-urllib3 packagekit packagekit-tools \
network-manager-l10n heif-gdk-pixbuf heif-thumbnailer gstreamer1.0-plugins-ugly \
gstreamer1.0-pipewire"

echo "  [1/4] removendo apps GNOME..."
systemd-nspawn -D "$C" bash -c "
  DEBIAN_FRONTEND=noninteractive apt-get remove -y $GNOME_PKGS 2>&1 | tail -3
  apt-mark manual $KEEP 2>/dev/null
  DEBIAN_FRONTEND=noninteractive apt-get -y autoremove 2>&1 | tail -3
  for p in $GNOME_PKGS; do dpkg --purge \$p 2>/dev/null; done
  echo '  GNOME removido'
"

# ---------------------------------------------------------------
# 2) config i3 (set $mod + wallpaper morcego)
# ---------------------------------------------------------------
echo "  [2/4] config i3..."
mkdir -p "$C/home/gnucego/.config/i3" "$C/etc/skel/.config/i3"
cat > "$C/home/gnucego/.config/i3/config" <<'I3EOF'
# i3 config — GNUcego GNU/Linux-libre
set $mod Mod4
font pango:DejaVu Sans Mono 10

exec_always --no-startup-id feh --bg-scale /usr/share/backgrounds/gnucego-wallpaper.png
exec --no-startup-id dex --autostart --environment i3
exec --no-startup-id nm-applet

bar {
    status_command i3status
    position bottom
}

bindsym $mod+Return exec terminator
bindsym $mod+d exec --no-startup-id dmenu_run

bindsym $mod+b exec icecat
bindsym $mod+e exec emacs
bindsym $mod+g exec gimp
bindsym $mod+o exec octave
bindsym $mod+n exec gnucash

bindsym $mod+Shift+q kill
bindsym $mod+Shift+c reload
bindsym $mod+Shift+r restart
bindsym $mod+Shift+e exit
bindsym $mod+Escape exec i3lock -c 000000

bindsym $mod+j focus left
bindsym $mod+k focus down
bindsym $mod+l focus up
bindsym $mod+semicolon focus right
bindsym $mod+Left focus left
bindsym $mod+Down focus down
bindsym $mod+Up focus up
bindsym $mod+Right focus right

bindsym $mod+Shift+j move left
bindsym $mod+Shift+k move down
bindsym $mod+Shift+l move up
bindsym $mod+Shift+semicolon move right

bindsym $mod+h split h
bindsym $mod+v split v
bindsym $mod+f fullscreen toggle
bindsym $mod+s layout stacking
bindsym $mod+w layout tabbed
bindsym $mod+t layout toggle split
bindsym $mod+space focus mode_toggle
bindsym $mod+Shift+space floating toggle

mode "resize" {
    bindsym j resize shrink width 10 px or 10 ppt
    bindsym k resize grow height 10 px or 10 ppt
    bindsym l resize shrink height 10 px or 10 ppt
    bindsym semicolon resize grow width 10 px or 10 ppt
    bindsym Left  resize shrink width 10 px or 10 ppt
    bindsym Down  resize grow height 10 px or 10 ppt
    bindsym Up    resize shrink height 10 px or 10 ppt
    bindsym Right resize grow width 10 px or 10 ppt
    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+r mode "resize"

bindsym $mod+1 workspace number 1
bindsym $mod+2 workspace number 2
bindsym $mod+3 workspace number 3
bindsym $mod+4 workspace number 4
bindsym $mod+5 workspace number 5
bindsym $mod+6 workspace number 6
bindsym $mod+7 workspace number 7
bindsym $mod+8 workspace number 8
bindsym $mod+9 workspace number 9

bindsym $mod+Shift+1 move container to workspace number 1
bindsym $mod+Shift+2 move container to workspace number 2
bindsym $mod+Shift+3 move container to workspace number 3
bindsym $mod+Shift+4 move container to workspace number 4
bindsym $mod+Shift+5 move container to workspace number 5
bindsym $mod+Shift+6 move container to workspace number 6
bindsym $mod+Shift+7 move container to workspace number 7
bindsym $mod+Shift+8 move container to workspace number 8
bindsym $mod+Shift+9 move container to workspace number 9
I3EOF
cp "$C/home/gnucego/.config/i3/config" "$C/etc/skel/.config/i3/config"
chown -R 1000:1000 "$C/home/gnucego/.config/i3" 2>/dev/null || true

# ---------------------------------------------------------------
# 3) wallpaper morcego correto
# ---------------------------------------------------------------
echo "  [3/4] wallpaper..."
if [ -f "$C/usr/share/backgrounds/gnucego-wallpaper.png" ]; then
    cp "$C/usr/share/backgrounds/gnucego-wallpaper.png" "$C/usr/share/backgrounds/gnucego/wallpaper.jpg" 2>/dev/null || true
fi

# ---------------------------------------------------------------
# 4) .xinitrc
# ---------------------------------------------------------------
echo "  [4/4] .xinitrc..."
cat > "$C/home/gnucego/.xinitrc" <<'XEOF'
#!/bin/sh
exec i3
XEOF
cp "$C/home/gnucego/.xinitrc" "$C/etc/skel/.xinitrc"
chmod +x "$C/home/gnucego/.xinitrc" "$C/etc/skel/.xinitrc"
chown 1000:1000 "$C/home/gnucego/.xinitrc" 2>/dev/null || true

echo "CONCLUÍDO: $C"
