### vim:ft=zsh:foldmethod=marker
## zsh mla search backend for lookup
## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>

LOOKUP_guard || return 1
[[ -n ${lookup_describe} ]] &&
    printf '%s' 'search interface for zsh'\''s ML archive' &&
    return 0

local lookup_context
local -a comp_args

lookup_context="$(LOOKUP_context)"

LOOKUP_guard -fd LOOKUP_help_${backend} ||
function LOOKUP_help_${backend}() {
    LOOKUP_guard || return 1
    printf 'usage: %s [-{n,u,w}] [-{l,y} <arg>] <query>\n' ${backend}
    printf '  -n            search the archives for keywords ('\''normal'\'' mode)\n'
    printf '  -u            search user archive by X-Seq: header\n'
    printf '  -w            search workers archive by X-Seq: header\n'
    printf '  -l <list>     limit search to a specific list\n'
    printf '  -y <year>     limit search to a specific year\n'
    printf '\n Query zsh[1]'\''s Mailing List Arhive[2].'
    printf '\n The normal keyword search is done via google'\''s '\''site:'\'' feature.\n'
    printf ' In X-Seq modes (-u and -w), all non-digits are stripped from query.\n'
    printf '\n The default mode is '\''normal'\'', which can be modified via the\n'
    printf ' '\''default-mode'\'' style in this context: %s\n' ${lookup_context}
    printf ' Valid values are: normal, users, workers\n'
    printf '\nExamples:\n'
    printf ' %% zstyle '\'':lookup:*:%s:*'\'' default-mode users\n' ${backend}
    printf ' %% lookup %s released\n' ${backend}
    printf ' %% lookup %s -u 12345\n' ${backend}
    printf ' %% lookup %s -w 12345\n' ${backend}
    printf '\n[1] <http://zsh.sourceforge.net>\n'
    printf '[2] <http://www.zsh.org/mla/>\n'
}
LOOKUP_help && return 0

if [[ -n ${lookup_complete} ]] ; then
    comp_args=(
        '-u[users x-seq search]:users\: X-Seq header number:'
        '-w[workers x-seq search]:workers\: X-Seq header number:'
        '-l[limit search to a specific list]:list name:(users workers "*")'
        '-y[limit search to a specific year]:year - "*" means all years:'
        '*:zsh mla search:true'
    )

    _arguments -s -w -A '-*' ${comp_args} && return 0
    _message 'zsh mla search'
    return 0
fi

local list year mode
local -A opts
local -x QUERY

list='*'
year='*'
lu_parseopts_args=(
    n   bool
    u   bool
    w   bool
    l   string
    y   string
)
LOOKUP_parseopts "$@" || return 1
[[ ${opts[-u]} == 'yes' ]] && mode='users'
[[ ${opts[-w]} == 'yes' ]] && mode='workers'
[[ ${opts[-n]} == 'yes' ]] && mode='normal'
[[ -n ${opts[-l]} ]] && list="${opts[-l]}"
[[ -n ${opts[-y]} ]] && year="${opts[-y]}"

if [[ -z ${mode} ]] ; then
    zstyle -s "${lookup_context}" default-mode mode || mode='normal'
fi

QUERY="${args[*]}"
LOOKUP_query_handler || return 1
if [[ -z ${QUERY} ]] ; then
    LOOKUP_help -f
    return 1
fi

case ${mode} in
(users|workers)
    QUERY="${QUERY//[^0-9]/}"
    if [[ ${mode} == 'users' ]] ; then
        LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?USERNUMBER=${QUERY}"
        return $?
    else
        LOOKUP_browser "http://www.zsh.org/cgi-bin/mla/redirect?WORKERNUMBER=${QUERY}"
        return $?
    fi
    ;;
(*)
    LOOKUP_encode -s -q
    LOOKUP_browser "http://www.google.com/search?q=site:www.zsh.org/mla/${list}/${year}/+${QUERY}"
    return $?
    ;;
esac
