### vim:ft=zsh:foldmethod=marker
###
### zsh function that looks up an argument in various webservices.
###
### Frank Terbeck <ft@bewatermyfriend.org>
### Last-Modified: Tue Dec 25 17:44:26 2007
###
### URI: <http://ft.bewatermyfriend.org/comp/zsh.html>
###
###
### currently supported:
###   dict.leo.org      ::  french<->german
###                         english<->german
###                         spanish<->german dictionary
###   www.google.com    ::  well... google...
###   google.com/linux  ::  linux specific google lookups
###   groups.google.com ::  various searches
###   lists.debian.org  ::  Andrew Suffield's MESSAGE-ID lookup
###                         lookups via google.com
###                         now run by Jeroen van Wolffelaar
###   bugs.debian.org   ::  querybts dropin ;-)
###   p.qa.debian.org   ::  PTS lookup
###   www.zsh.org/mla/  ::  lookup zsh ML URIs by numbers
###   en.wikipedia.org  ::  english wikipedia
###   de.wikipedia.org  ::  german wikipedia
###   csourcesearch.net ::  sourcecode search
###   koders.com        ::  yascs
###   sourceforge.net   ::  software lookup
###   freshmeat.net     ::  yaswl
###   filewatcher.com   ::  ftp lookups
###   RFC lookups       ::  looks up rfcs locally and remotely
###
### uses:
###   BROWSER           string
###                       Default: links
###   X_BROWSER_REMOTE  string
###                       Default: mozilla
###   LOCAL_RFC_PATH    colon seperated strings (similar to $PATH)
###                       Default: $HOME/share/rfc:/usr/share/doc/RFC/links
###

### just to get a known environment
emulate -L zsh
setopt extendedglob

local MY_BROWSER QUERY URI param remote googleme helptext firsthit rfc_path rfc_file

firsthit=0
remote=0
googleme=0
MY_BROWSER=${BROWSER:-links}
rfc_path=( ${(s.:.)LOCAL_RFC_PATH} )

### $helptext {{{
helptext="zsh internet lookup

usage: lookup [-R|-O|-P|-1] <option> query

options:
  -h      : this help text
  -g      : google.com
  -l      : google.com/linux
  -e      : dict.leo.org  eng-ger
  -f      : dict.leo.org  fre-ger
  -s      : dict.leo.org  esp-ger
  -G      : groups.google.com keyword query
  -m      : groups.google.com message-id query
  -a      : groups.google.com authors query
  -d      : lists.debian.org message-id query
  -B      : bugs.debian.org lookup
  -b      : bugs.debian.org by bug-id
  -p      : packages.qa.debian.org lookup
  -r      : display RFC by number
  -z      : lookup zsh ML URIs by number
  -w      : en.wikipedia.org
  -W      : de.wikipedia.org
  -C      : csourcesearch.net
  -k      : koders.net
  -S      : sourceforge.net
  -F      : freshmeat.net
  -D      : filewatcher.com

  -R      : send the query to an external browser (eg. mozilla)
  -P      : print the uri that would be used
  -O      : query website via google
          : (currently works for *.debian.org {-d,-B,-b})
  -1      : use /firsthit option for Jeroen\'s Message-id lookup

     Most <options> are rather commands, -R, -P, -O and -1 are really
     optional, but _must_ precede the command option if used.
     The command option, as well as the query string are required.
"
### }}}

