Files
shellscripts/install-packages.sh
2026-04-01 00:02:22 +07:00

37 lines
1.2 KiB
Bash

#!/bin/bash
# Install all packages from this machine via pacman.
# --needed skips already-installed packages.
# Only leaf packages are listed; dependencies are pulled in automatically.
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PACKAGE_FILE="$SCRIPT_DIR/packages.txt"
if [[ ! -f "$PACKAGE_FILE" ]]; then
echo "Error: $PACKAGE_FILE not found"
exit 1
fi
# Read packages from file, skipping blank lines and comments
mapfile -t packages < <(grep -v '^\s*#' "$PACKAGE_FILE" | grep -v '^\s*$')
if [[ ${#packages[@]} -eq 0 ]]; then
echo "No packages found in $PACKAGE_FILE"
exit 1
fi
echo "Resolving ${#packages[@]} packages..."
# Dry-run to find unknown packages (no sudo needed for --print)
mapfile -t skipped < <(pacman -S --needed --print "${packages[@]}" 2>&1 >/dev/null \
| grep "target not found" | awk -F': ' '{print $2}')
if [[ ${#skipped[@]} -gt 0 ]]; then
echo "Skipping ${#skipped[@]} package(s) not found: ${skipped[*]}"
mapfile -t packages < <(comm -23 \
<(printf '%s\n' "${packages[@]}" | sort) \
<(printf '%s\n' "${skipped[@]}" | sort))
fi
echo "Installing ${#packages[@]} packages..."
sudo pacman -S --needed "${packages[@]}"