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

# miaur — pure-shell AUR helper (paru-like daily workflow)

MIAUR_HOME=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)

# shellcheck source=lib/common.sh
source "$MIAUR_HOME/lib/common.sh"
# shellcheck source=lib/config.sh
source "$MIAUR_HOME/lib/config.sh"
# shellcheck source=lib/aur_rpc.sh
source "$MIAUR_HOME/lib/aur_rpc.sh"
# shellcheck source=lib/clone.sh
source "$MIAUR_HOME/lib/clone.sh"
# shellcheck source=lib/review.sh
source "$MIAUR_HOME/lib/review.sh"
# shellcheck source=lib/deps.sh
source "$MIAUR_HOME/lib/deps.sh"
# shellcheck source=lib/build.sh
source "$MIAUR_HOME/lib/build.sh"
# shellcheck source=lib/upgrade.sh
source "$MIAUR_HOME/lib/upgrade.sh"
# shellcheck source=lib/cli.sh
source "$MIAUR_HOME/lib/cli.sh"
# shellcheck source=lib/dispatch.sh
source "$MIAUR_HOME/lib/dispatch.sh"

main() {
	miaur_load_config

	local -a orig=("$@")
	parse_args "$@"

	# Pure pacman passthrough: forward original argv minus miaur-only options
	# so long options like --ignore survive (unlike short-cluster reconstruction).
	if [[ $DO_PASSTHRU -eq 1 && $OP != S && $OP != Q && $OP != G && $OP != B && $OP != interactive ]]; then
		local -a filtered=()
		mapfile -t filtered < <(strip_miaur_opts "${orig[@]}")
		[[ ${#filtered[@]} -gt 0 ]] || die "nothing to pass through to pacman"
		as_root ${MIAUR_PACMAN} "${filtered[@]}"
		return $?
	fi

	dispatch
}

main "$@"