while getopts "1hOPRg:l:e:f:G:m:a:d:B:b:w:W:C:k:p:z:S:s:F:D:r:" param; do
    case ${param} in
        ### -h    help {{{
        (h) print ${helptext}
            return 0  ;;
        ### }}}
        ### -g    google search {{{
        (g) QUERY=${OPTARG}
            URI="http://www.google.com/search?q=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -l    google linux specific search {{{
        (l) QUERY=${OPTARG}
            URI="http://www.google.com/linux?q=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -e    leo english <-> german dictionary {{{
        (e) QUERY=${OPTARG}
            URI="http://dict.leo.org?${QUERY}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -f    leo french  <-> german dictionary {{{
        (f) QUERY=${OPTARG}
            URI="http://dict.leo.org?lp=frde&search=${QUERY}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
            ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -s    leo spanish <-> german dictionary {{{
        (s) QUERY=${OPTARG}
            URI="http://dict.leo.org?lp=esde&search=${QUERY}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -G    google groups keyword lookup {{{
        (G) QUERY=${OPTARG}
            URI="http://groups.google.com/groups?q=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -m    google groups message-id lookup {{{
        (m) QUERY=${OPTARG}
            URI="http://groups.google.com/groups?selm=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -a    google groups author search {{{
        (a) QUERY=${OPTARG}
            URI="http://groups.google.com/groups?as_uauthors=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -d    lists.debian.org message-id lookup && google keyword search {{{
        (d) QUERY=${OPTARG}
            if [[ ${googleme} == 0 ]]; then
                if [[ ${firsthit} == 0 ]]; then
                    URI="http://lists.debian.org/msgid-search/${QUERY}/links"
                else
                    URI="http://lists.debian.org/msgid-search/${QUERY}/firsthit"
                fi
                if [[ ${remote} == 0 ]]; then
                    ${MY_BROWSER} ${URI}
                else
                    ${MY_BROWSER} -remote "openurl(${URI})"
                fi
            else
                URI="http://www.google.com/search?q=site:lists.debian.org+${QUERY:s/ /+/}"
                if [[ ${remote} == 0 ]]; then
                    ${MY_BROWSER} ${URI}
                else
                    ${MY_BROWSER} -remote "openurl(${URI})"
                fi
            fi
            return 0  ;;
        ### }}}
        ### -B    bugs.debian.org package && keyword lookup {{{
        (B) QUERY=${OPTARG}
            if [[ ${googleme} == 0 ]]; then
            URI="http://bugs.debian.org/${QUERY:s/ /%20/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            else
            URI="http://www.google.com/search?q=site:bugs.debian.org+${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
        fi
        return 0  ;;
        ### }}}
        ### -b    bugs.debian.org bug-id && keyword lookup {{{
        (b) QUERY=${OPTARG}
            if [[ ${googleme} == 0 ]]; then
                URI="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=${QUERY:s/#//}"
                if [[ ${remote} == 0 ]]; then
                    ${MY_BROWSER} ${URI}
                else
                    ${MY_BROWSER} -remote "openurl(${URI})"
                fi
            else
                URI="http://www.google.com/search?q=site:bugs.debian.org+${QUERY:s/ /+/}"
                if [[ ${remote} == 0 ]]; then
                    ${MY_BROWSER} ${URI}
                else
                    ${MY_BROWSER} -remote "openurl(${URI})"
                fi
            fi
            return 0  ;;
        ### }}}
        ### -p    packages.qa.debian.org: package tracking system {{{
        (p) QUERY=${OPTARG}
            URI="http://packages.qa.debian.org/${QUERY}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -z    lookup zsh ML URIs by number {{{
        (z) QUERY=${OPTARG}
            case ${QUERY} in
                ((#i)*u*)
                    URI='http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER='
                    ${MY_BROWSER} ${URI}${${QUERY##[^0-9]##}%%[^0-9]##}
                    ;;
                ((#i)*w*)
                    URI='http://www.zsh.org/cgi-bin/mla/redirect?WORKERNUMBER='
                    ${MY_BROWSER} ${URI}${${QUERY##[^0-9]##}%%[^0-9]##}
                    ;;
                ([0-9]*)
                    URI='http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER='
                    ${MY_BROWSER} ${URI}${${QUERY##[^0-9]##}%%[^0-9]##}
                    ;;
                (*)
                    print "cannot parse ${QUERY}"
                    ;;
            esac
            return 0 ;;
        ### }}}
        ### -w    english wikipedia lookup {{{
        (w) QUERY=${OPTARG}
            URI="http://en.wikipedia.org/wiki/${QUERY:s/ /_/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -W    german wikipedia lookup {{{
        (W) QUERY=${OPTARG}
            URI="http://de.wikipedia.org/wiki/${QUERY:s/ /_/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -C    csourcesearch.net source code search {{{
        (C) QUERY=${OPTARG}
            URI="http://csourcesearch.net/search/?csourceSearchBox=${QUERY:s/ /%20/}&csourceSearchType=Function%20Names&submit=Search"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -k    koders.com source code search {{{
        (k) QUERY=${OPTARG}
            URI="http://koders.com/?s=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -s    sf.net project search {{{
        (S) QUERY=${OPTARG}
            URI="http://sourceforge.net/search/?type=soft&q=${QUERY:s/ /+/}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -F    freshmeat project search {{{
        (F) QUERY=${OPTARG}
            URI="http://freshmeat.net/search/?q=${QUERY}&section=projects"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -D    filewatcher.com ftp download search {{{
        (D) QUERY=${OPTARG}
            URI="http://www.filewatcher.com/?q=${QUERY}"
            if [[ ${remote} == 0 ]]; then
                ${MY_BROWSER} ${URI}
            else
                ${MY_BROWSER} -remote "openurl(${URI})"
            fi
            return 0  ;;
        ### }}}
        ### -r    rfc display {{{

        ### This will try to autoview RFCs
        ###   Operation:
        ###     Check a search path: ${rfc_path}[number]/rfc$QUERY.txt{,.gz,.bz2}
        ###     If the rfc could not be found, try ftp://ftp.rfc-editor.org/in-notes/rfc$QUERY.txt
        ###       If that was found, ask if it should be downloaded to the first dir in $rfc_path
        ###       and after that view the file with the specified browser.
        (r) QUERY=${OPTARG:s/#//}
            if [[ -z ${rfc_path} ]] ; then
                rfc_path=( ${HOME}/share/rfc /usr/share/doc/RFC/links )
            fi
            print " RFC search path:"
            for i in {1..${#rfc_path}} ; do print "  ${i}: ${rfc_path[$i]}" ; done
                print "\n trying to find the document locally:\n"
                for i in {1..${#rfc_path}} ; do
                    for ext in {,.gz,.bz2} ; do
                        rfc_file="${rfc_path[$i]}/rfc${QUERY}.txt${ext}"
                        print "    checking ${rfc_file}"
                        if [[ -e ${rfc_file} ]] ; then
                            ### found
                            print "\n      found  ${rfc_file}"
                            if [[ ${remote} == 0 ]]; then
                                if [[ ${rfc_file:e} = "gz"   ]] ; then
                                    gunzip  -c ${rfc_file} | ${MY_BROWSER}
                                elif [[ ${rfc_file:e} = "bz2"  ]] ; then
                                    bunzip2 -c ${rfc_file} | ${MY_BROWSER}
                                else
                                    ${MY_BROWSER} ${rfc_file}
                                fi
                            else
                            if [[ ${rfc_file:e} = "gz"   ]] ; then
                                print "Sorry compressed files my not be supported by remote browsers."
                            elif [[ ${rfc_file:e} = "bz2"  ]] ; then
                                print "Sorry compressed files my not be supported by remote browsers."
                            else
                                ${MY_BROWSER} -remote "openurl(${rfc_file})"
                            fi
                        fi
                        return 0
                    fi
                done
            done
            print "\n document not found locally, trying remote..."
            if [[ ! -d ${rfc_path[1]} || ! -w ${rfc_path[1]} ]] ; then
                print " download path \'${rfc_path[1]}\' does not exist, or is not writable.\n ABORT. Please fix!"
                return 0;
            fi
            if [[ ! -x =lftp ]] ; then
                print "sorry, lftp is not in your \$PATH, please install it for this function."
                return 0;
            fi
            if lftp -c "lcd ${rfc_path[1]} ; get ftp://ftp.rfc-editor.org/in-notes/rfc${QUERY}.txt ; quit" ; then
                rfc_file="${rfc_path[1]}/rfc${QUERY}.txt"
                if [[ ${remote} == 0 ]]; then
                    ${MY_BROWSER} ${rfc_file}
                else
                    ${MY_BROWSER} -remote "openurl(${rfc_file})"
                fi
            else
                print " sorry, rfc${QUERY}.txt could not be found remotely."
            fi
            return 0 ;;
        ### }}}
        ### ----- misc option flags {{{

        (R) MY_BROWSER=${X_BROWSER_REMOTE:-mozilla} ; remote=1;;
        (P) MY_BROWSER="echo" ; remote=0;;
        (O) googleme=1 ;;
        (1) firsthit=1 ;;
        (?) return 1  ;;

            ### }}}
    esac
done

print "try $0 -h for help"