diff --git a/distros.sh b/distros.sh index 5a27432..79f2ee2 100755 --- a/distros.sh +++ b/distros.sh @@ -1,178 +1,178 @@ -#!/usr/bin/env bash +{ + distro="" + ubuntu="false" + debian="false" + arch="false" + fedora="false" + alpine="false" + opensuse="false" -distro="" -ubuntu="false" -debian="false" -arch="false" -fedora="false" -alpine="false" -opensuse="false" + # ─< 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 -# ─< 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_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_info() { - printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1" -} + echo_warning() { + printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%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" + } -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 + } + silentexec() { + echo_note "Refreshing repositories.." + "$@" >/dev/null 2>&1 + } -# ─< Check if the given command exists silently >───────────────────────────────────────── -command_exists() { - command -v "$@" >/dev/null 2>&1 -} -silentexec() { - echo_note "Refreshing repositories.." - "$@" >/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 -E" - 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) - ubuntu="true" - distro="ubuntu" - _install() { $_sudo apt-get install --assume-yes "$@"; } - silentexec $_sudo apt-get update - ;; - debian) - debian="true" - distro="debian" - _install() { $_sudo apt-get install --assume-yes "$@"; } - silentexec $_sudo apt-get update - ;; - fedora) - fedora="true" - distro="fedora" - _install() { $_sudo dnf install -y "$@"; } - silentexec $_sudo dnf update - ;; - alpine) - alpine="true" - distro="alpine" - _install() { $_sudo apk add "$@"; } - silentexec $_sudo apk update - ;; - arch | manjaro | garuda | endeavour) - arch="true" - distro="arch" - _install() { - if command_exists paru; then - echo_info "Using paru" - paru -S --noconfirm "$@" - elif command_exists yay; then - echo_info "Using yay" - yay -S --noconfirm "$@" - else - echo_warning "Using pacman" - $_sudo pacman -S --noconfirm "$@" - fi - } - if command_exists paru; then - silentexec paru -Sy - elif command_exists yay; then - silentexec yay -Sy + # ─< 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 -E" else - silentexec $_sudo pacman -Sy + echo_error "No sudo found and you're not root! Can't install packages." + return 1 fi - ;; - opensuse*) - opensuse="true" - distro="opensuse" - _install() { $_sudo zypper in "$@"; } - silentexec $_sudo zypper ref - ;; - *) - if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then - debian="true" - distro="debian" - _install() { $_sudo apt-get install --assume-yes "$@"; } - silentexec $_sudo apt-get update - elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then + 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) ubuntu="true" distro="ubuntu" _install() { $_sudo apt-get install --assume-yes "$@"; } silentexec $_sudo apt-get update - elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then - arch="true" - distro="arch" - _install() { $_sudo pacman -S --noconfirm "$@"; } - silentexec $_sudo pacman -Sy - elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then + ;; + debian) + debian="true" + distro="debian" + _install() { $_sudo apt-get install --assume-yes "$@"; } + silentexec $_sudo apt-get update + ;; + fedora) fedora="true" distro="fedora" _install() { $_sudo dnf install -y "$@"; } silentexec $_sudo dnf update - elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then + ;; + alpine) + alpine="true" + distro="alpine" + _install() { $_sudo apk add "$@"; } + silentexec $_sudo apk update + ;; + arch | manjaro | garuda | endeavour) + arch="true" + distro="arch" + _install() { + if command_exists paru; then + echo_info "Using paru" + paru -S --noconfirm "$@" + elif command_exists yay; then + echo_info "Using yay" + yay -S --noconfirm "$@" + else + echo_warning "Using pacman" + $_sudo pacman -S --noconfirm "$@" + fi + } + if command_exists paru; then + silentexec paru -Sy + elif command_exists yay; then + silentexec yay -Sy + else + silentexec $_sudo pacman -Sy + fi + ;; + opensuse*) opensuse="true" distro="opensuse" _install() { $_sudo zypper in "$@"; } silentexec $_sudo zypper ref - else - echo_error "Unsupported distribution: $ID" - exit 1 + ;; + *) + if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then + debian="true" + distro="debian" + _install() { $_sudo apt-get install --assume-yes "$@"; } + silentexec $_sudo apt-get update + elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then + ubuntu="true" + distro="ubuntu" + _install() { $_sudo apt-get install --assume-yes "$@"; } + silentexec $_sudo apt-get update + elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then + arch="true" + distro="arch" + _install() { $_sudo pacman -S --noconfirm "$@"; } + silentexec $_sudo pacman -Sy + elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then + fedora="true" + distro="fedora" + _install() { $_sudo dnf install -y "$@"; } + silentexec $_sudo dnf update + elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then + opensuse="true" + distro="opensuse" + _install() { $_sudo zypper in "$@"; } + silentexec $_sudo zypper ref + 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 + } + + if check_root; then + get_packager && + if $arch; then + echo "Arch it is.." + elif $debian; then + echo "Debian it is.." + elif $ubuntu; then + echo "Ubuntu it is.." + elif $fedora; then + echo "Fedora it is.." + elif $opensuse; then + echo "OpenSuse it is.." + elif $alpine; then + echo "Alpine it is.." fi - ;; - esac - else - echo_error "Unable to detect distribution. /etc/os-release not found." - exit 1 + fi } - -if check_root; then - get_packager && - if $arch; then - echo "Arch it is.." - elif $debian; then - echo "Debian it is.." - elif $ubuntu; then - echo "Ubuntu it is.." - elif $fedora; then - echo "Fedora it is.." - elif $opensuse; then - echo "OpenSuse it is.." - elif $alpine; then - echo "Alpine it is.." - fi - -fi