# wget(1) completion
# Copyright 2009 Droz Raphaël <gibboris@gmail.com>
# License: GNU GPL v2 or later
#
# $Id: wget,v 0.3 2009/03/27 14:25:40 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 "{0..9}" -- $cur ) )
	    return 0
	    ;;
	--quota)
	    COMPREPLY=( $( compgen -W "{0..9}{k,m}" -- $cur ) )
	    return 0
	    ;;
	--user|--http-user|--proxy-user|--ftp-user)
	    COMPREPLY=( $( compgen -W "$(sed -n '/^login/s/^ *login //p' .netrc)" -- $cur ) )
	    return 0
	    ;;
	--level)
	    COMPREPLY=( $( compgen -W "{1.. 5}" -- $cur ) )
            return 0
            ;;
	--header)
	    COMPREPLY=( $( compgen -W 'Accept Accept-Charset Accept-Encoding Accept-Language \
	     		Accept-Ranges Age Allow Authorization Cache-Control Connection Content-Encoding \
	     		Content-Language Content-Length Content-Location Content-MD5 Content-Range \
	     		Content-Type Date ETag Expect Expires From Host If-Match If-Modified-Since \
	     		If-None-Match If-Range If-Unmodified-Since Last-Modified Location Max-Forwards \
	     		Pragma Proxy-Authenticate Proxy-Authorization Range Referer Retry-After \
	     		Server TE Trailer Transfer-Encoding Upgrade User-Agent Vary Via Warning \
	     		WWW-Authenticate' -- $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
	    ;;
	*)
	    #cur=${cur/:/\\\\:}

	    #cur=${cur/\\:/:}
	    #cur=${cur/\\\//\/}
	    #COMPREPLY=( $( compgen -W "http{,s}\:\/\/{,www} ftp\:\/\/" -- $cur ) )
	    
	    #COMPREPLY=( $( compgen -W "http{,s}\\\\:\\\\/\\\\/{,www} ftp\\\\:\\\\/\\\\/" -- $cur ) )
	    
	    COMPREPLY=( $( compgen -W "http{,s}://{,www} ftp://" -- $cur ) )
            return 0
            ;;
    esac
    return 0
}
#complete -F _wget -o default wget
complete -F _wget -o nospace wget
