#!bash

_collie_cluster_format()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-c --copies" \
                -- ${cur} ))
            ;;
    esac
}

_collie_cluster_recover()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-f --force" \
                -- ${cur} ))
            ;;
    esac
}

_collie_vdi_create()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-P --prealloc" \
                -- ${cur} ))
            ;;
    esac
}

_collie_vdi_snapshot()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-s --snapshot" \
                -- ${cur} ))
            ;;
    esac
}

_collie_vdi_clone()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-P --prealloc -s --snapshot" \
                -- ${cur} ))
            ;;
    esac
}

_collie_vdi_read()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-s --snapshot" \
                -- ${cur} ))
            ;;
    esac
}

_collie_vdi_delete()
{
    local cur collie vdilist
    cur="${COMP_WORDS[COMP_CWORD]}"
    collie="${COMP_WORDS[0]}"
    vdilist="$(${collie} vdi list | tail -n+3 | grep '^  ' | awk '{print $1}')"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-s --snapshot" \
                -- ${cur} ))
            ;;
        *)
            COMPREPLY=($( compgen -W "${vdilist}" -- ${cur} ))
            ;;
    esac
}

_collie_vdi_object()
{
    local cur collie vdilist
    cur="${COMP_WORDS[COMP_CWORD]}"
    collie="${COMP_WORDS[0]}"
    vdilist="$(${collie} vdi list | tail -n+3 | grep '^  ' | awk '{print $1}')"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-i --index -s --snapshot" \
                -- ${cur} ))
            ;;
        *)
            COMPREPLY=($( compgen -W "${vdilist}" -- ${cur} ))
            ;;
    esac
}

_collie_vdi_setattr()
{
    local cur
    cur="${COMP_WORDS[COMP_CWORD]}"

    case "$cur" in
        -*)
            COMPREPLY=(${COMPREPLY[@]} \
                $( compgen \
                -W "-d --delete -x --exclusive" \
                -- ${cur} ))
            ;;
    esac
}

_collie_cluster()
{
    local opts
    opts="info format shutdown recover snapshot"

    case "$1" in
        info)
            ;;
        format)
            _collie_cluster_format
            ;;
        shutdown)
            ;;
        recover)
            _collie_cluster_recover
            ;;
        snapshot)
            ;;
        "")
            COMPREPLY=($( compgen \
                -W "${opts}" \
                -- "${COMP_WORDS[COMP_CWORD]}" ))
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

_collie_node()
{
    local opts
    opts="info list recovery kill cache"

    case "$1" in
        info)
            ;;
        list)
            ;;
        recovery)
            ;;
        kill)
            ;;
        cache)
            ;;
        "")
            COMPREPLY=($( compgen \
                -W "${opts}" \
                -- "${COMP_WORDS[COMP_CWORD]}" ))
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

_collie_vdi()
{
    local opts
    opts="create snapshot clone resize read write \
list tree graph delete object setattr getattr track check \
rollback flush backup restore"

    case "$1" in
	create)
	    _collie_vdi_create
	    ;;
	snapshot)
	    _collie_vdi_snapshot
	    ;;
	clone)
	    _collie_vdi_clone
	    ;;
	resize)
	    ;;
	read)
	    _collie_vdi_read
	    ;;
	write)
	    ;;
        list)
            ;;
        tree)
            ;;
        graph)
            ;;
        delete)
            _collie_vdi_delete
            ;;
        object)
            _collie_vdi_object
            ;;
        setattr)
            _collie_vdi_setattr
            ;;
        getattr)
            ;;
        track)
            ;;
        check)
            ;;
	rollback)
	    ;;
	flush)
	    ;;
	backup)
	    ;;
	restore)
	    ;;
        "")
            COMPREPLY=($( compgen \
                -W "${opts}" \
                -- "${COMP_WORDS[COMP_CWORD]}" ))
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

_collie()
{
    local opts cur cmd subcmd i
    opts="cluster node vdi"
    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ $COMP_CWORD -gt 1 ]; then
        cmd=${COMP_WORDS[1]}
    fi

    if [ $COMP_CWORD -gt 2 ]; then
        subcmd=${COMP_WORDS[2]}
    fi

    COMPREPLY=($( compgen -W "-a --address -p --port -h --help" -- ${cur} ))

    case "${cmd}" in
        cluster)
            _collie_cluster ${subcmd}
            ;;
        node)
            _collie_node ${subcmd}
            ;;
        vdi)
            _collie_vdi ${subcmd}
            ;;
        "")
            COMPREPLY=($( compgen -W "${opts}" -- ${cur} ))
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}

complete -F _collie collie
