#
# Provided by Eric NOULARD - eric.noulard@gmail.com
#
# We will try to complete cmake (http://www.cmake.org)
# command options at 2 (or may be 3 levels)
#  cmake <level0> <level1> <level2>
#
#  level0 is top level cmake options
#  level1 is the first argument of level0 option
#  level2 is the seconf argument of level1 argument
#   FIXME: I don't know how to handle level2 
#
_cmake() 
{
    local cur prev opts words cword
    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    # cmake command line option we want to complete
    opts=`cmake --help | grep "^  \-.*=\ .*" | cut -d" " -f 3 | cut -d= -f 1 | cut -d[ -f 1`  

    #
    #  Complete the arguments to some of 
    #  the most commonly used commands (Level 1).
    #
    case "${prev}" in
        -E)
	    local running=$(for x in `cmake -E |&  grep "^  " | cut -d" " -f 3`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
	--help-command|--help-commands)
	    local running=$(for x in `cmake --help-command-list`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;	    
	--help-module|--help-modules)
	    local running=$(for x in `cmake --help-module-list`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;; 
         --help-policy)
	    local running=$(for x in `cmake --help-policies | grep "^  CMP"`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;; 
         --help-property|--help-properties)
	    local running=$(for x in `cmake --help-property-list`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
         --help-variable|--help-variables)
	    local running=$(for x in `cmake --help-variable-list`; do echo ${x} ; done )
	    COMPREPLY=( $(compgen -W "${running}" -- ${cur}) )
            return 0
            ;;
        *)
            ;;
    esac        

    #
    #  Complete the arguments to some of 
    #  the most commonly used commands (Level 2).
    #   ?? How to do that ..

    #
    # Complete the option (Level 0 - right after cmake)
    # 
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
}
complete -F _cmake cmake

