#!/bin/sh
# $Id: debcontents,v 1.11 2009-05-13 01:22:54 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  "files"
defyn SURFRAW_debcontents_ubuntu  no
defyn SURFRAW_debcontents_archive 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:
  -a                            Search archived debian releases
  -u                            Search ubuntu packages instead of debian
  -search=                      Type of search
      files             |       Packages that contain files named like this
      path              |       Paths ending with the keyword
      word              |       Packages that contain files or directories
                                whose names contain the keyword
                                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
     Archived Debian (-a):
      bo                |       1.3.1
      hamm              |       2.0
      slink             |       2.1
      potato            |       2.2
      woody             |       3.0
      sarge             |       3.1
     Ubuntu (-u):
       dapper           |       Dapper
       dapper-updates   |       Dapper Updates
       dapper-backports |       Dapper Backports
       hardy            |       Hardy
       hardy-updates    |       Hardy Updates
       hardy-backports  |       Hardy Backports
       intrepid         |       Intrepid
       intrepid-updates |       Intrepid Updates
       intrepid-backports|       Intrepid Backports
       jaunty           |       Jaunty
       jaunty-updates   |       Jaunty Updates
       jaunty-backports |       Jaunty Backports
       karmic           |       Karmic
       karmic-updates   |       Karmic Updates
       karmic-backports |       Karmic 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    	;;
	-a*)        setoptyn SURFRAW_debcontents_archive 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"
elif ifyes SURFRAW_debcontents_archive
then
	searchpage="http://archive.debian.net/#search_contents"
	searchurl="http://archive.debian.net/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=jaunty
	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
	p*)      if ifyes SURFRAW_debcontents_ubuntu ||
	            ifyes SURFRAW_debcontents_archive
                 then
                    searchmode=""
		 else
		    searchmode=path
                 fi;;
	f*)      searchmode=exactfilename  ;;
	w*)      searchmode=filename       ;;
	*)       err "Unknown search type" ;;
    esac

    url="${url}&mode=${searchmode}"
    w3_browse_url "$url"
fi
