#!/bin/bash

# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
YELLOW='\033[1;33m'
NC='\033[0m' # No color

clear

# Ensure required packages are installed
echo -e "${ORANGE}Checking for required utilities...${NC}"
sleep 1
if ! command -v lsb_release &>/dev/null; then
  apt update -qq && apt install -y lsb-release
fi
echo

# Step 2: Check for updates
echo -e "${ORANGE}Checking system for updates...${NC}"
sleep 2
if apt update -qq; then
  UPGRADABLE=$(apt list --upgradable 2>/dev/null | wc -l)
  if [ "$UPGRADABLE" -gt 1 ]; then
    echo -e "${GREEN}Found Updates${NC}"
  else
    echo -e "${RED}No Updates found${NC}"
  fi
else
  echo -e "${RED}Failed to update package list${NC}"
fi
echo

# Step 4: Upgrade system
echo -e "${ORANGE}Starting Upgrading system...${NC}"
sleep 2
if DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" \
  -o Dpkg::Options::="--force-confold" upgrade -yq; then
  echo -e "${GREEN}System Upgrade COMPLETE${NC}"
else
  echo -e "${RED}System Upgrade FAILED${NC}"
fi
sleep 2
echo

# Step 5: Detect OS Info
echo -e "${YELLOW}Your Operating System:${NC}"
sleep 1
DISTRO=$(lsb_release -si)
VERSION=$(lsb_release -sr)
CODENAME=$(lsb_release -sc)
echo "Distro: $DISTRO"
echo "Version: $VERSION"
echo "Codename: $CODENAME"
echo
sleep 2

# Step 6: Check for newer distro release dynamically
echo -e "${ORANGE}Checking for newer distro release...${NC}"
sleep 2

TARGET=""
if [ "$DISTRO" = "Debian" ]; then
  if [ "$CODENAME" != "trixie" ]; then
    echo -e "${GREEN}Newer Distro Available: Debian 13 (trixie)${NC}"
    TARGET="trixie"
  else
    echo -e "${RED}NO new Distro found${NC}"
  fi

elif [ "$DISTRO" = "Ubuntu" ]; then
  if dpkg --compare-versions "$VERSION" lt "24.04"; then
    echo -e "${GREEN}Newer Distro Available: Ubuntu 24.04 LTS (noble)${NC}"
    TARGET="noble"
  else
    echo -e "${RED}NO new Distro found${NC}"
  fi

else
  echo -e "${RED}Unsupported distribution for automatic upgrade${NC}"
fi
echo
sleep 2

# Step 7: If newer distro found
if [ -n "$TARGET" ]; then
  echo -e "${ORANGE}Checking for new distro repos in sources.list...${NC}"
  sleep 2

  if grep -R "$TARGET" /etc/apt/sources.list /etc/apt/sources.list.d/ &>/dev/null; then
    echo -e "${GREEN}New Distro Repositories already in sources.list${NC}"
  else
    echo -e "${RED}New Distro Repositories are not in sources.list${NC}"
  fi
  echo
  sleep 2

  # Add new repo
  echo -e "${ORANGE}Adding repositories to sources.list...${NC}"
  sleep 2
  if sed -i "s/$CODENAME/$TARGET/g" /etc/apt/sources.list; then
    echo -e "${GREEN}Repositories successfully added to sources.list${NC}"
  else
    echo -e "${RED}Repositories FAILED to add to sources.list${NC}"
  fi
  echo
  sleep 2

  # Update with new repos
  echo -e "${ORANGE}Checking system for updates...${NC}"
  sleep 2
  if apt update; then
    echo -e "${GREEN}System updates COMPLETE${NC}"
  else
    echo -e "${RED}System updates FAILED${NC}"
  fi
  echo
  sleep 2

  # Full upgrade
  echo -e "${ORANGE}Starting full system upgrade...${NC}"
  sleep 2

  # Preseed the debconf prompt for automatic service restart
  echo '* libraries/restart-without-asking boolean true' | debconf-set-selections

  # Run upgrade in fully noninteractive mode
  if DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" \
    -o Dpkg::Options::="--force-confold" full-upgrade -yq; then
    echo -e "${GREEN}System full-upgrade COMPLETE${NC}"
  else
    echo -e "${RED}System full-upgrade FAILED${NC}"
  fi
  echo
  sleep 2
fi

# Step 8: Autoremove
echo -e "${ORANGE}Removing system old packages...${NC}"
sleep 2
if apt autoremove -y; then
  echo -e "${GREEN}Removing system old packages COMPLETE${NC}"
else
  echo -e "${RED}Removing system old packages FAILED${NC}"
fi
echo
sleep 2

# Step 9: Clean
echo -e "${ORANGE}Cleaning system old packages...${NC}"
sleep 2
if apt autoclean && apt clean; then
  echo -e "${GREEN}Cleaning system old packages COMPLETE${NC}"
else
  echo -e "${RED}Cleaning system old packages FAILED${NC}"
fi
echo
sleep 2

# Step 10: Final Distro Info
echo "------------------------------------------------------------"
echo -e "${YELLOW}Final Distro Information:${NC}"
lsb_release -a 2>/dev/null
echo "------------------------------------------------------------"
