Files
2026-09-25 14:26:30 +02:00

127 lines
4.0 KiB
Bash

#!/bin/bash
# setup/lib/common.sh
#
# Shared helpers for the setup/install scripts under setup/.
# Source this file; do not execute it directly:
#
# SETUP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# # shellcheck source=../lib/common.sh
# source "$SETUP_DIR/../lib/common.sh"
#
# Conventions:
# - The caller owns `set -euo pipefail` and PACKAGES_FILE.
# - Package lists use @pacman (default) / @aur / @npm section markers;
# '#' starts a comment, blank lines are ignored.
# - Set NPM_USE_SUDO=1 before calling install_npm_pkgs when the global
# npm root is root-owned (Arch installs npm to /usr/lib/node_modules).
# Print the repository root (setup/lib/ -> repo root).
repo_root() {
cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd
}
log() {
echo "==> $*"
}
# ---------------------------------------------------------------------------
# Read a packages.txt file, split into pacman / aur / npm sections.
# Usage: read_packages <file> <pacman_array> <aur_array> <npm_array>
# ---------------------------------------------------------------------------
read_packages() {
local file="$1" pacman_ref="$2" aur_ref="$3" npm_ref="$4"
local section="pacman"
local line current
local -n _pacman="$pacman_ref" _aur="$aur_ref" _npm="$npm_ref"
while IFS= read -r line || [[ -n "$line" ]]; do
current="${line%%#*}"
current="${current##*( )}"
current="${current%%*( )}"
[[ -z "$current" ]] && continue
case "$current" in
@pacman) section="pacman" ;;
@aur) section="aur" ;;
@npm) section="npm" ;;
*)
case "$section" in
pacman) _pacman+=("$current") ;;
aur) _aur+=("$current") ;;
npm) _npm+=("$current") ;;
esac
;;
esac
done < "$file"
}
# ---------------------------------------------------------------------------
# Install sections (arrays passed by nameref).
# ---------------------------------------------------------------------------
install_pacman_pkgs() {
local -n _pkgs="$1"
if ((${#_pkgs[@]})); then
log "Installiere Pacman-Pakete: ${_pkgs[*]}"
yay -S --needed "${_pkgs[@]}"
fi
}
install_aur_pkgs() {
local -n _pkgs="$1"
if ((${#_pkgs[@]})); then
log "Installiere AUR-Pakete: ${_pkgs[*]}"
# --aur selects yay's AUR mode. For names that a repository package
# can also provide, use an explicit aur/<package> target in the list.
yay -S --aur --needed "${_pkgs[@]}"
fi
}
install_npm_pkgs() {
local -n _pkgs="$1"
if ((${#_pkgs[@]})); then
log "Installiere globale npm-Pakete: ${_pkgs[*]}"
if [[ "${NPM_USE_SUDO:-0}" == "1" ]]; then
sudo npm install -g "${_pkgs[@]}"
else
npm install -g "${_pkgs[@]}"
fi
fi
}
# Install all three sections at once.
install_all_pkgs() {
install_pacman_pkgs "$1"
install_aur_pkgs "$2"
install_npm_pkgs "$3"
}
# ---------------------------------------------------------------------------
# Enable multilib in /etc/pacman.conf (with a safety backup).
# ---------------------------------------------------------------------------
enable_multilib() {
sudo sed -i.bak '/^#\[multilib\]/,+1 s/^#//' /etc/pacman.conf
}
# ---------------------------------------------------------------------------
# Ensure yay (AUR helper) is installed.
# ---------------------------------------------------------------------------
ensure_yay() {
if pacman -Q yay &>/dev/null; then
echo "yay bereits installiert; fahre fort."
return
fi
if pacman -Si yay &>/dev/null; then
echo "Installiere yay mit pacman ..."
sudo pacman -S --needed yay
return
fi
echo "installiere yay via git ..."
sudo pacman -S --needed base-devel git
local tmp
tmp="$(mktemp -d)"
git clone https://aur.archlinux.org/yay.git "$tmp/yay"
(
cd "$tmp/yay"
makepkg -si
)
rm -rf "$tmp"
}