#!/bin/sh
# elvis: codesearch	-- Search source code using Google Code Search (www.google.fr/codesearch)
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_codesearch_lang         any
def   SURFRAW_codesearch_package      ""
def   SURFRAW_codesearch_files        ""
def   SURFRAW_codesearch_license      any
def   SURFRAW_codesearch_class        ""
def   SURFRAW_codesearch_function     ""
defyn SURFRAW_codesearch_case         0
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words or regular expressions]...
Description:
  Surfraw search source code using Google Code Search (www.google.fr/codesearch)
Local options:
  -lang=LANG                    Limit search to programming language LANG
                                Options:
                                 any (default), actionscript, ada applescript,
                                 asp, assembly, autoconf automake, awk, basic,
                                 bat, c, c++, c#, caja, cobol, coldfusion,
                                 configure, css, d, eiffel, erlang, fortran, go,
                                 haskell, inform, java, javascript, jsp, lex,
                                 limbo, lisp, lolcode, lua, m4, makefile, maple,
                                 mathematica, matlab, messagecatalog, modula2,
                                 modula3, objectivec, ocaml, pascal, perl, php,
                                 pod, prolog, proto, python, r, rebol, ruby,
                                 sas, scheme, scilab, shell, sgml, smalltalk,
                                 sql, sml, svg, swig, tcl, tex, texinfo, troff,
                                 verilog, vhdl, vim, xslt, xul, yacc
  -package=REGEXP               Find code in packages matching REGEXP
  -files=REGEXP                 Find code in files matching REGEXP
  -license=LICENSE              Find code with the specified license
				Options:
                                 any (default),aladdin, artistic, apache, apple,
                                 bsd, cpl, epl, agpl, gpl, lgpl, disclaimer,
                                 ibm, lucent, mit, mozilla, nasa, python, qpl,
                                 sleepycat, zope
  -class=CLASS                  Find code in a class matching CLASS
  -function=FUNC 		Find code in a function matching FUNC
  -case                         Case Sensitive matching
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -pack*=*)  setopt   SURFRAW_codesearch_package       $optarg ;;
    -fi*=*)    setopt   SURFRAW_codesearch_files         $optarg ;;
    -ca*)      setoptyn SURFRAW_codesearch_case          1       ;;
    -lan*=*)   setopt   SURFRAW_codesearch_lang          $optarg ;;
    -li*=*)    setopt   SURFRAW_codesearch_license       $optarg ;;
    -cl*=*)    setopt   SURFRAW_codesearch_class         $optarg ;;
    -fu*=*)    setopt   SURFRAW_codesearch_function      $optarg ;;
    *) return 1 ;;
    esac
    return 0
}

add_if_set()
{
    if test -n "$1"; then
	echo "+$2:$1"
    fi
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments
if test -z "$w3_args"; then
    w3_browse_url "http://www.google.fr/codesearch"
else
    escaped_args=`w3_url_of_arg $w3_args`
    case "$SURFRAW_codesearch_lang" in
      any)        setopt SURFRAW_codesearch_lang ""           ;;
      cplusplus)  SURFRAW_codesearch_lang="%22c%2B%2B%22"     ;;
      csharp)     SURFRAW_codesearch_lang="c%23"     ;;
    esac

    if [ "$SURFRAW_codesearch_license" = "any" ]; then
	setopt SURFRAW_codesearch_license ""
    else
	setopt SURFRAW_codesearch_license "^${SURFRAW_codesearch_license}\$"
    fi

    query="${escaped_args}"
    query="${query}$(add_if_set "$SURFRAW_codesearch_package"   "package")"
    query="${query}$(add_if_set "$SURFRAW_codesearch_lang"      "lang")"
    query="${query}$(add_if_set "$SURFRAW_codesearch_file"      "file")"
    query="${query}$(add_if_set "$SURFRAW_codesearch_class"     "class")"
    query="${query}$(add_if_set "$SURFRAW_codesearch_function"  "function")"
    query="${query}$(add_if_set "$SURFRAW_codesearch_license"   "license")"
    if ifyes SURFRAW_codesearch_case; then
	query="${query} case:yes"
    fi

    url="http://www.google.fr/codesearch#search/&q=${query}&type=cs"
    w3_browse_url "$url"
fi
