#!/bin/sh
# $Id: ctan,v 1.1 2006-03-15 23:05:10 ianb-guest Exp $
# elvis: ctan		-- Search the Comprehensive TeX Archive Network (ctan.org)
. surfraw || exit 1

w3_config_hook () {
defyn SURFRAW_ctan_name  0
defyn SURFRAW_ctan_desc  0
defyn SURFRAW_ctan_doc   0
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Search the Comprehensive TeX Archive Network (ctan.org)
Local options:
  -name                         Search by filename
  -desc                         Search descriptions (default)
  -doc                          Search documentation
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -n*)        setoptyn SURFRAW_ctan_name   1       ;;
    -de*)       setoptyn SURFRAW_ctan_desc   1       ;;
    -do*)       setoptyn SURFRAW_ctan_doc    1       ;;
	*) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments
if test -z "$w3_args"; then
    w3_browse_url "http://www.ctan.org/"
else
    escaped_args=`w3_url_of_arg $w3_args`
	if ifno SURFRAW_ctan_name &&
	   ifno SURFRAW_ctan_desc  &&
	   ifno SURFRAW_ctan_doc
	then
		SURFRAW_ctan_desc=1
	fi

	url="http://www.ctan.org/cgi-bin/"
	if ifyes SURFRAW_ctan_name
	then
		url="${url}filenameSearch.py?filename=${escaped_args}"
	elif ifyes SURFRAW_ctan_desc
	then
		url="${url}search.py?metadataSearch=${escaped_args}"
	else # must be SURFRAW_ctan_doc
		url="${url}searchFullText.py?fullTextSearch=${escaped_args}"
	fi

	w3_browse_url "$url"
fi
