502 lines
14 KiB
Bash
502 lines
14 KiB
Bash
# INFO:
|
|
# ╭──────────────────────────────╮
|
|
# │ User variables and functions │
|
|
# ╰──────────────────────────────╯
|
|
distro=""
|
|
ubuntu="false"
|
|
debian="false"
|
|
arch="false"
|
|
fedora="false"
|
|
alpine="false"
|
|
opensuse="false"
|
|
|
|
# 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'
|
|
|
|
# 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_error() {
|
|
echo-error "$@"
|
|
}
|
|
|
|
echo-info() {
|
|
echo "${BOLD}${BLUE}INFO:${NC} $1${NC}"
|
|
}
|
|
|
|
echo_info() {
|
|
echo-info "$@"
|
|
}
|
|
|
|
echo-warning() {
|
|
echo "${BOLD}${YELLOW}WARNING:${NC} $1${NC}"
|
|
}
|
|
|
|
echo_warning() {
|
|
echo-warning "$@"
|
|
}
|
|
|
|
echo-note() {
|
|
echo "${BOLD}${GREEN}NOTE:${NC} $1${NC}"
|
|
}
|
|
|
|
echo_note() {
|
|
echo-note "$@"
|
|
}
|
|
|
|
# ─< Check if the given command exists silently >─────────────────────────────────────────
|
|
command_exists() {
|
|
command -v "$@" >/dev/null 2>&1
|
|
}
|
|
|
|
silentexec() {
|
|
"$@" >/dev/null 2>&1
|
|
}
|
|
|
|
# if given an array, it checks if the command is available, and if not - installs all packages in that array one by one
|
|
checkAndInstall() {
|
|
local pkg=$1
|
|
# echo "DEBUG:: GOT ARRAY :: ${@}"
|
|
# INFO: if it's not a list, then just check and install the package..
|
|
if [[ -z $2 ]]; then
|
|
if ! command_exists "$pkg"; then
|
|
echo "${GREY}${BOLD}Installing ${GREEN}$pkg${NC}"
|
|
if pkg-install "$pkg"; then
|
|
echo "${GREEN}${BOLD}Installed $pkg${NC}"
|
|
else
|
|
echo-error "Installing $pkg"
|
|
fi
|
|
else
|
|
echo "${BOLD}${GREY}$pkg is already installed..${NC}"
|
|
fi
|
|
else
|
|
# ─< else go though the list of items and do the same >───────────────────────────────────
|
|
echo "${BRIGHT_YELLOW}Installing ${RED}${#@}${BRIGHT_YELLOW} packages..${NC}"
|
|
# echo "${BRIGHT_BLUE} $* ${NC}"
|
|
for deps in "${@}"; do
|
|
if ! command_exists $deps; then
|
|
if pkg-install "$deps"; then
|
|
echo_pkg deps "$deps - ${GREEN}installed"
|
|
else
|
|
echo_pkg deps "$deps is already installed.."
|
|
fi
|
|
else
|
|
echo_pkg deps "skipping $deps - as it's ${RED}already installed.."
|
|
fi
|
|
done
|
|
fi
|
|
}
|
|
|
|
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
|
|
check-environment() {
|
|
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
|
|
|
|
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
|
|
}
|
|
|
|
pikaCheckFile="$HOME/.cache/pika_script_detection"
|
|
|
|
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 pkg-remove() function removes │
|
|
# │ without confirmation! use with CAUTION!! │
|
|
# ╰─────────────────────────────────────────────────────────────────────╯
|
|
_setup() {
|
|
case "$1" in
|
|
debian | ubuntu)
|
|
pkg-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:
|
|
pkg-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)
|
|
pkg-install() {
|
|
pkger=dnf
|
|
$_sudo dnf -y install "$@"
|
|
}
|
|
|
|
# CAUTION:
|
|
pkg-remove() {
|
|
pkger=dnf
|
|
$_sudo dnf -y remove "$@"
|
|
}
|
|
;;
|
|
arch)
|
|
checkAUR() {
|
|
if ! command_exists yay && ! command_exists paru; then
|
|
return 69
|
|
else
|
|
return 0
|
|
fi
|
|
}
|
|
|
|
pkg-install() {
|
|
if command_exists paru; then
|
|
paru -S --color always --noconfirm --needed "$@"
|
|
elif command_exists yay; then
|
|
yay -S --color always --noconfirm --needed "$@"
|
|
else
|
|
$_sudo pacman -S --color always --noconfirm --needed "$@"
|
|
fi
|
|
}
|
|
|
|
# CAUTION:
|
|
pkg-remove() {
|
|
if command_exists paru; then
|
|
paru -R --color always --noconfirm "$@"
|
|
elif command_exists yay; then
|
|
yay -R --color always --noconfirm "$@"
|
|
else
|
|
$_sudo pacman -R --color always --noconfirm "$@"
|
|
fi
|
|
}
|
|
;;
|
|
opensuse)
|
|
pkg-install() {
|
|
$_sudo zypper in "$@"
|
|
}
|
|
|
|
# CAUTION:
|
|
pkg-remove() {
|
|
$_sudo zypper rem "$@"
|
|
}
|
|
;;
|
|
alpine)
|
|
pkg-install() {
|
|
$_sudo apk add "$@"
|
|
}
|
|
|
|
# CAUTION:
|
|
pkg-remove() {
|
|
$_sudo apk remove "$@"
|
|
}
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# ─< Distribution detection and installation >────────────────────────────────────────
|
|
get-packagemanager() {
|
|
# ─< define fallback function >───────────────────────────────────────────────────────────
|
|
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 │
|
|
# ╰────────────────────────────────────────────────────────────────────────────────────────╯
|
|
|
|
distro-setup() {
|
|
case "$distro" in
|
|
debian)
|
|
echo "${BOLD}Found ${RED}debian${NC}"
|
|
_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)
|
|
echo "${BOLD}Found ${BRIGHT_YELLOW}ubuntu${NC}"
|
|
_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)
|
|
echo "${BOLD}Found ${BRIGHT_BLUE}fedora${NC}"
|
|
_setup fedora
|
|
# Add version-specific var like: fedora_40=true
|
|
fedora_version="fedora_${VERSION_ID//./_}"
|
|
eval "$fedora_version=true"
|
|
;;
|
|
arch)
|
|
echo "${BOLD}Found ${BLUE}arch${NC}"
|
|
_setup arch
|
|
if command_exists yay || command_exists paru; then
|
|
aur=true
|
|
else
|
|
aur=false
|
|
fi
|
|
;;
|
|
alpine)
|
|
echo "${BOLD}Found ${BLUE}alpine${NC}"
|
|
_setup $distro
|
|
;;
|
|
opensuse)
|
|
echo "${BOLD}Found ${GREEN}opensuse${NC}"
|
|
_setup $distro
|
|
;;
|
|
esac
|
|
}
|
|
|
|
update_package_list() {
|
|
local USER="${USER:-$(whoami)}"
|
|
|
|
echo_info "Refreshing repository sources.."
|
|
case "$distro" in
|
|
ubuntu | debian) $_sudo apt-get update ;;
|
|
fedora) $_sudo dnf update ;;
|
|
arch)
|
|
if command_exists pacman; then
|
|
if ! checkAUR; then
|
|
local paruBuildDir="/opt/builds"
|
|
$_sudo mkdir -p "$paruBuildDir"
|
|
|
|
echo "${YELLOW}Installing paru as AUR helper...${NC}"
|
|
$_sudo pacman -S --needed --noconfirm base-devel git
|
|
|
|
cd "$paruBuildDir" && echo "${YELLOW} Cloning paru from ${NC}https://aur.archlinux.org/paru-bin.git"
|
|
$_sudo git clone https://aur.archlinux.org/paru-bin.git paru
|
|
|
|
$_sudo chown -R "$USER": "$paruBuildDir/paru"
|
|
cd "$paruBuildDir/paru"
|
|
|
|
echo "${GREEN}${BOLD}Installing paru"
|
|
makepkg --noconfirm -si
|
|
sleep 1
|
|
|
|
if command_exists paru; then
|
|
echo "${GREEN}Paru installed${NC}"
|
|
else
|
|
echo "${RED}${BOLD}Error: Something went wrong when installing paru!"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
sleep 0.01
|
|
|
|
if command_exists paru; then
|
|
paru -Sy
|
|
elif command_exists yay; then
|
|
yay -Sy
|
|
else
|
|
$_sudo pacman -Sy
|
|
fi
|
|
;;
|
|
opensuse) $_sudo zypper ref ;;
|
|
alpine) $_sudo apk update ;;
|
|
*)
|
|
echo-error "Unsupported distribution: ${BRIGHT_RED}$distro"
|
|
return 69
|
|
;;
|
|
esac
|
|
}
|
|
|
|
if check-environment; then
|
|
get-packagemanager
|
|
distro-setup
|
|
|
|
# WHY:
|
|
# ╭─────────────────────────────────────────────────────────────────────────╮
|
|
# │ check if the script has run at least once, so that the sources dont │
|
|
# │ have to get updated again.. │
|
|
# ╰─────────────────────────────────────────────────────────────────────────╯
|
|
pikaCheckFile="$HOME/.cache/pika_script_detection"
|
|
|
|
if ! checkFileAge "$pikaCheckFile"; then
|
|
echo "${BOLD}${GREY}Updating sources..${NC}"
|
|
|
|
if update-package-list; then
|
|
silentexec touch "$pikaCheckFile"
|
|
fi
|
|
else
|
|
echo "${BOLD}${GREY}Skipping repo refresh${NC}"
|
|
fi
|
|
fi
|