sh/setup/updates.sh
2025-02-25 10:58:12 +01:00

157 lines
4.2 KiB
Bash
Executable file

#!/bin/sh
# ─< ANSI color codes >───────────────────────────────────────────────────────────────────
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
}
echo_info() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
}
echo_warning() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
}
echo_note() {
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
}
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
check_root() {
if [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
echo_info "User is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_info "Root access confirmed."
_sudo=""
fi
}
# ─< Distribution detection and installation >────────────────────────────────────────
get_packager() {
if [ -e /etc/os-release ]; then
echo_info "Detecting distribution..."
. /etc/os-release
# ─< Convert $ID and $ID_LIKE to lowercase >──────────────────────────────────────────────
ID=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]')
ID_LIKE=$(printf "%s" "$ID_LIKE" | tr '[:upper:]' '[:lower:]')
case "$ID" in
ubuntu | pop) packager="apt" ;;
debian) packager="apt" ;;
fedora) packager="dnf" ;;
alpine) packager="apk" ;;
arch | manjaro | garuda | endeavour) packager="pacman" ;;
opensuse*) packager="zypper" ;;
*)
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
packager="apt"
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
packager="apt"
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
packager="pacman"
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
packager="dnf"
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
packager="zypper"
else
echo_error "Unsupported distribution: $ID"
exit 1
fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi
}
_update() {
case "$packager" in
apt)
if command_exists nala; then
echo_note "Using nala to update packages.. Please be patient.."
sleep 1
$_sudo nala update
$_sudo nala upgrade -y
$_sudo nala autoremove -y
$_sudo apt-get autoclean -y
else
echo_note "Using nala to update packages.. Please be patient.."
sleep 1
$_sudo apt-get update
$_sudo apt-get upgrade -y
$_sudo apt-get autoremove -y
$_sudo apt-get autoclean -y
fi
;;
dnf)
echo_note "Using dnf to update packages.. Please be patient.."
sleep 1
$_sudo dnf update
;;
pacman)
echo_note "Using pacman to update packages.. Please be patient.."
sleep 1
$_sudo pacman -Syu --noconfirm
;;
apk)
echo_note "Using apk to update packages.. Please be patient.."
sleep 1
$_sudo apk update
$_sudo apk upgrade
;;
zypper)
echo_note "Using zypper to update packages.. Please be patient.."
sleep 1
$_sudo zypper dup
;;
*)
if [ -z "$packager" ]; then
echo_error "The packager variable is not declared.."
else
echo_error "$packager is not a known packager.."
fi
;;
esac
}
_flatpak() {
if command_exists flatpak; then
echo_info "Trying to update flatpaks.."
sleep 1
flatpak update
else
echo_note "No flatpaks found"
fi
}
main() {
check_root
sleep 1
if get_packager; then
_update
_flatpak
fi
}
main