_smc()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="device linkgroup info stats ueid -a -d -dd -v"
    opts_smcd="device linkgroup info stats ueid seid -a -d -v"
    opts_short="device linkgroup"
    opts_show="show link-show"
    opts_show_smcd="show"
    opts_stats="show reset"
    opts_ueid="show add del flush"
    opts_seid="show enable disable"
    opts_type="smcd smcr"
    opts_final="all netdev ibdev"
    opts_final_smcd="all"
    case "${prev}" in
        -v)
            if [ $1 = "smcr" ]; then
                COMPREPLY=( $(compgen -W "${opts_short}" -- ${cur}))
            fi
            return 0
            ;;
        -vv)
            if [ $1 = "smcr" ]; then
                COMPREPLY=( $(compgen -W "${opts_short}" -- ${cur}))
            fi
            return 0
            ;;
        device)
            if [ $1 = "smcr" ]; then
                COMPREPLY=( $(compgen -W "${opts_show}" -- ${cur}))
            else
                COMPREPLY=( $(compgen -W "${opts_show_smcd}" -- ${cur}))
            fi
            return 0
            ;;
        linkgroup)
            if [ $1 = "smcr" ]; then
                COMPREPLY=( $(compgen -W "${opts_show}" -- ${cur}) )
            else
                COMPREPLY=( $(compgen -W "${opts_show_smcd}" -- ${cur}) )
            fi
            return 0
            ;;
        info)
            COMPREPLY=( $(compgen -W "${opts_show_smcd}" -- ${cur}) )
            return 0
            ;; 
        show)
            if [ $1 = "smcr" ]; then
                COMPREPLY=( $(compgen -W "${opts_final}" -- ${cur}) )
            else
                COMPREPLY=( $(compgen -W "${opts_final_smcd}" -- ${cur}) )
            fi
            return 0
            ;;
        link-show)
            COMPREPLY=( $(compgen -W "${opts_final}" -- ${cur}) )
            return 0
            ;;
        smcd)
            COMPREPLY=( $(compgen -W "${opts_smcd}" -- ${cur}) )
            return 0
            ;;
        smcr)
            COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
            return 0
            ;;
        stats)
            COMPREPLY=( $(compgen -W "${opts_stats}" -- ${cur}) )
            return 0
            ;;
        ueid)
            COMPREPLY=( $(compgen -W "${opts_ueid}" -- ${cur}) )
            return 0
            ;;
        seid)
            COMPREPLY=( $(compgen -W "${opts_seid}" -- ${cur}) )
            return 0
            ;;
        *)
        ;;
    esac
}

function _smc_pnet_complete_() {
    case "${COMP_WORDS[COMP_CWORD-1]}" in
        --interface*|-I*)
            COMPREPLY=($(compgen -W "$(ls -1 /sys/class/net/)" -- "${COMP_WORDS[COMP_CWORD]}"))
            return;;
        --ibdevice*|-D*)
            which smc_rnics >/dev/null
            if [ $?  -eq 0 ]; then
                COMPREPLY=($(compgen -W "$(smc_rnics | tail -n +3 | awk '{print($3)}' | uniq)" -- "${COMP_WORDS[COMP_CWORD]}"))
            else
                COMPREPLY=($(compgen -W "$(ls -1 /sys/bus/pci/devices)" -- "${COMP_WORDS[COMP_CWORD]}"))
            fi
            return;;
        --ibport*|-P*)
            ;;
    esac

    COMPREPLY=($(compgen -W "--help --version --add --delete --show --flush --interface --ibdevice --ibport" -- "${COMP_WORDS[COMP_CWORD]}"))
}

function _smc_rnics_complete_() {
    case "${COMP_WORDS[COMP_CWORD-1]}" in
        --enable|-e)
            COMPREPLY=($(compgen -W "$(smc_rnics | grep -e "^ [[:space:]0-9a-f]\{2\}  0" | awk '{print($1)}')" -- "${COMP_WORDS[COMP_CWORD]}"))
            return;;
        --disable|-d)
            COMPREPLY=($(compgen -W "$(smc_rnics | grep -e "^ [[:space:]0-9a-f]\{2\}  1" | awk '{print($1)}')" -- "${COMP_WORDS[COMP_CWORD]}"))
            return;;
    esac

    COMPREPLY=($(compgen -W "--help --version --disable --enable --rawids" -- "${COMP_WORDS[COMP_CWORD]}"))
}

function _smc_chk_complete_() {
    case "${COMP_WORDS[COMP_CWORD-1]}" in
        --pnetid|-i)
            COMPREPLY=($(compgen -W "$(ip link show | grep -e "^[0-9]\+:" | awk '{print($2)}' | sed s'/:$//') $(ip link show up | grep -e "^\s*altname" | awk '{print($2)}')" -- "${COMP_WORDS[COMP_CWORD]}"))
            return;;
        --connect|-C|--port|-p)
            COMPREPLY=()
            return;;
    esac

    COMPREPLY=($(compgen -W "$(ip link show up | grep -e "^[0-9]\+:" | awk '{print($2)}' | sed s'/:$//') $(ip link show up | grep -e "^\s*altname" | awk '{print($2)}') --connect --help --version --debug --pnetid --port --server --static-analysis --live-test --ipv6" -- "${COMP_WORDS[COMP_CWORD]}"))
}

complete -W "--help --tgz --version" smc_dbg
complete -W "--help --version --all --listening --debug --wide --smcd --smcr" smcss
complete -F _smc smcd
complete -F _smc smcr
complete -F _smc_pnet_complete_ smc_pnet
complete -F _smc_rnics_complete_ smc_rnics
complete -F _smc_chk_complete_ smc_chk
