simplified it A LOT!

This commit is contained in:
pika 2025-05-07 22:11:40 +02:00
parent b3f1f197bf
commit 6c8b3b8b72

View file

@ -1,4 +1,29 @@
#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the _install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
if command_exists curl; then
eval "$(curl -fsSL https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh)"
else
echo "curl is required, but missing.."
exit 1
fi
generic() {
set -euo pipefail
@ -68,73 +93,7 @@ generic() {
fi
}
# ─< 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) generic ;;
debian) generic ;;
fedora) generic ;;
alpine) generic ;;
arch | manjaro | garuda | endeavour)
if command_exists paru; then
paru -S --noconfirm zellij
elif command_exists yay; then
yay -S --noconfirm zellij
fi
;;
opensuse*) generic ;;
*)
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
generic
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
generic
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
$_sudo pacman -S zellij
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
generic
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
generic
else
echo_error "Unsupported distribution: $ID"
exit 1
fi
;;
case "$distro" in
arch | opensuse) _install zellij ;;
*) generic ;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi
}
if check_root; then
get_packager
fi