# wget(1) completion
# Copyright 2009 Droz Raphaël <gibboris@gmail.com>
# License: GNU GPL v2 or later
#
# $Id: wget,v 0.2 2009/03/20 13:18:21 r4ph Exp $

_wget()
{
    local cur prev

    COMPREPLY=()
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}
    
    # If `cur' is long option containing `=', reassign prev and cur
    if [[ "$cur" == --*=* ]]; then
	prev=${cur/=*}
	cur=${cur/*=}
    fi

    case "$prev" in
	--progress)
	    COMPREPLY=( $( compgen -W 'bar dot' -- $cur ) )
	    return 0
	    ;;
	--bind-address|--exclude-domains|--domains|-D)
	    _known_hosts
	    return 0
	    ;;
	--restrict-file-names)
	    COMPREPLY=( $( compgen -W 'unix windows' -- $cur ) )
	    #todo : ,nocontrol
	    return 0
	    ;;
	--prefer-family)
	    COMPREPLY=( $( compgen -W 'IPv4 IPv6 none' -- $cur ) )
            return 0
            ;;
        --directory-prefix|--ca-directory|-P)
            _filedir -d
            return 0
            ;;
        --load-cookies|--save-cookies|--post-file|--certificate|--ca-certificate|--private-key|--random-file|--egd-file|--append-output|-a)
	    _filedir
	    return 0
	    ;;
	--input-file|-i)
	    _filedir && COMPREPLY=( "${COMPREPLY[@]}" - )
	    return 0
	    ;;
	--secure-protocol)
	    COMPREPLY=( $( compgen -W 'auto SSLv2 SSLv3 TLSv1' -- $cur ) )
	    return 0
	    ;;
	--certificate-type|--private-key-type)
	    COMPREPLY=( $( compgen -W 'PEM DER' -- $cur ) )
	    return 0
	    ;;
	--follow-tags|--ignore-tags)
	    COMPREPLY=( $( compgen -W 'a abbr acronym address applet area b base basefont bdo big blockquote \
	     		body br button caption center cite code col colgroup dd del dir div dfn dl dt em fieldset \
	     		font form frame frameset h6 head hr html i iframe img input ins isindex kbd label legend \
	     		li link map menu meta noframes noscript object ol optgroup option p param pre q s samp \
	     		script select small span strike strong style sub sup table tbody td textarea tfoot th \
	     		thead title tr tt u ul var xmp' -- $cur ) )
	    return 0
	    ;;
	--tries|--timeout|--dns-timeout|--connect-timeout|--limit-rate|--wait|--waitretry|--cut-dirs|--max-redirect)
	    COMPREPLY=( $( compgen -W "$(seq 0 1 9)" -- $cur ) )
	    return 0
	    ;;
	#--quota 0-9*[km]
	--user|--http-user|--proxy-user|--ftp-user)
	    COMPREPLY=( $( compgen -W "$(sed -n '/^login/s/^ *login //p' .netrc)" -- $cur ) )
	    return 0
	    ;;
	--level)
	    COMPREPLY=( $( compgen -W "$(seq 1 1 5)" -- $cur ) )
            return 0
            ;;
	--header)
	    COMPREPLY=( $( compgen -W "bloup popo" -- $cur ) )
	    return 0
	    ;;
    esac

    case "$cur" in
	--*)
            _longopt wget
	    return 0
	    ;;
	-*)
	    COMPREPLY=( $( compgen -W "$(wget -h | sed -n 's/^[ [:blank:]]*\(-[a-zA-A46]\).*$/\1/p')" -- $cur ) )
	    return 0
	    ;;
	h)
	    COMPREPLY=( $( compgen -o nospace -W 'http' -- $cur ) )
	    return 0
	    ;;
	f)
	    COMPREPLY=( $( compgen -o nospace -W 'ftp://' -- $cur ) )
	    return 0
	    ;;
	http)
	    COMPREPLY=( $( compgen -W 'http:// https://' -- $cur ) )
	    return 0
	    ;;
	https\?://w)
	    COMPREPLY=( $( compgen -W "${cur}ww." -- $cur ) )
	    return 0
	    ;;
    esac
    return 0
}
complete -F _wget -o default wget
