imports/distros.sh
2025-05-21 17:37:51 +02:00

575 lines
16 KiB
Bash
Executable file

# INFO:
# ╭──────────────────────────────╮
# │ User variables and functions │
# ╰──────────────────────────────╯
distro=""
ubuntu="false"
debian="false"
arch="false"
fedora="false"
alpine="false"
opensuse="false"
# ─< old - 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
}
# INFO:
# ↓ should get set in the install script itself
# ↓
# echo with $PACKAGE and first argument, if 2 exist
echo_pkg() {
# if arg 2 does not exist, use normal echo
if [[ -z $2 ]]; then
case "$1" in
build)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}-build:${NC}${BRIGHT_BLUE} Building $PACKAGE ${NC}"
;;
clone)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}-clone:${NC}${BRIGHT_YELLOW} Cloning $PACKAGE sources..${NC}"
;;
install)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}-install:${NC}${BRIGHT_GREEN} Installing $PACKAGE now!${NC}"
;;
*)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}:${NC}${YELLOW} $1 ${NC}"
;;
esac
else
case "$1" in
deps | dep | dependencies)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}-${pkger:-dependencies}:${BRIGHT_YELLOW} $2 ${NC}"
;;
*)
echo "${BOLD}${BRIGHT_RED}${PACKAGE:-PKG}-$1:${NC}${BRIGHT_YELLOW} $2 ${NC}"
;;
esac
fi
}
echo_info() {
echo "${BOLD}${BLUE}INFO:${NC}${BRIGHT_BLUE} $1${NC}"
}
echo_warning() {
echo "${BOLD}${YELLOW}WARNING:${NC}${BRIGHT_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
}
source_script() {
local url="$1"
local import="$(mktemp)"
# ─< if $1 is a local file, source this one instead >─────────────────────────────────────
if [ -f "$url" ]; then
source "$url"
sleep 0.1
return 0
else
echo_info "Sourcing external script:${NC} $url"
# ─< if $1 is a url, grab it and source it, also deletes afterwards >─────────────────────
if command_exists curl; then
curl -fsSL $url -o $import
elif command_exists wget; then
wget -o $import $url
else
echo "curl/wget is required, but missing.."
exit 69
fi
source "$import"
sleep 0.1
rm -f "$import"
fi
}
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
if run _install "$1"; then
echo_pkg deps "$1 - ${GREEN}installed"
else
echo_pkg deps "$1 is already installed.."
fi
else
echo_pkg deps "skipping $1 - as it's ${RED}already installed.."
fi
else
# ─< else go though the list of items and do the same >───────────────────────────────────
echo_pkg deps "${BRIGHT_YELLOW}Installing ${RED}${#@}${BRIGHT_YELLOW} packages.. - ${BRIGHT_BLUE} $* ${NC}"
for deps in "${@}"; do
# echo_pkg deps "Installing $deps"
if ! command_exists $deps; then
if run _install "$deps"; then
echo_pkg deps "$deps - ${GREEN}installed"
else
echo_pkg deps "$deps is already installed.."
fi
else
echo_pkg deps "skipping $1 - as it's ${RED}already installed.."
fi
done
fi
}
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
check_env() {
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
pikaCheckFile="$HOME/.cache/pika_script_detection"
checkFileAge() {
local file="$1"
[[ ! -e "$file" ]] && return 2 # File doesn't exist
if [[ $(find "$file" -mtime +7 -print) ]]; then
return 69 # File is older than 1 week
else
return 0 # File is within 1 week
fi
}
if [ -f "$pikaCheckFile" ]; then
if checkFileAge $pikaCheckFile; then
PIKA_INIT=true
else
unset PIKA_INIT
fi
else
unset PIKA_INIT
fi
}
# CAUTION:
# ╭─────────────────────────────────────────────────────────────────────╮
# │ This can break really quickly, since the _remove() function removes │
# │ without confirmation! use with CAUTION!! │
# ╰─────────────────────────────────────────────────────────────────────╯
_setup() {
case "$1" in
debian | ubuntu)
_install() {
# $_sudo apt-get install --assume-yes "$@"
if command_exists nala; then
pkger=nala
$_sudo nala install --assume-yes "$@"
else
pkger=apt-get
$_sudo apt-get install --assume-yes "$@"
fi
}
# CAUTION:
_remove() {
if command_exists nala; then
pkger=nala
$_sudo nala remove --assume-yes "$@"
$_sudo nala autoremove --assume-yes
$_sudo nala autopurge --assume-yes
else
pkger=apt-get
# echo_pkg "Using pacman"
$_sudo apt-get remove --assume-yes "$@"
$_sudo apt-get autoremove --assume-yes
fi
}
;;
fedora)
_install() {
pkger=dnf
$_sudo dnf -y install "$@"
}
# CAUTION:
_remove() {
pkger=dnf
$_sudo dnf -y remove "$@"
}
;;
arch)
checkAUR() {
if ! command_exists yay && ! command_exists paru; then
return 69
else
return 0
fi
}
_install() {
if command_exists paru; then
pkger=paru
# echo_pkg "Using paru"
paru -S --color always --noconfirm --needed "$@"
elif command_exists yay; then
pkger=yay
# echo_pkg "Using yay"
yay -S --color always --noconfirm --needed "$@"
else
pkger=pacman
# echo_pkg "Using pacman"
$_sudo pacman -S --color always --noconfirm --needed "$@"
fi
}
# CAUTION:
_remove() {
if command_exists paru; then
pkger=paru
# echo_info "Using paru"
paru -R --color always --noconfirm "$@"
elif command_exists yay; then
pkger=yay
# echo_info "Using yay"
yay -R --color always --noconfirm "$@"
else
pkger=pacman
# echo_warning "Using pacman"
$_sudo pacman -R --color always --noconfirm "$@"
fi
}
;;
opensuse)
_install() {
pkger=zypper
$_sudo zypper in "$@"
}
# CAUTION:
_remove() {
pkger=zypper
$_sudo zypper rem "$@"
}
;;
alpine)
_install() {
pkger=apk
apk add "$@"
}
# CAUTION:
_remove() {
pkger=apk
apk remove "$@"
}
;;
esac
}
# ─< Distribution detection and installation >────────────────────────────────────────
get_packager() {
fallback() {
# ─────────────────────────────────────< get packager >─────────────────────────────────────
local 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
}
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"
echo_warning "Trying fallback.."
fallback
fi
;;
esac
else
echo_warning "Unable to detect distribution - /etc/os-release not found."
echo_note "Trying other methods.."
sleep 3
fallback
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_info "Refreshing repository sources.."
case "$distro" in
ubuntu | debian) run $_sudo apt-get update ;;
fedora) run $_sudo dnf update ;;
arch)
if command_exists pacman; then
if ! checkAUR; then
local paruBuildDir="/opt/builds"
local USER="$(whoami)"
$_sudo mkdir -p "$paruBuildDir"
# if ! command_exists paru; then
echo "${YELLOW}Installing paru as AUR helper...${NC}"
run $_sudo pacman -S --needed --noconfirm base-devel git
cd "$paruBuildDir" && $_sudo git clone https://aur.archlinux.org/paru-bin.git paru
$_sudo chown -R "$USER": "$paruBuildDir/paru"
cd "$paruBuildDir/paru" && makepkg --noconfirm -si
echo "${GREEN}Paru installed${NC}"
# else
# printf "%b\n" "${GREEN}Paru already installed${RC}"
# fi
fi
fi
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: ${BRIGHT_RED}$distro"
return 69
;;
esac
}
if check_env; then
get_packager
dist_setup
# WHY:
# ╭─────────────────────────────────────────────────────────────────────────╮
# │ check if the script has run at least once, so that the sources dont │
# │ have to get updated again.. │
# ╰─────────────────────────────────────────────────────────────────────────╯
if [[ -z "$PIKA_INIT" ]]; then
echo_pkg "First time being imported.."
if update_package_list; then
touch "$pikaCheckFile"
fi
else
echo_note "Skipping repo refresh (PIKA_INIT is already set)"
fi
fi