function _mirrorrib {
    local cur prev words cword
    declare -a words
    declare -i cword
    _init_completion || return
    readonly cur prev words cword

    # Count arguments up to the cursor.
    local n_args n_words word do_enable_opts
    local is_optarg_expected is_optarg_announced is_optarg
    declare -i n_args=0
    declare -i n_words=$cword;\
     (($cword + 1 >= ${#words[@]})) && [ -z "${words[$cword]}" ]\
     && ((--n_words))
    readonly n_words
    declare -i do_enable_opts=1
    declare -i is_optarg_expected=0
    declare -i is_optarg_announced=0
    declare -i is_optarg=0
    for word in "${words[@]:1:$n_words}"; do
        is_optarg_announced=0
        is_optarg=0
        if (($n_args)); then
            # Options have ended and args have begun,
            # so count the present arg.
            ((++n_args))
        elif (($is_optarg_expected)); then
            # Do not count the optarg.
            is_optarg=1
        elif (($do_enable_opts)) && [[ "$word" = -?* ]]; then
            # Do not count the option cluser, but ...
            [[ "$word" = -*([^p])p  ]] && is_optarg_announced=1
            [[ "$word" = -*([^p])-* ]] && do_enable_opts=0
        else
            # This is the first argument. Count it.
            ((++n_args))
        fi
        is_optarg_expected=$is_optarg_announced
    done
    readonly is_optarg is_optarg_expected

    # Complete the argument at the cursor.
    local do_complete_arg
    declare -i do_complete_arg=0
    if (($cword <= $n_words)); then
        # The cursor is still in a word.
        if (($is_optarg)); then
            _filedir
        elif (($n_args <= 1)) && [[ "$cur" = - ]]; then
            COMPREPLY=( $(compgen -W '-l -s -? -p' -- "$cur") )
        elif [[ "$cur" = -* ]]; then
            COMPREPLY=( $(compgen -- "$cur") )
        elif (($n_args)); then
            do_complete_arg=1
        else
            COMPREPLY=( $(compgen -- "$cur") )
        fi
    else
        # The cursor has advanced past the last word.
        if (($is_optarg_expected)); then
            _filedir
        else
            ((++n_args))
            do_complete_arg=1
        fi
    fi
    readonly n_args do_complete_arg
    if (($do_complete_arg)); then
        case $n_args in
            4|5|6) _filedir -d;;
            *) COMPREPLY=( $(compgen -- "$cur") );;
        esac
    fi
    return 0
} &&
complete -F _mirrorrib mirrorrib
