#!/bin/sh
# $Id: debcontents,v 1.7 2007-09-07 02:59:36 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_usecase no
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
  -case                         Make searches case-sensitive (ubuntu only)
  -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):
          warty         |       Warty
          hoary         |       Hoary
          breezy        |       Breezy
          dapper        |       Dapper
          edgy          |       Edgy
          feisty        |       Feisty
          gutsy         |       Gutsy
                                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/"
	searchurl="http://packages.ubuntu.com/cgi-bin/search_contents.pl"
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
	# old style still used by ubuntu
	url="$searchurl?word=${escaped_args}&arch=${SURFRAW_debcontents_arch}"

	if [ "$SURFRAW_debcontents_distro" = "stable" ]
	then
		SURFRAW_debcontents_distro=dapper
	fi
	url="$url&version=${SURFRAW_debcontents_distro}"

	case "$SURFRAW_debcontents_search" in
	        "")  url="$url&searchmode=searchfiles"        ;;
		f*)  url="$url&searchmode=searchfiles"        ;;
		d*)  url="$url&searchmode=searchfilesanddirs" ;;
		w*)  url="$url&searchmode=searchword"         ;;
		l*)  url="$url&searchmode=filelist"           ;;
		*)   err "Unknown search type"                ;;
	esac

	if ifyes SURFRAW_debcontents_usecase
	then
		url="$url&case=sensitive"
	else
		url="$url&case=insensitive"
	fi
    else # new style as used on p.d.o
	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
    fi

    w3_browse_url "$url"
fi
