#!/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.8 $

# ej
#    English-Japanese transfer frontend derived from jisho(1)
#
# usage
#    ej [-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:
    ej words...
     -d dictonary   set target dictionary
     -h             print help message
     -v             print version'

# default configuration
#
dictionary=eijiro
readonly ejurl_eijiro='http://www2.alc.co.jp/ejr/index.php?word_in2=%82%A0%82%A2%82%A4%82%A6%82%A8&word_in3=PVawEWi72JXCKoa0Je&word_in='
readonly ejurl_goo='http://dictionary.goo.ne.jp/search.php?kind=ej&MT='
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.8 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))


# English to Japanese (Eijiro)
#     English to Japanese translation
#
# usage
#     etoj_eijiro word
# 
etoj_eijiro()
{
    local count=0
    local start=29
    w3m -o user_agent="$useragent" -dump "$ejurl_eijiro$(url_encode "$*")" | 
    while IFS= read line
    do
        count=$((1 + $count))
        [ $count -gt $start ] && echo "$line"
    done
}

# English to Japanese (Goo Dictonary)
#     English to Japanese translation
#
# usage
#     etoj_goo word
# 
etoj_goo()
{
    local count=0
    local start=29
    w3m -cols 100 -dump "$ejurl_goo$(url_encode "$*")" | 
    LANG=C cut -c 15-90 | 
    while IFS= read line
    do
        count=$((1 + $count))
        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 \"etoj_$dictionary\" "\$word" | $(pager)
done