#!/bin/sh
# $Id: debcontents,v 1.9 2008-03-11 12:59:45 ianb-guest Exp $
# elvis: debcontents	-- Search contents of debian/ubuntu packages (packages.debian.org/packages.ubuntu.com)
. surfraw || exit 1

w3_config_hook () {
def   SURFRAW_debcontents_arch    i386
def   SURFRAW_debcontents_distro  stable
def   SURFRAW_debcontents_search  ""
defyn SURFRAW_debcontents_ubuntu  no
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Search contents of debian/ubuntu packages (packages.debian.org/packages.ubuntu.com)
Local options:
  -u                            Search ubuntu packages instead of debian
  -search=                      Type of search
      files             |       Packages that contain files named like this (debian, ubuntu)
      path              |       Paths ending with the keyword (debian)
      dirs              |       Packages that contain files or directories
                                named like this (ubuntu)
      word              |       Packages that contain files or directories
                                whose names contain the keyword (debian, ubuntu)
      list                      List all files in this package (ubuntu)
                                Default: $SURFRAW_debcontents_search
                                Environment: SURFRAW_debcontents_search
  -arch=                        Specialized search on architecture
      i386              |       Intel x86       (debian & ubuntu)
      powerpc           |       PowerPC         (debian & ubuntu)
      amd64             |       AMD64           (debian & ubuntu)
      m68k              |       Motorola 680x0  (debian)
      alpha             |       Alpha           (debian)
      sparc             |       SPARC           (debian)
      arm               |       ARM             (debian)
      hppa              |       HP PA/RISC      (debian)
      ia64              |       Intel IA-64     (debian)
      mips              |       MIPS            (debian)
      mipsel            |       MIPS (DEC)      (debian)
      s390              |       IBM S/390       (debian)
      hurd-i386                 Hurd (i386)     (debian)
                                Default: $SURFRAW_debcontents_arch
                                Environment: SURFRAW_debcontents_arch
  -distro=                      Specific distribution
     Debian:
       stable           |       Stable
       testing          |       Testing
       unstable         |       Unstable
       oldstable        |       Old Stable
     Ubuntu (-u):
       dapper           |       Dapper
       dapper-updates   |       Dapper Updates
       dapper-backports |       Dapper Backports
       edgy             |       Edgy
       edgy-updates     |       Edgy Updates
       edgy-backports   |       Edgy Backports
       feisty           |       Feisty
       feisty-updates   |       Feisty Updates
       feisty-backports |       Feisty Backports
       gutsy            |       Gutsy
       gutsy-updates    |       Gutsy Updates
       gutsy-backports  |       Gutsy Backports
       hardy            |       Hardy
       hardy-updates    |       Hardy Updates
       hardy-backports  |       Hardy Backports
                                Default: $SURFRAW_debcontents_distro
                                Environment: SURFRAW_debcontents_distro
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
	-arch=*)    setopt   SURFRAW_debcontents_arch 	 $optarg	;;
	-distro=*)  setopt   SURFRAW_debcontents_distro  $optarg	;;
	-search=*)  setopt   SURFRAW_debcontents_search  $optarg	;;
	-u*)        setoptyn SURFRAW_debcontents_ubuntu  yes    	;;
	-ca*) 	    setoptyn SURFRAW_debcontents_usecase yes    	;;
	*) return 1 ;;
    esac
    return 0
}

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

if ifyes SURFRAW_debcontents_ubuntu
then
	searchpage="http://packages.ubuntu.com/#search_contents"
	searchurl="http://packages.ubuntu.com/search"
else
	searchpage="http://packages.debian.org/#search_contents"
	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 ifyes SURFRAW_debcontents_ubuntu
    then
	if [ "$SURFRAW_debcontents_distro" = "stable" ]
	then
		SURFRAW_debcontents_distro=gutsy
	fi
    fi
    url="$searchurl?keywords=${escaped_args}&searchon=contents"

    url="$url&arch=${SURFRAW_debcontents_arch}"
    url="$url&suite=${SURFRAW_debcontents_distro}"

    case "$SURFRAW_debcontents_search" in
	"")  url="$url&mode=path"           ;;
	p*)  url="$url&mode=path"           ;;
	f*)  url="$url&mode=exactfilename"  ;;
	w*)  url="$url&mode=filename"       ;;
	*)   err "Unknown search type"      ;;
    esac

    w3_browse_url "$url"
fi
