#-*- mode: shell-script;-*-

# Debian GNU/Linux make-kpkg(1) completion.
# Copyright 2002 "Dr. Rafael Sepulveda" <drs@gnulinux.org.mx>
#
have make-kpkg &&
_make_kpkg()
{
    local cur prev options paroptions special i
    
    #check if we are in a kernel directory.
    [[ -d "drivers" && -d "kernel" && -d "fs" && -d "include/linux" ]] || \
	return 0

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
    options="--added_modules --added-modules --added_patches --added-patches \
             --append_to_version --append-to-version --arch --arch_in_name \
             --arch-in-name binary binary-arch binary-indep build \
             buildpackage --bzimage clean --config configure --cross_compile \
             --cross-compile debian --help --initrd -j --jobs --overlay-dir \
             kernel_debug kernel_doc kernel_headers kernel_manual kernel_image \
             kernel_source linux_doc linux_headers linux_image linux_source \
             libc-kheaders modules modules_clean modules_config modules_image \
             --noexec --pgpsign --stem --revision --rootcmd --subarch \
             --targets --uc --us --verbose --zimage"

    paroptions="$options"


    [[ $prev == '--config' ]] && \
	paroptions='config oldconfig silentoldconfig menuconfig xconfig'

    #exclude some options
    [[ "$DEBIAN_REVISION_MANDATORY" ]] ||\
	{ [[ -a "stamp-configure" ]] && paroptions=${paroptions/--revision}; }
    [[ "$ARCH_IN_NAME" ]] && paroptions=${paroptions//--arch[-_]in[-_]name}
    [[ "$CROSS_COMPILE" ]] && paroptions=${paroptions//--cross[-_]compile}
    [[ "$KPKG_ARCH" ]] && paroptions=${paroptions/--arch}
    [[ "$KPKG_SUBARCH" ]] && paroptions=${paroptions/--subarch}
    [[ "$KPKG_OVERLAY_DIR" ]] && paroptions=${paroptions/--overlay-dir}
    [[ $(grep CONFIG_BLK_DEV_INITRD .config 2>/dev/null \
        | cut -d= -f2) == "y" ]] && \
	[[ "$INITRD" == "YES" ]] && paroptions=${paroptions/--initrd}

    for (( i=0; i < ${#COMP_WORDS}-1; i++ )); do
	#we need to start and end every line with '|' so it can be recognized.
	if [[ ${COMP_WORDS[i]} == @(binary|binary-arch|binary-indep|build|\
        |buildpackage|clean|debian|kernel_debug|kernel_doc|kernel_headers|\
        |kernel_manual|kernel_image|kernel_source|linux_doc|linux_headers|\
        |linux_image|linux_source|libc-kheaders|modules|modules_clean|\
            |modules_config|modules_image) ]]; then
	    paroptions=$( echo $options | sed s/--[^\ ]*//g )
	    COMPREPLY=( $( compgen -W "$paroptions" | grep ^$cur ) )
	    return 0;
	fi
    done

    COMPREPLY=( $( compgen -W "$paroptions" | grep ^$cur ) )

    return 0

}
[ "$have" ] && complete -F _make_kpkg -o filenames make-kpkg
