39 lines · 984 B · executable
#!/usr/bin/env bash
# arch-pkgs - Manage Arch Linux package lists
# Usage: arch-pkgs [export|install]
#   export  - Write current packages to arch/pacman.txt and arch/aur.txt (default)
#   install - Install all packages from arch/pacman.txt and arch/aur.txt

set -euo pipefail

if [[ ! -f /etc/os-release ]]; then
    echo "Error: /etc/os-release not found" >&2
    exit 1
fi

# shellcheck source=/etc/os-release
source /etc/os-release

if [[ "$ID" != "arch" ]]; then
    echo "Error: this script only runs on Arch Linux (detected: $ID)" >&2
    exit 1
fi

DOTFILES="${DOTFILES:-$HOME/.dotfiles}"

cmd="${1:-export}"

case "$cmd" in
export)
    pacman -Qen | awk '{print $1}' | sort > "$DOTFILES/arch/pacman.txt"
    pacman -Qem | awk '{print $1}' | sort > "$DOTFILES/arch/aur.txt"
    ;;
install)
    pacman -S --needed - < "$DOTFILES/arch/pacman.txt"
    paru -S --needed - < "$DOTFILES/arch/aur.txt"
    ;;
*)
    echo "Usage: arch-pkgs [export|install]" >&2
    exit 1
    ;;
esac