#!/bin/bash

# Exit immediately if a command exits with a non-zero status
set -e

echo "Starting Sethian 0.1 (Slim Edition + Printing/Scanning) Customization..."

# --- 1. Repositories & Updates ---
echo "Configuring repositories..."
sed -i 's/main$/main contrib non-free non-free-firmware/' /etc/apt/sources.list
sed -i 's/main non-free-firmware$/main contrib non-free non-free-firmware/' /etc/apt/sources.list

# Disable installing "Recommends" to keep future installs light
echo 'APT::Install-Recommends "0";' > /etc/apt/apt.conf.d/01norecommends
echo 'APT::Install-Suggests "0";' >> /etc/apt/apt.conf.d/01norecommends

apt-get update

# --- 2. Remove Bloatware ---
echo "Removing Heavy/Unused Packages..."

# Remove Evolution (Email)
apt-get purge -y evolution evolution-common evolution-plugins evolution-ews

# Remove LibreOffice (Huge space saver)
apt-get purge -y libreoffice*

# Remove GNOME Games
apt-get purge -y gnome-games aisleriot gnome-sudoku gnome-mines gnome-mahjongg gnome-nibbles gnome-robots quadrapassel lightsoff swell-foop tali five-or-more hitori iagno

# Remove other standard apps you might not need (Maps, Weather, Contacts, etc.)
# NOTE: We are KEEPING simple-scan for scanner support.
apt-get purge -y gnome-maps gnome-weather gnome-contacts gnome-clocks cheese rhythmbox totem

# Aggressive cleanup
apt-get autoremove -y

# --- 3. Install Core Applications & Utilities ---
echo "Installing Core Apps (VLC, Chrome, Extensions, Printer GUI)..."
# We include system-config-printer to ensure the printer setup GUI is available.
apt-get install -y vlc curl wget gnupg git system-config-printer simple-scan

# Install Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/google-chrome.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list
apt-get update
apt-get install -y google-chrome-stable
update-alternatives --set x-www-browser /usr/bin/google-chrome-stable
update-alternatives --set gnome-www-browser /usr/bin/google-chrome-stable

# Install Gemini CLI
echo "Installing Node.js and Gemini CLI..."
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
npm install -g @google/gemini-cli

# Install GNOME Extensions
apt-get install -y gnome-shell-extension-dash-to-panel gnome-shell-extensions

# --- 4. Drivers & Firmware ---
echo "Installing firmware..."
apt-get install -y firmware-linux firmware-linux-nonfree firmware-misc-nonfree firmware-iwlwifi firmware-realtek

# --- 5. GNOME Configuration ---
echo "Applying GNOME Settings Overrides..."

cat <<EOF > /usr/share/glib-2.0/schemas/90_sethian_custom.gschema.override
[org.gnome.desktop.interface]
color-scheme='prefer-dark'
gtk-theme='Adwaita-dark'

[org.gnome.shell]
enabled-extensions=['dash-to-panel@jderose9.github.io', 'apps-menu@gnome-shell-extensions.gcampax.github.com']
favorite-apps=['google-chrome.desktop', 'org.gnome.Terminal.desktop', 'org.gnome.Nautilus.desktop', 'vlc.desktop']

[org.gnome.shell.extensions.dash-to-panel]
panel-position='TOP'
taskbar-locked=true
dot-position='TOP'
appicon-margin=4
appicon-padding=4
group-apps=false

[org.gnome.desktop.wm.preferences]
button-layout='appmenu:minimize,maximize,close'
EOF

glib-compile-schemas /usr/share/glib-2.0/schemas/

# --- Final Cleanup ---
echo "Deep Cleaning..."
apt-get clean
rm -rf /var/lib/apt/lists/*
rm -rf /tmp/*
rm -rf /var/cache/apt/archives/*

echo "Sethian 0.1 Customization Complete!"