svcs=$(ls /opt/opensvc/etc/*env 2>/dev/null|xargs -n1 basename 2>/dev/null|sed -e "s/.env//")
svcmgr_opts="--force --debug --waitlock --rid --tags --service"
nodemgr_opts="--force --debug --cron"

_svcmgr() 
{
    local a b c d prev
    COMPREPLY=()
    a="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
	--tags)
	    local tags=$(grep -h "tag" /opt/opensvc/etc/*env|cut -d= -f2)
	    COMPREPLY=( $(compgen -W "${tags}" -- ${a}) )
            return 0
            ;;
        --rid)
	    local rid=$(grep -h "\[.*\]" /opt/opensvc/etc/*env|sed -e "s/\[//"|sed -e "s/\]//")
	    COMPREPLY=( $(compgen -W "${rid}" -- ${a}) )
            return 0
            ;;
        --service)
	    COMPREPLY=( $(compgen -W "${svcs}" -- ${a}) )
            return 0
            ;;
        *)
        ;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
        local opts="configure create disklist freeze frozen mount postsync presync print_status print_resource_status print_service edit_service prstart prstatus prstop push restart start startapp startcontainer startdisk startip startloop startpool startstandby startvg status stop stopapp stopcontainer stopdisk stopip stoploop stoppool stopvg syncall syncbreak syncdrp syncfullsync syncnodes syncquiesce syncresync syncupdate syncverify thaw umount collector compliance"
    elif [ $COMP_CWORD -eq 2 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        case "${b}" in
            collector)
		local opts="json ack alerts checks events status list show asset networks disks"
		;;
	    compliance)
		local opts="check fixable fix show list attach detach env"
		;;
	    push)
		local opts="appinfo"
		;;
	    set)
		local opts="--param --value"
		;;
	    get|unset)
		local opts="--param"
		;;
	    resource)
		local opts="monitor"
		;;
	    print|json)
		local opts="disklist status resource"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 3 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        case "${c} ${b}" in
            "collector json")
		local opts="alerts checks events status list show asset networks disks"
		;;
            "collector ack")
		local opts="action"
		;;
            "collector list")
		local opts="actions"
		;;
            "collector show")
		local opts="actions"
		;;
            "collector disks")
		local opts="--table"
		;;
            "collector asset")
		local opts="--table"
		;;
            "collector networks")
		local opts="--table"
		;;
            "collector status")
		local opts="--table"
		;;
            "collector checks")
		local opts="--table"
		;;
            "collector events")
		local opts="--begin --end --table"
		;;
            "compliance list")
		local opts="ruleset moduleset"
		;;
            "compliance show")
		local opts="ruleset moduleset"
		;;
            "compliance fixable")
		local opts="--module --moduleset"
		;;
            "compliance fix")
		local opts="--module --moduleset --attach"
		;;
            "compliance check")
		local opts="--module --moduleset"
		;;
            "compliance env")
		local opts="--module --moduleset"
		;;
            "compliance attach")
		local opts="moduleset ruleset"
		;;
            "compliance detach")
		local opts="moduleset ruleset"
		;;
            "print resource")
		local opts="status"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 4 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        case "${d} ${c} ${b}" in
            "collector json list")
		local opts="actions"
		;;
            "collector json show")
		local opts="actions"
		;;
            "collector json events")
		local opts="--begin --end"
		;;
            "collector ack action")
		local opts="--comment --author --duration"
		;;
            "collector list actions")
		local opts="--begin --end --table"
		;;
            "collector show action")
		local opts="--id --begin --end --table"
		;;
            "compliance detach moduleset")
		local opts="--moduleset"
		;;
            "compliance detach ruleset")
		local opts="--ruleset"
		;;
            "compliance attach moduleset")
		local opts="--moduleset"
		;;
            "compliance attach ruleset")
		local opts="--ruleset"
		;;
            "compliance list moduleset")
		local opts="--moduleset"
		;;
            "compliance list ruleset")
		local opts="--ruleset"
		;;
            "print resource status")
		local opts="--rid"
		;;
        esac
    elif [ $COMP_CWORD -eq 5 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        e="${COMP_WORDS[COMP_CWORD-4]}"
        case "${e} ${d} ${c} ${b}" in
            "collector json ack action")
		local opts="--comment --author --duration"
		;;
            "collector json list actions")
		local opts="--begin --end"
		;;
            "collector json show action")
		local opts="--id --begin --end"
		;;
	esac
     fi

     COMPREPLY=($(compgen -W "${opts}" -- ${a}))  
     return 0
}

_nodemgr() 
{
    local a b c d prev
    COMPREPLY=()
    a="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
	--module)
	    local opts=$(ls /opt/opensvc/var/compliance/[0-9]* | xargs -n1 basename | sed "s/^[0-9]*[-]//")
	    COMPREPLY=( $(compgen -W "${opts}" -- ${a}) )
            return 0
            ;;
        *)
        ;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
        local opts="compliance checks collector pushasset pushnsr pusheva pushdcs pushvioserver pushnecism pushibmsvc pushhp3par pushibmds pushhds pushdisks pushbrocade pushsym pushservices syncservices pushstats pushpkg pushpatch push get set unset register reboot shutdown provision prkey updateservices updatepkg updatecomp scanscsi dequeue"
    elif [ $COMP_CWORD -eq 2 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        case "${b}" in
            dequeue)
		local opts="actions"
		;;
            collector)
		local opts="json ack alerts checks events status list show asset networks disks"
		;;
	    compliance)
		local opts="check fixable fix show list attach detach env"
		;;
	    push)
		local opts="appinfo"
		;;
	    set)
		local opts="--param --value"
		;;
	    get|unset)
		local opts="--param"
		;;
	    pushstats)
		local opts="--stats-dir --begin --end"
		;;
	    pushnsr|pusheva|pushdcs|pushvioserver|pushnecism|pushibmsvc|pushhp3par|pushibmds|pushhds|pushdisks|pushbrocade)
		local opts="--object"
		;;
	    pushsym)
		local opts="--symcli-db-file --object"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 3 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        case "${c} ${b}" in
            "collector json")
		local opts="alerts checks events status list show asset networks disks"
		;;
            "collector ack")
		local opts="action"
		;;
            "collector list")
		local opts="actions filtersets nodes services"
		;;
            "collector show")
		local opts="actions"
		;;
            "collector events")
		local opts="--begin --end --table"
		;;
            "collector checks")
		local opts="--table"
		;;
            "collector events")
		local opts="--table"
		;;
            "collector status")
		local opts="--table"
		;;
            "collector asset")
		local opts="--table"
		;;
            "collector networks")
		local opts="--table"
		;;
            "collector disks")
		local opts="--table"
		;;
            "compliance list")
		local opts="ruleset moduleset"
		;;
            "compliance show")
		local opts="ruleset moduleset"
		;;
            "compliance fixable")
		local opts="--module --moduleset"
		;;
            "compliance fix")
		local opts="--module --moduleset --attach"
		;;
            "compliance check")
		local opts="--module --moduleset"
		;;
            "compliance env")
		local opts="--module --moduleset"
		;;
            "compliance attach")
		local opts="moduleset ruleset"
		;;
            "compliance detach")
		local opts="moduleset ruleset"
		;;
            *)
                ;;
        esac
    elif [ $COMP_CWORD -eq 4 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        case "${d} ${c} ${b}" in
            "collector json list")
                local opts="actions"
                ;;
            "collector json show")
                local opts="actions"
                ;;
            "collector json events")
                local opts="--begin --end"
                ;;
            "collector ack action")
		local opts="--comment --author --duration"
		;;
            "collector list actions")
		local opts="--begin --end"
		;;
            "collector list nodes")
		local opts="--filterset"
		;;
            "collector list services")
		local opts="--filterset"
		;;
            "collector show action")
		local opts="--id --begin --end"
		;;
            "compliance detach moduleset")
		local opts="--moduleset"
		;;
            "compliance detach ruleset")
		local opts="--ruleset"
		;;
            "compliance attach moduleset")
		local opts="--moduleset"
		;;
            "compliance attach ruleset")
		local opts="--ruleset"
		;;
            "compliance list moduleset")
		local opts="--moduleset"
		;;
            "compliance list ruleset")
		local opts="--ruleset"
		;;
        esac
    elif [ $COMP_CWORD -eq 5 ]; then
        b="${COMP_WORDS[COMP_CWORD-1]}"
        c="${COMP_WORDS[COMP_CWORD-2]}"
        d="${COMP_WORDS[COMP_CWORD-3]}"
        e="${COMP_WORDS[COMP_CWORD-4]}"
        case "${e} ${d} ${c} ${b}" in
            "collector json ack action")
                local opts="--comment --author --duration"
                ;;
            "collector json list actions")
                local opts="--begin --end"
                ;;
            "collector json show action")
                local opts="--id --begin --end"
                ;;
        esac
    fi

    COMPREPLY=($(compgen -W "${opts}" -- ${a}))  
    return 0
}

opts=$svcmgr_opts
complete -F _svcmgr svcmgr

opts=$svcmgr_opts
for s in ${svcs} ; do
    complete -F _svcmgr $s
done

opts=$nodemgr_opts
complete -F _nodemgr nodemgr

