#compdef pacstrap

_pacstrap_args=(
    '-h[display help]'
)

_pacstrap_args_nonh=(
    '-C[Use an alternate config file for pacman]:config file:_files -/'
    '-c[Use the package cache on the host, rather than the target]'
    '-D[Skip pacman dependency checks]'
    '-G[Avoid copying the host pacman keyring to the target]'
    '-i[Prompt for package confirmation when needed (run interactively)]'
    '-K[Initialize an empty pacman keyring in the target (implies -G)]'
    '-M[Avoid copying the host mirrorlist to the target]'
    '-N[Run in unshare mode as a regular user]'
    '-P[Copy the host pacman config to the target]'
    '-U[Use pacman -U to install packages]'
)


# builds command for invoking pacman in a _call_program command - extracts
# relevant options already specified (config file, etc)
# $cmd must be declared by calling function
_pacman_get_command() {
	# this is mostly nicked from _perforce
	cmd=( "pacman" "2>/dev/null")
	integer i
	for (( i = 2; i < CURRENT - 1; i++ )); do
		if [[ ${words[i]} = "--config" || ${words[i]} = "--root" ]]; then
			cmd+=( ${words[i,i+1]} )
		fi
	done
}

# provides completions for packages available from repositories
# these can be specified as either 'package' or 'repository/package'
_pacman_completions_all_packages() {
	local -a cmd packages repositories packages_long
	_pacman_get_command

	if compset -P1 '*/*'; then
		packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) )
		typeset -U packages
		_wanted repo_packages expl "repository/package" compadd ${(@)packages}
	else
		packages=( $(_call_program packages $cmd[@] -Sql) )
		typeset -U packages
		_wanted packages expl "packages" compadd - "${(@)packages}"

		repositories=(${(o)${${${(M)${(f)"$(</etc/pacman.conf)"}:#\[*}/\[/}/\]/}:#options})
		typeset -U repositories
		_wanted repo_packages expl "repository/package" compadd -S "/" $repositories
	fi
}

_pacstrap_none(){
    _arguments -s : \
        "$_pacstrap_args[@]" \
        "$_longopts[@]" \
}

_longopts=( '--help[display help]' )

_pacstrap(){
    if [[ -z ${(M)words:#--help} && -z ${(M)words:#-h} ]]; then
        case $words[CURRENT] in
            -c*|-d*|-i*)
                _arguments -s "$_pacstrap_args_nonh[@]"
                ;;
            -*)
                _arguments -s : \
                    "$_pacstrap_args[@]" \
                    "$_pacstrap_args_nonh[@]" \
                    "$_longopts[@]"
                ;;
            --*)
                _arguments -s : \
                    "$_longopts[@]"
                ;;
            *)
                _arguments -s : \
                    "$_pacstrap_args[@]" \
                    "$_pacstrap_args_nonh[@]" \
                    "$_longopts[@]" \
                    ":*:_path_files -/" \
                    ":*:_pacman_completions_all_packages"
                ;;
        esac
    else
        return 1
    fi
}

_install_scripts(){
    case "$service" in
        pacstrap)
            _pacstrap "$@";;
    esac
}

_install_scripts "$@"
