# Bakefile completion
# 
# $Id$
#

_bakefile_list_formats() {
    local all_formats
    all_formats=`bakefile --help | awk '/available formats are/,EOF {split($0,a);print a[1]}' | tail -n +2`
    COMPREPLY=( $(compgen -W "$all_formats" -- $1 ) )
}

_bakefile() {
    local cur prev all_cmds
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    COMPREPLY=()

    case "$prev" in
        -f|--format)
            _bakefile_list_formats $cur
            ;;
        --eol)
            COMPREPLY=( $(compgen -W "format dos unix mac native" -- $cur ) )
            ;;
        --wrap-output)
            # allow anything here
            ;;
        -D)
            # allow anything here
            ;;
        *)
            case "$cur" in
                -*) 
                    all_cmds="--version -h --help -f --format -o --output -D \
                              -I -v --verbose -q --quiet --dry-run --touch \
                              --eol --wrap-output \
                              --output-deps --output-changes --xml-cache \
                              --debug --dump"
                    COMPREPLY=( $(compgen -W "$all_cmds" -- $cur ) )
                    ;;
                *)
                    _filedir 'bkl'
                    ;;
                    
            esac
            ;;
    esac

    return 0
}

complete -F _bakefile -o filenames bakefile


_bakefile_gen() {
    local cur prev all_cmds
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    COMPREPLY=()

    case "$prev" in
        -f|--formats)
            _bakefile_list_formats $cur
            ;;
        -b|--bakefiles)
            _filedir 'bkl'
            ;;
        -j|--jobs|-D)
            # allow anything here
            ;;
        *)
            case "$cur" in
                -*) 
                    all_cmds="-h --help -f --formats -b --bakefiles \
                              -D -c --clean -j --jobs -p --pretend --dry-run \
                              -k --keep-going -B --always-make \
                              --list-files \
                              -v --verbose -V --very-verbose"
                    COMPREPLY=( $(compgen -W "$all_cmds" -- $cur ) )
                    ;;
                *)
                    # nothing here - bakefile_gen doesn't take filenames
                    ;;
                    
            esac
            ;;
    esac

    return 0
}

complete -F _bakefile_gen bakefile_gen
