This commit is contained in:
pika 2025-05-07 10:35:35 +02:00
parent c811cea4b3
commit 55d75c6e4b

View file

@ -1,178 +1,178 @@
#!/usr/bin/env bash {
distro=""
ubuntu="false"
debian="false"
arch="false"
fedora="false"
alpine="false"
opensuse="false"
distro="" # ─< ANSI color codes >───────────────────────────────────────────────────────────────────
ubuntu="false" RED='\033[0;31m'
debian="false" CYAN='\033[0;36m'
arch="false" YELLOW='\033[0;33m'
fedora="false" LIGHT_GREEN='\033[0;92m'
alpine="false" BOLD='\033[1m'
opensuse="false" NC='\033[0m' # No Color
# ─< ANSI color codes >─────────────────────────────────────────────────────────────────── echo_error() {
RED='\033[0;31m' printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
CYAN='\033[0;36m' }
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() { echo_info() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2 printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
} }
echo_info() { echo_warning() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1" printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
} }
echo_warning() { echo_note() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1" printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
} }
echo_note() { # ─< Check if the given command exists silently >─────────────────────────────────────────
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1" command_exists() {
} command -v "$@" >/dev/null 2>&1
}
silentexec() {
echo_note "Refreshing repositories.."
"$@" >/dev/null 2>&1
}
# ─< Check if the given command exists silently >───────────────────────────────────────── # ─< Check if the user is root and set sudo variable if necessary >───────────────────────
command_exists() { check_root() {
command -v "$@" >/dev/null 2>&1 if [ "$(id -u)" -ne 0 ]; then
} if command_exists sudo; then
silentexec() { echo_info "User is not root. Using sudo for privileged operations."
echo_note "Refreshing repositories.." _sudo="sudo -E"
"$@" >/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
else else
silentexec $_sudo pacman -Sy echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi fi
;; else
opensuse*) echo_info "Root access confirmed."
opensuse="true" _sudo=""
distro="opensuse" fi
_install() { $_sudo zypper in "$@"; } }
silentexec $_sudo zypper ref
;; # ─< Distribution detection and installation >────────────────────────────────────────
*) get_packager() {
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then if [ -e /etc/os-release ]; then
debian="true" echo_info "Detecting distribution..."
distro="debian" . /etc/os-release
_install() { $_sudo apt-get install --assume-yes "$@"; }
silentexec $_sudo apt-get update # ─< Convert $ID and $ID_LIKE to lowercase >──────────────────────────────────────────────
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then ID=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]')
ID_LIKE=$(printf "%s" "$ID_LIKE" | tr '[:upper:]' '[:lower:]')
case "$ID" in
ubuntu | pop)
ubuntu="true" ubuntu="true"
distro="ubuntu" distro="ubuntu"
_install() { $_sudo apt-get install --assume-yes "$@"; } _install() { $_sudo apt-get install --assume-yes "$@"; }
silentexec $_sudo apt-get update silentexec $_sudo apt-get update
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then ;;
arch="true" debian)
distro="arch" debian="true"
_install() { $_sudo pacman -S --noconfirm "$@"; } distro="debian"
silentexec $_sudo pacman -Sy _install() { $_sudo apt-get install --assume-yes "$@"; }
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then silentexec $_sudo apt-get update
;;
fedora)
fedora="true" fedora="true"
distro="fedora" distro="fedora"
_install() { $_sudo dnf install -y "$@"; } _install() { $_sudo dnf install -y "$@"; }
silentexec $_sudo dnf update 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" opensuse="true"
distro="opensuse" distro="opensuse"
_install() { $_sudo zypper in "$@"; } _install() { $_sudo zypper in "$@"; }
silentexec $_sudo zypper ref 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 fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi 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