213 lines
7.4 KiB
Shell
213 lines
7.4 KiB
Shell
#!/usr/bin/env bash
|
|
# The package layer: which distro this is, whether a package is installed, and how a
|
|
# package gets installed. BASE_DISTRO is resolved once, here, and exported - every
|
|
# caller reads the same answer instead of asking a second time. Callers source log.sh
|
|
# first: install_if_missing reports through its info/ok/skip/warn, and package_install
|
|
# warns when there is no manager to use. privileges.sh provides caelestia_sudo.
|
|
if [[ -z "${CAELESTIA_PACKAGES_SOURCED:-}" ]]; then
|
|
CAELESTIA_PACKAGES_SOURCED=1
|
|
|
|
# CAELESTIA_OS_RELEASE lets a test point the probe at a fixture rather than assert
|
|
# against whatever the machine it runs on happens to be.
|
|
detect_base_distro() {
|
|
local detected="unknown"
|
|
local os_release="${CAELESTIA_OS_RELEASE:-/etc/os-release}"
|
|
|
|
if [[ -n "${BASE_DISTRO:-}" ]]; then
|
|
printf '%s\n' "$BASE_DISTRO"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -f "$os_release" ]]; then
|
|
. "$os_release"
|
|
case "${ID:-}" in
|
|
arch|cachyos|endeavouros|manjaro|artix)
|
|
detected="arch"
|
|
;;
|
|
fedora|nobara|bazzite|rhel|centos|almalinux|rocky)
|
|
detected="fedora"
|
|
;;
|
|
debian|ubuntu|pop|mint|kali|raspbian|elementary|zorin|deepin|devuan)
|
|
detected="debian"
|
|
;;
|
|
*)
|
|
if echo "${ID_LIKE:-}" | grep -iq "arch"; then
|
|
detected="arch"
|
|
elif echo "${ID_LIKE:-}" | grep -iq "fedora"; then
|
|
detected="fedora"
|
|
elif echo "${ID_LIKE:-}" | grep -iq -E "debian|ubuntu"; then
|
|
detected="debian"
|
|
fi
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
if [[ "$detected" == "unknown" ]]; then
|
|
if command -v pacman >/dev/null 2>&1; then
|
|
detected="arch"
|
|
elif command -v dnf >/dev/null 2>&1; then
|
|
detected="fedora"
|
|
elif command -v apt-get >/dev/null 2>&1; then
|
|
detected="debian"
|
|
fi
|
|
fi
|
|
|
|
printf '%s\n' "$detected"
|
|
}
|
|
|
|
export BASE_DISTRO="$(detect_base_distro)"
|
|
|
|
# Asks this distro's own package manager whether $1 is installed. The distro decides
|
|
# which manager to ask: probing by "whichever tool is on PATH" answers for the wrong
|
|
# package universe on a machine that has a second manager installed alongside.
|
|
package_present() {
|
|
local pkg="$1"
|
|
case "$BASE_DISTRO" in
|
|
arch) pacman -Qq "$pkg" >/dev/null 2>&1 ;;
|
|
fedora) rpm -q "$pkg" >/dev/null 2>&1 ;;
|
|
debian) dpkg -s "$pkg" >/dev/null 2>&1 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
# Echoes the arguments that are not installed yet, one per line.
|
|
filter_missing() {
|
|
local pkg
|
|
for pkg in "$@"; do
|
|
package_present "$pkg" || printf '%s\n' "$pkg"
|
|
done
|
|
}
|
|
|
|
# Echoes the arguments that are installed, one per line.
|
|
filter_installed() {
|
|
local pkg
|
|
for pkg in "$@"; do
|
|
package_present "$pkg" && printf '%s\n' "$pkg"
|
|
done
|
|
}
|
|
|
|
record_failed_package() {
|
|
local dir="${XDG_CACHE_HOME:-$HOME/.cache}/caelestia-niri"
|
|
mkdir -p "$dir"
|
|
printf '%s\n' "$1" >> "$dir/failed_packages.txt"
|
|
}
|
|
|
|
# Installs $1 with this distro's own tool, honouring CONFIRM_ARG. That variable is
|
|
# one flag or nothing at all, so it goes into an array instead of being spliced in
|
|
# bare: an unquoted ${CONFIRM_ARG:-} splits on nothing and vanishes, and a quoted one
|
|
# would hand yay an empty argument to treat as a package name.
|
|
package_install() {
|
|
local pkg="$1"
|
|
local -a confirm=()
|
|
[[ -n "${CONFIRM_ARG:-}" ]] && confirm=("$CONFIRM_ARG")
|
|
|
|
case "$BASE_DISTRO" in
|
|
arch)
|
|
yay -S --needed "${confirm[@]}" "$pkg" 2>/dev/null ||
|
|
caelestia_sudo pacman -S --needed "${confirm[@]}" "$pkg" 2>/dev/null
|
|
;;
|
|
fedora)
|
|
caelestia_sudo dnf install -y "$pkg" 2>/dev/null
|
|
;;
|
|
debian)
|
|
caelestia_sudo apt-get install -y "$pkg" 2>/dev/null
|
|
;;
|
|
*)
|
|
warn "No package manager is known for '$BASE_DISTRO'; cannot install $pkg."
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# A bare Arch has no AUR helper, and the theme, a font and the login screen come from there.
|
|
ensure_yay() {
|
|
command -v yay >/dev/null 2>&1 && return 0
|
|
command -v makepkg >/dev/null 2>&1 || caelestia_sudo pacman -S --needed --noconfirm base-devel git || return 1
|
|
local dir
|
|
dir="$(mktemp -d)"
|
|
git clone --quiet --depth 1 https://aur.archlinux.org/yay-bin.git "$dir/yay-bin" &&
|
|
(cd "$dir/yay-bin" && makepkg -si --noconfirm)
|
|
local status=$?
|
|
rm -rf "$dir"
|
|
return "$status"
|
|
}
|
|
|
|
# Everything in one go: one transaction, one password, no questions. If that fails, each is tried
|
|
# alone, so one bad package does not cost the others and the log says which it was.
|
|
install_bulk() {
|
|
(($#)) || return 0
|
|
local -a repo=() aur=()
|
|
local p
|
|
case "$BASE_DISTRO" in
|
|
arch)
|
|
for p in "$@"; do
|
|
if pacman -Si "$p" >/dev/null 2>&1; then repo+=("$p"); else aur+=("$p"); fi
|
|
done
|
|
if ((${#repo[@]})) && ! ui_run "Installing ${#repo[@]} packages" caelestia_sudo pacman -S --needed --noconfirm "${repo[@]}"; then
|
|
for p in "${repo[@]}"; do
|
|
ui_run "Installing $p" caelestia_sudo pacman -S --needed --noconfirm "$p" || true
|
|
done
|
|
fi
|
|
if ((${#aur[@]})); then
|
|
if ui_run "Setting up yay" ensure_yay; then
|
|
ui_run "Building ${#aur[@]} from the AUR" yay -S --needed --noconfirm --answerclean None --answerdiff None --removemake "${aur[@]}" || true
|
|
else
|
|
warn "These are in the AUR, and yay could not be set up: ${aur[*]}"
|
|
fi
|
|
fi
|
|
;;
|
|
fedora) ui_run "Installing $# packages" caelestia_sudo dnf install -y --skip-unavailable "$@" || true ;;
|
|
debian) ui_run "Installing $# packages" caelestia_sudo apt-get install -y "$@" || true ;;
|
|
*) warn "No package manager is known for '$BASE_DISTRO'."; return 1 ;;
|
|
esac
|
|
for p in "$@"; do
|
|
package_present "$p" || record_failed_package "$p"
|
|
done
|
|
}
|
|
|
|
# Takes out what the installer added only to build, by the same tool.
|
|
remove_packages() {
|
|
local p
|
|
# One at a time: a package something else needs is refused, and the rest still go.
|
|
for p in "$@"; do
|
|
case "$BASE_DISTRO" in
|
|
arch) caelestia_sudo pacman -Rns --noconfirm "$p" || true ;;
|
|
fedora) caelestia_sudo dnf remove -y "$p" || true ;;
|
|
debian) caelestia_sudo apt-get autoremove -y "$p" || true ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# install_if_missing <pkg> [fallback-pkg...]
|
|
#
|
|
# Installs the first candidate that works, and returns 0 as soon as one does. A
|
|
# package that is already present counts as working. A candidate that fails is only
|
|
# recorded once every fallback after it has failed too: recording per attempt would
|
|
# report a package that a fallback went on to install.
|
|
install_if_missing() {
|
|
local pkg
|
|
local -a failed=()
|
|
|
|
for pkg in "$@"; do
|
|
if package_present "$pkg"; then
|
|
skip "$pkg already installed."
|
|
return 0
|
|
fi
|
|
|
|
info "Installing $pkg..."
|
|
if package_install "$pkg"; then
|
|
ok "$pkg installed."
|
|
return 0
|
|
fi
|
|
|
|
warn "Could not install $pkg, skipping."
|
|
failed+=("$pkg")
|
|
done
|
|
|
|
for pkg in "${failed[@]}"; do
|
|
record_failed_package "$pkg"
|
|
done
|
|
return 1
|
|
}
|
|
|
|
fi
|