# Bash completion for lc and lc-add-repo (Local Copr)
# Installed as /usr/share/bash-completion/completions/{lc,lc-add-repo}

_lc() {
    local cur prev subcmd opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "init remove build" -- "${cur}") )
        return 0
    fi

    subcmd="${COMP_WORDS[1]}"
    case "${subcmd}" in
        init)   opts="--repo --gpg-key" ;;
        remove) opts="--repo" ;;
        build)  opts="--source --torepo --spec --addrepo --storage --extra-mock-args --enable-network --conf --mock-config" ;;
        *) return 0 ;;
    esac

    # Flag position: complete the subcommand's options.
    if [[ "${cur}" == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
        return 0
    fi

    # Value position: what we can offer depends on which flag precedes us.
    case "${prev}" in
        --source|--torepo)   COMPREPLY=( $(compgen -d -- "${cur}") ) ;;              # repo directory
        --spec|--conf)       COMPREPLY=( $(compgen -f -- "${cur}") ) ;;              # file
        --storage)           COMPREPLY=( $(compgen -W "tmpfs tmpfs-tmponly ssd" -- "${cur}") ) ;;
        --repo|--gpg-key|--addrepo|--extra-mock-args|--mock-config)
                             COMPREPLY=( $(compgen -o default -- "${cur}") ) ;;      # free-form value: fall back to bash's default completion
        *)  # not a value slot (after the subcommand, after a store_true flag, or
            # after an already-consumed value): offer the option list again.
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) ;;
    esac
}

_lc_add_repo() {
    local cur prev subcmd opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "add remove refresh list" -- "${cur}") )
        return 0
    fi

    subcmd="${COMP_WORDS[1]}"
    case "${subcmd}" in
        add)     opts="--name --force --no-refresh" ;;
        remove)  opts="--no-refresh" ;;
        # No options; single positional is the lc repo path -> directory completion.
        refresh) COMPREPLY=( $(compgen -d -- "${cur}") ); return 0 ;;
        list|*)  return 0 ;;
    esac

    if [[ "${cur}" == -* ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
    else
        case "${prev}" in
            --name) : ;;
            *)      COMPREPLY=( $(compgen -d -- "${cur}") ) ;;
        esac
    fi
}

complete -F _lc lc 2>/dev/null || true
complete -F _lc_add_repo lc-add-repo 2>/dev/null || true
