#!/bin/sh
# $Id$
# elvis: debpackages	-- Search debian/ubuntu packages (packages.debian.org/packages.ubuntu.com)
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_debian_release  any
def   SURFRAW_debian_distro   any
def   SURFRAW_debian_search   pkg
defyn SURFRAW_debian_ubuntu   no
defyn SURFRAW_debian_subwords yes
defyn SURFRAW_debian_archive  no
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Search debian/ubuntu packages (packages.debian.org/packages.ubuntu.com)
Local options:
  -a                            Search archived debian distributions
  -u                            Search ubuntu packages instead of debian
  -release=                     Specialized search on release
    Debian:
      any               |       all releases
      main              |       main release
      contrib           |       contrib
      non-free          |       packages not meeting DFSG
    Ubuntu (-u):
      any               |       all releases
      main              |
      restricted        |
      universe          |
      multiverse        |

                                Default: $SURFRAW_debian_release
                                Environment: SURFRAW_debian_release
  -distro=                      Specific distribution
    Debian:
      any               |       All distributions
      stable            |       Stable
      testing           |       Testing
      unstable          |       Unstable
      oldstable         |       Old Stable
      experimental      |       Experimental
    Archived Debian (-a):
      bo                |       1.3.1
      hamm              |       2.0
      slink             |       2.1
      potato            |       2.2
      woody             |       3.0
      sarge             |       3.1
    Ubuntu (-u):
      any               |       All distributions
      dapper            |
      dapper-updates    |
      dapper-backports  |
      hardy             |
      hardy-updates     |
      hardy-backports   |
      intrepid          |
      intrepid-updates  |
      intrepid-backports|
      jaunty            |
      jaunty-updates    |
      jaunty-backports  |
      karmic            |
      karmic-updates    |
      karmic-backports
                                Default: $SURFRAW_debian_distro
                                Environment: SURFRAW_debian_distro
  -search=                      Field to search
          pkg           |       Package names only
          desc          |       Descriptions
          src           |       Source package names
  -nosub                        Don't allow subword matches (exact matches only)
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
	-release=*) setopt   SURFRAW_debian_release  $optarg ;;
	-distro=*)  setopt   SURFRAW_debian_distro   $optarg ;;
	-search=*)  setopt   SURFRAW_debian_search   $optarg ;;
	-a*)        setoptyn SURFRAW_debian_archive  yes     ;;
	-u*)        setoptyn SURFRAW_debian_ubuntu   yes     ;;
	-nos*)      setoptyn SURFRAW_debian_subwords no	     ;;
	*) return 1 ;;
    esac
    return 0
}

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

if   ifyes SURFRAW_debian_ubuntu
then
	searchpage="http://packages.ubuntu.com/"
	searchurl="http://packages.ubuntu.com/search"
elif ifyes SURFRAW_debian_archive
then
	searchpage="http://archive.debian.net/"
	searchurl="http://archive.debian.net/search"
else
	searchpage="http://packages.debian.org/#search_packages"
	searchurl="http://packages.debian.org/search"
fi

if test -z "$w3_args"; then
    w3_browse_url "$searchpage"
else
    escaped_args=`w3_url_of_arg $w3_args`
    if [ "${SURFRAW_debian_distro}" = "any" ]; then
	SURFRAW_debian_distro="all"
    fi
    if [ "${SURFRAW_debian_release}" = "any" ]; then
	SURFRAW_debian_release="all"
    fi
    url="$searchurl?keywords=${escaped_args}&suite=${SURFRAW_debian_distro}&section=${SURFRAW_debian_release}"

    case "$SURFRAW_debian_search" in
	pkg*)  url="$url&searchon=names"        ;;
	src*)  url="$url&searchon=sourcenames"  ;;
	desc*) url="$url&searchon=all"          ;;
	*)     err "Unknown search type"        ;;
    esac

    if ifno SURFRAW_debian_subwords
    then
	url="$url&exact=1"
    fi

    w3_browse_url "$url"
fi
