#!/bin/bash

# Ensure root
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root."
  exit 1
fi

##############################################
# Mandatory Desktop Environment + Networking #
##############################################

echo "Updating package lists..."
DEBIAN_FRONTEND=noninteractive apt update

echo "Installing XFCE desktop, PipeWire audio, and networking stack..."
DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends \
  xfce4 xfce4-goodies lightdm lightdm-gtk-greeter xserver-xorg \
  pipewire pipewire-audio pipewire-pulse wireplumber \
  dbus-x11 dbus-user-session \
  net-tools network-manager network-manager-gnome netplan.io \
  xfce4-power-manager gnome-disk-utility \
  openvpn network-manager-openvpn network-manager-openvpn-gnome \
  vlc nfs-common samba

# Check if key packages installed
if dpkg -l | grep -q "^ii  xfce4 "; then
  echo "XFCE core installed successfully."
else
  echo "WARNING: XFCE core not detected!"
fi
if dpkg -l | grep -q "^ii  lightdm "; then
  echo "LightDM installed successfully."
else
  echo "WARNING: LightDM not detected!"
fi
if dpkg -l | grep -q "^ii  network-manager-gnome "; then
  echo "NetworkManager applet (nm-applet) installed successfully."
else
  echo "WARNING: NetworkManager applet not detected!"
fi

# Enable services
systemctl enable NetworkManager
systemctl enable lightdm

# Set system to boot into graphical mode
systemctl set-default graphical.target
echo "System set to boot into GUI (XFCE + LightDM)."

# Configure LightDM for XFCE (critical for Ubuntu Server installs)
mkdir -p /etc/lightdm/lightdm.conf.d
cat <<EOF > /etc/lightdm/lightdm.conf.d/50-xfce-greeter.conf
[Seat:*]
greeter-session=lightdm-gtk-greeter
user-session=xfce
allow-guest=false
EOF

# LightDM X11 config (keeps previous)
cat <<EOF > /etc/lightdm/lightdm.conf.d/10-x11.conf
[Seat:*]
display-setup-script=/bin/true
EOF

# Auto-login prompt
read -p "Enable auto-login for LightDM? [y/N]: " AUTOLOGIN_CHOICE
if [[ "$AUTOLOGIN_CHOICE" =~ ^[Yy]$ ]]; then
  read -p "Enter existing username for auto-login: " AUTOLOGIN_USER
  if id "$AUTOLOGIN_USER" >/dev/null 2>&1; then
    cat <<EOF > /etc/lightdm/lightdm.conf.d/20-autologin.conf
[Seat:*]
autologin-user=$AUTOLOGIN_USER
autologin-timeout=0
EOF
    echo "Auto-login enabled for $AUTOLOGIN_USER."
  else
    echo "ERROR: User $AUTOLOGIN_USER does not exist. Skipping auto-login."
  fi
fi


# NetworkManager via Netplan
cat <<EOF > /etc/netplan/01-network-manager.yaml
network:
  version: 2
  renderer: NetworkManager
EOF
netplan generate
netplan apply
echo "NetworkManager enabled (reboot for full effect)."

# Disable IPv6 prompt
read -p "Disable IPv6 system-wide? [y/N]: " IPV6_CHOICE
if [[ "$IPV6_CHOICE" =~ ^[Yy]$ ]]; then
  cat <<EOF > /etc/sysctl.d/99-disable-ipv6.conf
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
EOF
  sysctl -p /etc/sysctl.d/99-disable-ipv6.conf
  echo "IPv6 disabled."
fi

# Disable boot delay service
systemctl disable systemd-networkd-wait-online.service
systemctl mask systemd-networkd-wait-online.service

# Clean Ubuntu branding
apt purge -y ubuntu-wallpapers ubuntu-wallpapers-* lightdm-gtk-greeter-settings || true
rm -rf /usr/share/backgrounds/ubuntu*

echo "Desktop environment and networking setup complete."

