_pirpm_completions() {
    local cur prev words cword
    _init_completion || return

    local commands="install-packages-for-building replace-current-with-remote install-spec-build-requires remove-spec-build-requires build-package version-up commit-update create-edit create-patch help"

    if [[ $cword -eq 1 ]]; then
        COMPREPLY=($(compgen -W "$commands" -- "$cur"))
        return
    fi

    case "${words[1]}" in
        install-spec-build-requires)
            if [[ ! " ${words[*]} " =~ " -r " ]]; then
                COMPREPLY=($(compgen -W "-r" -- "$cur"))
            fi
            if [[ -z "$cur" || "$cur" != -* || "$cur" == "-"* ]]; then
                local used_specs=()
                for ((i=2; i<cword; i++)); do
                    [[ "${words[i]}" == *.spec ]] && used_specs+=("${words[i]}")
                done
                local all_specs=($(compgen -f -X '!*.spec' -- "$cur"))
                local available_specs=()
                for spec in "${all_specs[@]}"; do
                    local found=0
                    for used in "${used_specs[@]}"; do
                        [[ "$spec" == "$used" ]] && found=1 && break
                    done
                    [[ $found -eq 0 ]] && available_specs+=("$spec")
                done
                COMPREPLY+=("${available_specs[@]}")
            fi
            ;;
        build-package)
            if [[ ! " ${words[*]} " =~ " -i " ]]; then
                COMPREPLY=($(compgen -W "-i" -- "$cur"))
            fi
            if [[ -z "$cur" || "$cur" != -* || "$cur" == "-"* ]]; then
                local used_specs=()
                for ((i=2; i<cword; i++)); do
                    [[ "${words[i]}" == *.spec ]] && used_specs+=("${words[i]}")
                done
                local all_specs=($(compgen -f -X '!*.spec' -- "$cur"))
                local available_specs=()
                for spec in "${all_specs[@]}"; do
                    local found=0
                    for used in "${used_specs[@]}"; do
                        [[ "$spec" == "$used" ]] && found=1 && break
                    done
                    [[ $found -eq 0 ]] && available_specs+=("$spec")
                done
                COMPREPLY+=("${available_specs[@]}")
            fi
            ;;
        remove-spec-build-requires|version-up|commit-update|create-patch)
            local used_specs=()
            for ((i=2; i<cword; i++)); do
                [[ "${words[i]}" == *.spec ]] && used_specs+=("${words[i]}")
            done
            local all_specs=($(compgen -f -X '!*.spec' -- "$cur"))
            local available_specs=()
            for spec in "${all_specs[@]}"; do
                local found=0
                for used in "${used_specs[@]}"; do
                    [[ "$spec" == "$used" ]] && found=1 && break
                done
                [[ $found -eq 0 ]] && available_specs+=("$spec")
            done
            COMPREPLY=("${available_specs[@]}")
            ;;
        install-packages-for-building|replace-current-with-remote|help)
            ;;
    esac
} && complete -F _pirpm_completions pirpm