#!/bin/bash

# Function to update packages using apt
update_apt() {
    sudo apt update && sudo apt upgrade -y
}

# Function to update packages using dnf
update_dnf() {
    sudo dnf check-update && sudo dnf upgrade -y
}

# Function to update packages using yum
update_yum() {
    sudo yum check-update && sudo yum upgrade -y
}

# Function to update packages using pacman
update_pacman() {
    sudo pacman -Syu --noconfirm
}

# Function to update packages using zypper
update_zypper() {
    sudo zypper refresh && sudo zypper update -y
}

# Function to update packages using apk (Alpine Linux)
update_apk() {
    sudo apk update && sudo apk upgrade
}

# Check the distribution and update packages accordingly
if [ -x "$(command -v apt)" ]; then
    echo "Detected Debian-based distribution"
    update_apt
elif [ -x "$(command -v dnf)" ]; then
    echo "Detected Fedora-based distribution"
    update_dnf
elif [ -x "$(command -v yum)" ]; then
    echo "Detected Red Hat-based distribution"
    update_yum
elif [ -x "$(command -v pacman)" ]; then
    echo "Detected Arch-based distribution"
    update_pacman
elif [ -x "$(command -v zypper)" ]; then
    echo "Detected openSUSE-based distribution"
    update_zypper
elif [ -x "$(command -v apk)" ]; then
    echo "Detected Alpine Linux distribution"
    update_apk
else
    echo "Unsupported distribution or package manager not found"
fi
