#!/bin/sh
# elvis: scroogle	-- Search Google anonymously via Scroogle (www.scroogle.org)
# ianb@erislabs.net 20100416
. surfraw || exit 1

w3_config_hook () {
    def   SURFRAW_scroogle_results      20
    defyn SURFRAW_scroogle_ssl          yes
    def   SURFRAW_scroogle_lang         "$SURFRAW_lang"
    # number in URL of language pages
    def   SURFRAW_scroogle_langpagever  8
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Search Google anonymously using Scroogle (www.scroogle.org)
Local options:
  -nossl                        Don't use https
  -results=NUM                  Number of search results returned
                                Default: $SURFRAW_scroogle_results
                                Environment: SURFRAW_scroogle_results
  -lang=                        Search for pages written in LANG.
         al             |       any language (default)
         ar             |       Arabic
         zs             |       Chinese (simplified)
         zt             |       Chinese (traditional)
         cs             |       Czech
         da             |       Danish
         nl             |       Dutch
         en             |       English
         et             |       Estonian
         fi             |       Finnish
         fr             |       French
         de             |       German
         el             |       Greek
         iw             |       Hebrew
         hu             |       Hungarian
         is             |       Icelandic
         it             |       Italian
         ja             |       Japanese
         ko             |       Korean
         lv             |       Latvian
         lt             |       Lithuanian
         no             |       Norwegian
         pt             |       Portuguese
         pl             |       Polish
         ro             |       Romanian
         ru             |       Russian
         es             |       Spanish
         sv             |       Swedish
         tr             |       Turkish
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -results=*) setopt   SURFRAW_scroogle_results "$optarg" ;;
    -lang=*)    setopt   SURFRAW_scroogle_lang    "$optarg" ;;
    -no*)       setoptyn SURFRAW_scroogle_ssl     no         ;;
    *) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments

if ifno SURFRAW_scroogle_ssl; then
    base="http://www.scroogle.org"
    path="cgi-bin/nbbw.cgi"
else
    base="https://ssl.scroogle.org"
    path="cgi-bin/nbbwssl.cgi"
fi

case "$SURFRAW_scroogle_lang" in
    al|ar|zs|zt|cs|da|nl|en|et|fi|fr|de|el|iw|hu|is|it|ja|ko|lv|lt|no|\
    pt|pl|ro|ru|es|sv|tr) lang="${SURFRAW_scroogle_lang}" ;;
    *) lang="";
esac

if test -z "$w3_args"; then
# urls of search page:
#   http://www.scroogle.org/cgi-bin/scraper.htm
#   http://www.scroogle.org/scrap${SURFRAW_scroogle_lang}${SURFRAW_scroogle_langpagever}.html
#   https://ssl.scroogle.org/
#   https://ssl.scroogle.org/scrap${SURFRAW_scroogle_lang}${SURFRAW_scroogle_langpagever}.html
    url="${base}"
    if ifno SURFRAW_scroogle_ssl; then
	file="cgi-bin/scraper.htm"
    else
	file=""
    fi
    if test -n "$lang";then
	url="${url}/scrap${lang}${SURFRAW_scroogle_langpagever}.html"
    else
	url="${url}/${file}"
    fi
else
    case "$SURFRAW_scroogle_results" in
	50)  results=5 ;;
	100) results=1 ;;
	*)   results=2 ;; # default = 2 == 20 results
    esac

    urlopts="n=${results}"

    if test -n "$lang";then
	urlopts="${urlopts}&l=${lang}"
    fi

    escaped_args=`w3_url_of_arg $w3_args`
    # Gw must come first
    url="${base}/${path}?Gw=${escaped_args}&${urlopts}"
fi

w3_browse_url "$url"

