#!/usr/bin/env bash

# Display a start message
echo "Starting Debian system update..."

# Update the package list
while true; do
    read -p "Do you want to update the package list? (y/n) " confirm
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
if [[ "$confirm" =~ ^[yY]$ ]]; then
    sudo apt update
else
    echo "Package list update skipped."
fi

# Upgrade the packages
while true; do
    read -p "Do you want to upgrade the packages? (y/n) " confirm
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
if [[ "$confirm" =~ ^[yY]$ ]]; then
    sudo apt upgrade
else
    echo "Package upgrade skipped."
fi

# Perform a full distribution upgrade
while true; do
    read -p "Do you want to perform a full distribution upgrade? (y/n) " confirm
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
if [[ "$confirm" =~ ^[yY]$ ]]; then
    sudo apt full-upgrade
else
    echo "Full distribution upgrade skipped."
fi

# Remove obsolete packages
while true; do
    read -p "Do you want to remove obsolete packages? (y/n) " confirm
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
if [[ "$confirm" =~ ^[yY]$ ]]; then
    sudo apt autoremove
else
    echo "Obsolete packages removal skipped."
fi

# Clean up unnecessary files
while true; do
    read -p "Do you want to clean up unnecessary files? (y/n) " confirm
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
if [[ "$confirm" =~ ^[yY]$ ]]; then
    sudo apt clean
else
    echo "Cleanup of unnecessary files skipped."
fi

# Display a completion message
echo "Update process completed!"
while true; do
    read -p "Press Enter to close..."
    if [[ "$confirm" =~ ^[yY]$ ]]; then
        break
    elif [[ "$confirm" =~ ^[nN]$ ]]; then
        echo "Update canceled by user."
        read -p "Press Enter to exit..."
        exit 1
    else
        echo "Invalid response. Please enter y or n."
    fi
done
