# 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 # Basic Colors BLACK=$'\e[30m' RED=$'\e[31m' GREEN=$'\e[32m' YELLOW=$'\e[33m' BLUE=$'\e[34m' MAGENTA=$'\e[35m' CYAN=$'\e[36m' WHITE=$'\e[37m' # Bright Colors BRIGHT_BLACK=$'\e[90m' BRIGHT_RED=$'\e[91m' BRIGHT_GREEN=$'\e[92m' BRIGHT_YELLOW=$'\e[93m' BRIGHT_BLUE=$'\e[94m' BRIGHT_MAGENTA=$'\e[95m' BRIGHT_CYAN=$'\e[96m' BRIGHT_WHITE=$'\e[97m' # Background Colors (standard) BG_BLACK=$'\e[40m' BG_RED=$'\e[41m' BG_GREEN=$'\e[42m' BG_YELLOW=$'\e[43m' BG_BLUE=$'\e[44m' BG_MAGENTA=$'\e[45m' BG_CYAN=$'\e[46m' BG_WHITE=$'\e[47m' # Background Bright Colors BG_BRIGHT_BLACK=$'\e[100m' BG_BRIGHT_RED=$'\e[101m' BG_BRIGHT_GREEN=$'\e[102m' BG_BRIGHT_YELLOW=$'\e[103m' BG_BRIGHT_BLUE=$'\e[104m' BG_BRIGHT_MAGENTA=$'\e[105m' BG_BRIGHT_CYAN=$'\e[106m' BG_BRIGHT_WHITE=$'\e[107m' # Styles BOLD=$'\e[1m' ITALIC=$'\e[3m' UNDERLINE=$'\e[4m' BLINK=$'\e[5m' # May not work in all terminals INVERT=$'\e[7m' # Invert foreground/background STRIKE=$'\e[9m' # Strikethrough # Reset NC=$'\e[0m' # Reset all styles/colors echo_error() { echo "${BOLD}${RED}${UNDERLINE}ERROR:${NC}${BRIGHT_RED} $1 ${NC}" >&2 } echo_pkg() { echo "${BOLD}${RED}PGK:${NC}${YELLOW} $1 ${NC}" } echo_info() { echo "${BOLD}${BRIGHT_BLUE}INFO:${NC}${BLUE} $1 ${NC}" } echo_warning() { echo "${BOLD}${BRIGHT_YELLOW}WARNING:${NC}${YELLOW} $1 ${NC}" } echo_note() { echo "${BOLD}${GREEN}NOTE:${NC}${BRIGHT_GREEN} $1 ${NC}" } # ─< 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_pkg "$1 is not installed. Installing it now.." run _install "$1" else echo_pkg "skipping $1 - as it's already installed.." fi else # ─< else go though the list of items and do the same >─────────────────────────────────── echo "${BRIGHT_YELLOW}Installing package group -${BOLD}${RED} $@ ${NC}" for deps in "${@}"; do echo_pkg "Installing $deps" if ! command_exists $deps; then echo_pkg "$deps is not installed. Installing it now.." run _install "$deps" else echo_pkg "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_pkg "Using paru" paru -S --color always --noconfirm --needed "$@" elif command_exists yay; then echo_pkg "Using yay" yay -S --color always --noconfirm --needed "$@" else echo_pkg "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