imports/distros.sh
2025-05-18 18:31:05 +02:00

328 lines
9.1 KiB
Bash
Executable file

# INFO:
# ╭──────────────────────────────╮
# │ User variables and functions │
# ╰──────────────────────────────╯
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
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
}
silentexec() {
"$@" >/dev/null 2>&1
}
run() {
if $silent; then
silentexec "$@"
else
"$@"
fi
}
# if given an array, it checks if the command is available, and if not - installs all packages in that array one by one
checkAndInstall() {
# echo "DEBUG:: GOT ARRAY :: ${@}"
# ─< if it's not a list, then just check and install the package.. >──────────────────────
if [ -z "$2" ]; then
if ! command_exists "$1"; then
echo_note "$1 is not installed. Installing it now.."
run _install "$1"
else
echo_note "skipping $1 - as it's already installed.."
fi
else
# ─< else go though the list of items and do the same >───────────────────────────────────
printf "${YELLOW} Installing package group - ${RED} $@ ${NC}"
for deps in "${@}"; do
echo_info "Installing $deps"
if ! command_exists $deps; then
echo_note "$deps is not installed. Installing it now.."
run _install "$deps"
else
echo_note "skipping $deps - as it's already installed.."
fi
done
fi
}
# ─< 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
}
_setup() {
case "$1" in
debian | ubuntu)
_install() { $_sudo apt-get install --assume-yes "$@"; }
_remove() {
$_sudo apt-get remove --assume-yes "$@"
$_sudo apt-get autoremove --assume-yes
}
;;
fedora)
_install() { $_sudo dnf -y install "$@"; }
_remove() { $_sudo dnf -y remove "$@"; }
;;
arch)
_install() {
if command_exists paru; then
echo_info "Using paru"
paru -S --color always --noconfirm --needed "$@"
elif command_exists yay; then
echo_info "Using yay"
yay -S --color always --noconfirm --needed "$@"
else
echo_warning "Using pacman"
$_sudo pacman -S --color always --noconfirm --needed "$@"
fi
}
_remove() {
if command_exists paru; then
echo_info "Using paru"
paru -R --color always --noconfirm "$@"
elif command_exists yay; then
echo_info "Using yay"
yay -R --color always --noconfirm "$@"
else
echo_warning "Using pacman"
$_sudo pacman -R --color always --noconfirm "$@"
fi
}
;;
opensuse)
_install() { $_sudo zypper in "$@"; }
_remove() { $_sudo zypper rem "$@"; }
;;
alpine)
_install() { apk add "$@"; }
_remove() { apk remove "$@"; }
;;
esac
}
# ─< 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 | zorin | kubuntu | linuxmintubuntu)
ubuntu="true"
distro="ubuntu"
;;
debian | kali | linuxmint | elementary | neon | kdeneon | deepin)
debian="true"
distro="debian"
;;
fedora | centos | rhel | rocky | almalinux)
fedora="true"
distro="fedora"
;;
alpine)
alpine="true"
distro="alpine"
;;
arch | manjaro | garuda | endeavour)
arch="true"
distro="arch"
;;
opensuse*)
opensuse="true"
distro="opensuse"
;;
*)
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
debian="true"
distro="debian"
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
ubuntu="true"
distro="ubuntu"
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
arch="true"
distro="arch"
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
fedora="true"
distro="fedora"
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
opensuse="true"
distro="opensuse"
else
echo_error "Unsupported distribution: $ID"
exit 1
fi
;;
esac
else
echo_warning "Unable to detect distribution /etc/os-release not found."
echo_note "Trying other methods.."
# ─────────────────────────────────────< get packager >─────────────────────────────────────
pkger=""
for pkg in apt-get dnf pacman apk zypper; do
if command_exists $pkg; then
printf "Using ${RED}${pkg}${NC} method.."
pkger="$pkg"
# break
return 0
fi
done
case "$pkger" in
apt-get)
ubuntu="true"
debian="true"
distro="debian"
;;
dnf)
fedora="true"
distro="fedora"
;;
apk)
alpine="true"
distro="alpine"
;;
pacman)
arch="true"
distro="arch"
;;
zypper)
opensuse="true"
distro="opensuse"
;;
*)
echo_error "Can not detect distribution correctly!"
# echo_error "DEBUG:: $pkger ::"
return 69
;;
esac
fi
}
# INFO:
# ╭────────────────────────────────────────────────────────────────────────────────────────╮
# │ Automated setup for refreshing repositories and overall getting the variables to setup │
# ╰────────────────────────────────────────────────────────────────────────────────────────╯
dist_setup() {
case "$distro" in
debian)
_setup debian
# Codename support
if [ -n $VERSION_CODENAME ]; then
case "$VERSION_CODENAME" in
trixie) trixie=true ;;
bookworm) bookworm=true ;;
bullseye) bullseye=true ;;
buster) buster=true ;;
esac
fi
;;
ubuntu)
_setup ubuntu
# Codename support
if [ -n $VERSION_CODENAME ]; then
case "$VERSION_CODENAME" in
noble) noble=true ;;
jammy) jammy=true ;;
focal) focal=true ;;
bionic) bionic=true ;;
esac
fi
;;
fedora)
_setup fedora
# Add version-specific var like: fedora_40=true
fedora_version="fedora_${VERSION_ID//./_}"
eval "$fedora_version=true"
;;
arch)
_setup arch
if command_exists yay || command_exists paru; then
aur=true
else
aur=false
fi
;;
alpine)
_setup alpine
;;
opensuse)
_setup opensuse
;;
esac
}
update_package_list() {
echo_note "Refreshing repositories.."
case "$distro" in
ubuntu | debian) run $_sudo apt-get update ;;
fedora) run $_sudo dnf update ;;
arch)
if command_exists paru; then
run paru -Sy
elif command_exists yay; then
run yay -Sy
else
run $_sudo pacman -Sy
fi
;;
opensuse) run $_sudo zypper ref ;;
alpine) run $_sudo apk update ;;
*) echo_error "Unsupported distribution: $distro" ;;
esac
}
if check_root; then
get_packager
dist_setup
update_package_list
fi