#!/bin/sh
#
# Copyright (c) 2003-2005 Daichi GOTO <daichi@ongs.co.jp>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
# THE POSSIBILITY OF SUCH DAMAGE.

# author: Daichi GOTO (daichi@ongs.co.jp)
# first edition: Mon Mar 31 13:54:51 2003
# last modified: $Date: 2005/03/22 15:16:43 $
# version: $Revision: 1.7 $

# jisho
#    Japanese dictionary frontend derived from jisho(1)
#
# usage
#    jisho [-d dictonary] words...


# check_required_program
#     check that the required program exists
# usage
#     check_required_program program_name port_directory
check_required_program()
{
    type "$1" > /dev/null || {
        echo 'error:'
        echo "    you need $1."
        echo "    install it from $2."
        ! :
    } 1>&2
}

# pager
#    get pager
# usage
#    pager
pager()
{
    local pager=${PAGER:-lv}
    pager=${pager##*/}
    pager=${pager%% *}
    type $pager > /dev/null || {
        pager=jless
        type $pager > /dev/null || pager=cat
    }
    echo $pager
}

# url_encode
#    get URL encoded string
# usage
#    url_encode 'string'
url_encode()
{
    is_alphabet "$*" && { echo "$*" | sed 's/ /+/g'; return; }
    check_required_program nkf /usr/ports/japanese/nkf/
    echo -n "$*" | nkf -s | 
    perl -pe 's/([^ w])/sprintf("%%%02X", ord($1))/eg;' | tr ' ' '+'
}

# usage
#    print usage
# usage
#    usage_msg='usage message'
#    usage
usage()
{
    echo "$usage_msg" 1>&2
}

# version
#    print version
# usage
#    version '$Revision: 1.19 $'
version()
{
    ver=$1
    ver=${ver#* }
    echo ${ver% $}
}

usage_msg='usage:
    jisho words...
     -d dictonary   set target dictionary
     -h             print help message'

# default configuration
#
dictionary=infoseek
readonly url_goo='http://dictionary.goo.ne.jp/search.php?kind=jn&MT='
readonly url_infoseek='http://jiten.www.infoseek.co.jp/Kokugo?sm=0&pg=result_k.html&col=KO&qt='
readonly useragent='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'

# check the option arguments
#
while getopts d:hv option
do
    case "$option" in
    d)
        dictionary=$OPTARG
        ;;
    h|\?)
        usage
        exit 0
        ;;
    v)
        version '$Revision: 1.7 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))

# Infoseek Japanese Dictionary
#
# usage
#     dic_infoseek word
# 
dic_infoseek()
{
    count=0
    start=19
    w3m -cols 144 -dump "$url_infoseek$(url_encode "$*")" | 
    LANG=C cut -c 31-113 | 
    while IFS= read line
    do
        count=$((1 + $count))
        echo "$line" | grep '^Ads ' > /dev/null && return
        echo "$line" | grep 'つかりませんでした' > /dev/null && return
        [ $count -gt $start ] && echo "$line"
    done
}

# Goo Japanese Dictionary
#
# usage
#     dic_goo word
# 
dic_goo()
{
    count=0
    start=30
    w3m -cols 92 -dump "$url_goo$(url_encode "$*")" | 
    LANG=C cut -c 11-84 | 
    while IFS= read line
    do
        count=$((1 + $count))
        echo "$line" | grep '堂提供' > /dev/null && return
        echo "$line" | grep '^検索結果に該当するものが' > /dev/null && return
        [ $count -gt $start ] && echo "$line"
    done
}

check_required_program w3m /usr/ports/japanese/w3m-img/ || exit 1

for word
do
    eval \"dic_$dictionary\" "\$word" | $(pager)
done
