#!/bin/sh
# elvis: opensearch	-- Search an OpenSearch-enabled website
# ianb@erislabs.net 20060903

. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_opensearch_title          ""
def   SURFRAW_opensearch_count          $SURFRAW_results
def   SURFRAW_opensearch_startindex     ""
def   SURFRAW_opensearch_pageindex      ""
def   SURFRAW_opensearch_language       "$SURFRAW_lang"
def   SURFRAW_opensearch_inputencoding  ""
def   SURFRAW_opensearch_outputencoding ""
defyn SURFRAW_opensearch_list           no
defyn SURFRAW_opensearch_redirect       no
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [url] [search words]...
Description:
  Surfraw search an OpenSearch-enabled website.
  If opensearch-discover and opensearch-genquery are installed then
  it will handle the opensearch lookups locally, otherwise it will fall
  back on a redirector.
Local options:
  -r                            Explicitly use redirector.
  -l                            List OpenSearch searches at url.
  -title=TITLE                  Use search with title matching TITLE.
                                TITLE is case-insensitive.
                                Example: -t book matches "Book Search"
Many sites do not implement all (or any) of the following
parameters, in which case they will be silently ignored.
  -count=NUM                    Number of search results returned.
                                Default: $SURFRAW_opensearch_count.
                                Environment: SURFRAW_opensearch_count.
                                If not set, defaults to value of SURFRAW_results
  -index=NUM                    Offset of first result.
  -page=NUM                     Which page of results to show.
  -language=LANG                Language to display results in.
                                LANG should be a 2-letter ISO country code.
                                Examples: -l=en or -lang=de.
				Environment: SURFRAW_opensearch_lang, SURFRAW_lang
  -inputencoding=ENC            Specify how search terms are encoded.
  -outputencoding=ENC           Request output encoded as ENC.
                                Environment: SURFRAW_opensearch_search
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -t*=*)  setopt   SURFRAW_opensearch_title          "$optarg" ;;
    -c*=*)  setopt   SURFRAW_opensearch_count          "$optarg" ;;
    -i*=*)  setopt   SURFRAW_opensearch_startindex     "$optarg" ;;
    -p*=*)  setopt   SURFRAW_opensearch_pageindex      "$optarg" ;;
    -la*=*) setopt   SURFRAW_opensearch_language       "$optarg" ;;
    -in*=*) setopt   SURFRAW_opensearch_inputencoding  "$optarg" ;;
    -o*=*)  setopt   SURFRAW_opensearch_outputencoding "$optarg" ;;
    -l)     setoptyn SURFRAW_opensearch_list            1        ;;
    -r)     setoptyn SURFRAW_opensearch_redirect        yes      ;;
	*) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
if test -z "$w3_args"; then
	w3_usage_hook
	exit 0
fi

if  ifno SURFRAW_opensearch_redirect      &&
    [ ! -x $(which opensearch-discover) ] ||
    [ ! -x $(which opensearch-genquery) ]
then
    setoptyn SURFRAW_opensearch_redirect yes
fi

# Clobbers command line, but we've already parsed it
set $w3_args
siteurl="$1"
shift
w3_args="$*"
# w3_args now contains a list of arguments

if ifyes SURFRAW_opensearch_redirect; then
    if ifyes SURFRAW_opensearch_list; then
	err "-l not supported with -r"
    elif test -z "$w3_args"; then
	w3_browse_url "$siteurl"
    else
	escaped_args=`w3_url_of_arg $w3_args`

	if [ -n "$SURFRAW_opensearch_title" ]; then
	    err "-t not supported with -r"
	fi

	url="http://erislabs.net/ianb/webtools/opensearch/"
	url="${url}?site=$(w3_url_of_arg "$siteurl")"
	url="${url}&searchTerms=${escaped_args}"
	url="${url}&count=${SURFRAW_opensearch_count}"
	url="${url}&startIndex=${SURFRAW_opensearch_startindex}"
	url="${url}&pageIndex=${SURFRAW_opensearch_pageindex}"
	url="${url}&language=${SURFRAW_opensearch_language}"
	url="${url}&inputEncoding=${SURFRAW_opensearch_inputencoding}"
	url="${url}&outputEncoding=${SURFRAW_opensearch_outputencoding}"
	url="${url}&fmt=html"

	w3_browse_url "$url"
    fi

else # dont use redirector

    if ifyes SURFRAW_opensearch_list; then
	opensearch-discover -q -v -a $siteurl
	case "$?" in
	    0) ;; # OK
	    1) err "Cannot access $siteurl" ;;
	    2) err "Cannot find ${matching}OpenSearch link at $siteurl"  ;;
	    3) err "opensearch-discover: required perl module not found"  ;;
	    *) err "Unknown error from opensearch-discover" ;;
	esac

    elif test -z "$w3_args"; then
	w3_browse_url "$siteurl"
    else
	escaped_args=`w3_url_of_arg $w3_args`

	discover_args="-q"
	matching="" # for error message below
	if [ -n "$SURFRAW_opensearch_title" ]; then
		discover_args="-q -t $SURFRAW_opensearch_title"
		matching="matching "
	fi

	opensearchurl=`opensearch-discover $discover_args $siteurl`

	case "$?" in
		0) ;; # OK
		1) err "Cannot access $siteurl" ;;
		2) err "Cannot find ${matching}OpenSearch link at $siteurl"  ;;
		3) err "opensearch-discover: required perl module not found"  ;;
		*) err "Unknown error from opensearch-discover" ;;
	esac

	queryargs="-q -H"

	if [ -n "$SURFRAW_opensearch_count" ]; then
		queryargs="$queryargs -c $SURFRAW_opensearch_count"
	fi

	if [ -n "$SURFRAW_opensearch_startindex" ]; then
		queryargs="$queryargs -i $SURFRAW_opensearch_startindex"
	fi

	if [ -n "$SURFRAW_opensearch_pageindex" ]; then
		queryargs="$queryargs -p $SURFRAW_opensearch_pageindex"
	fi

	if [ -n "$SURFRAW_opensearch_language" ]; then
		queryargs="$queryargs -l $SURFRAW_opensearch_language"
	fi

	if [ -n "$SURFRAW_opensearch_inputencoding" ]; then
		queryargs="$queryargs -I $SURFRAW_opensearch_inputencoding"
	fi

	if [ -n "$SURFRAW_opensearch_outputencoding" ]; then
		queryargs="$queryargs -O $SURFRAW_opensearch_outputencoding"
	fi

	query=`opensearch-genquery $queryargs $opensearchurl $w3_args`

	case "$?" in
		0) ;; # OK
		1) err "Cannot access OpenSearch description $opensearchurl" ;;
		2) err "Error parsing OpenSearch Description at $opensearchurl"  ;;
		3) err "No HTML search specified in $opensearchurl" ;;
		4) err "opensearch-genquery: cannot find required perl module WWW::OpenSearch" ;;
		*) err "Unknown error from opensearch-genquery" ;;
	esac

	w3_browse_url "$query"

    fi
fi
