#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat <<'EOF'
Usage: package-installer <package-file>

Install packages listed in <package-file> using paru.
Lines starting with # and blank lines are ignored.
EOF
}

if [[ ${1:-} == "" || ${1:-} == "-h" || ${1:-} == "--help" ]]; then
  usage
  exit 0
fi

package_file="$1"

if [[ ! -f "$package_file" ]]; then
  echo "Error: file not found: $package_file" >&2
  exit 1
fi

mapfile -t packages < <(
  sed -e 's/#.*$//' -e 's/[[:space:]]*$//' "$package_file" | awk 'NF'
)

if (( ${#packages[@]} == 0 )); then
  echo "No packages to install (file is empty after filtering)."
  exit 0
fi

paru -S --needed --noconfirm -- "${packages[@]}"
