#!/bin/sh
# elvis: wikipedia	-- Search the free encyclopedia wikipedia
# $Id$
# Author: Moritz Muehlenhoff <jmm@informatik.uni-bremen.de>
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_wikipedia_language	"$SURFRAW_lang"
# default to off until we know the ssl site is going to stick around
defyn SURFRAW_wikipedia_ssl		no
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search-string]
Description:
  Search the Wikipedia online encyclopedia
Local options:
  -s				  Uses experimental HTTPS secure connection.
                                  Environment: SURFRAW_wikipedia_ssl
  -language=ISOCODE | -l=ISOCODE  Two letter language code (resembles ISO country codes)
				  Default: en
				  Environment: SURFRAW_wikipedia_language, SURFRAW_lang

Examples:
  $w3_argv0 -language=de Richard Stallman
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
	-language=*)	setopt	SURFRAW_wikipedia_language		$optarg	;;
	-l=*)	setopt	SURFRAW_wikipedia_language		$optarg	;;
	-s*)   setoptyn SURFRAW_wikipedia_ssl			yes ;;
	*) return 1 ;;
    esac
    return 0
}

w3_config
# disable requoting, doesn't work with this elvi.
SURFRAW_quote_ifs=0
w3_parse_args "$@"

if [ -z "$SURFRAW_wikipedia_language" ]; then
    SURFRAW_wikipedia_language="en"
fi

prefix="http://${SURFRAW_wikipedia_language}.wikipedia.org/w/wiki.phtml?search="
prefixs="https://secure.wikimedia.org/wikipedia/${SURFRAW_wikipedia_language}/w/index.php?search="
suffix="&go=Go"

if null "$w3_args"; then
    if ifyes SURFRAW_wikipedia_ssl ; then
	w3_browse_url "https://secure.wikimedia.org/wikipedia/${SURFRAW_wikipedia_language}/wiki/Main_Page"
    else
	w3_browse_url "http://${SURFRAW_wikipedia_language}.wikipedia.org"
    fi
else
    escaped_args=`w3_url_of_arg $w3_args`
    if ifyes SURFRAW_wikipedia_ssl ; then
        w3_browse_url "${prefixs}${escaped_args}"
    else
	w3_browse_url "${prefix}${escaped_args}${suffix}"
    fi
fi
