# 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 } 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 >─────────────────────────────────── 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 } # ─< 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" _install() { $_sudo apt-get install --assume-yes "$@"; } # Codename support case "$VERSION_CODENAME" in noble) noble=true ;; jammy) jammy=true ;; focal) focal=true ;; bionic) bionic=true ;; esac ;; debian | kali | linuxmint | elementary | neon | kdeneon | deepin) debian="true" distro="debian" _install() { $_sudo apt-get install --assume-yes "$@"; } case "$VERSION_CODENAME" in trixie) trixie=true ;; bookworm) bookworm=true ;; bullseye) bullseye=true ;; buster) buster=true ;; esac ;; fedora | centos | rhel | rocky | almalinux) fedora="true" distro="fedora" _install() { $_sudo dnf install -y "$@"; } # Add version-specific var like: fedora_40=true fedora_version="fedora_${VERSION_ID//./_}" eval "$fedora_version=true" ;; alpine) alpine="true" distro="alpine" _install() { $_sudo apk add "$@"; } ;; arch | manjaro | garuda | endeavour) arch="true" distro="arch" if command_exists yay || command_exists paru; then aur=true else aur=false fi _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 } ;; opensuse*) opensuse="true" distro="opensuse" _install() { $_sudo zypper in "$@"; } ;; *) if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then debian="true" distro="debian" _install() { $_sudo apt-get install --assume-yes "$@"; } elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then ubuntu="true" distro="ubuntu" _install() { $_sudo apt-get install --assume-yes "$@"; } elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then arch="true" distro="arch" _install() { $_sudo pacman -S --noconfirm "$@"; } elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then fedora="true" distro="fedora" _install() { $_sudo dnf install -y "$@"; } elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then opensuse="true" distro="opensuse" _install() { $_sudo zypper in "$@"; } 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" _install() { $_sudo apt-get install --assume-yes "$@"; } ;; dnf) fedora="true" distro="fedora" _install() { $_sudo dnf install -y "$@"; } ;; apk) alpine="true" distro="alpine" _install() { $_sudo apk add "$@"; } ;; pacman) arch="true" distro="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 } ;; zypper) opensuse="true" distro="opensuse" _install() { $_sudo zypper in "$@"; } ;; *) 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 │ # ╰────────────────────────────────────────────────────────────────────────────────────────╯ 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 && update_package_list fi