#!/bin/sh
# elvis: rpmsearch	-- Search for RPMs in various distros
# ianb@erislabs.net 20091112
. surfraw || exit 1

w3_config_hook () {
    def   SURFRAW_rpmsearch_distro  all
    def   SURFRAW_rpmsearch_deptype ""
    def   SURFRAW_rpmsearch_by      fuzzyname
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Search for RPMs in various distros
Local options:
  -search=TYPE                  Search by TYPE
          fuzzyname       |      Fuzzy name match
          rpmname         |      RPM name
          dep             |      Dependency Match
          file            |      Search by File
                                Default: $SURFRAW_rpmsearch_by
                                Environment: SURFRAW_rpmsearch_by
  -dep=                         Dependency match type (implies -search=dep)
          provides        |      Provides
          requires        |      Requires
          conflict        |      Conflict
          obsoletes       |      Obsoletes
          trigger         |      Trigger
          suggest         |      Suggest
          enhanced        |      Enhanced
                                Default: $SURFRAW_rpmsearch_by
                                Environment: SURFRAW_rpmsearch_by
  -distro=NAME                  Search in distribution NAME
          all             |      All (default)
          Altlinux        |      Altlinux
          Arklinux        |      Arklinux
          Caosity         |      Caosity
          CentOS          |      CentOS
          Fedora          |      Fedora
          Mageia          |      Mageia
          Mandriva        |      Mandriva
          Momonga         |      Momonga
          OpenSuse        |      OpenSuse
          PLD             |      PLD
          ScientificLinux |      Scientific Linux
          UnityLinux      |      Unity Linux
                                Default: $SURFRAW_rpmsearch_distro
                                Environment: SURFRAW_rpmsearch_distro
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -dist*=*)   setopt   SURFRAW_rpmsearch_distro  "$optarg" ;;
    -de*=*)     case "$optarg" in
		    [Pp]*) setopt SURFRAW_rpmsearch_deptype "P";;
		    [Rr]*) setopt SURFRAW_rpmsearch_deptype "R";;
		    [Cc]*) setopt SURFRAW_rpmsearch_deptype "C";;
		    [Oo]*) setopt SURFRAW_rpmsearch_deptype "O";;
		    [Tt]*) setopt SURFRAW_rpmsearch_deptype "T";;
		    [Ss]*) setopt SURFRAW_rpmsearch_deptype "S";;
		    [Ee]*) setopt SURFRAW_rpmsearch_deptype "E";;
		    *)     err "unknown dep type";;
		esac
		setopt SURFRAW_rpmsearch_by bydep ;;
    -sear*=*)   case "$optarg" in
                    fu*)      setopt SURFRAW_rpmsearch_by fuzzyname;;
		    byn*|r*)  setopt SURFRAW_rpmsearch_by byname;;
		    bydep|d*) setopt SURFRAW_rpmsearch_by bydep;;
		    byf|fi*)  setopt SURFRAW_rpmsearch_by byfile;;
		    *)        err "unknown search type" ;;
		esac ;;

    *) 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://sophie.zarb.org/search"
else
    escaped_args=`w3_url_of_arg $w3_args`

    if [ "$SURFRAW_rpmsearch_distro" = all ]; then
	SURFRAW_rpmsearch_distro=""
    elif [ "$SURFRAW_rpmsearch_distro" = ScientificLinux ]; then
	SURFRAW_rpmsearch_distro="Scientific+Linux"
    fi
    w3_browse_url "http://sophie.zarb.org/search?distribution=${SURFRAW_rpmsearch_distro}&search=${escaped_args}&type=${SURFRAW_rpmsearch_by}&deptype=${SURFRAW_rpmsearch_deptype}"
fi
