#!/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: Wed Mar 26 22:23:12 2003
# last modified: $Date: 2005/03/22 07:42:43 $
# version: $Revision: 1.34 $

# tenki
#    display tenki-yohou 
#
# usage
#    tenki [-w] [-a] [-i] [-t todoufuken] [-r retry_count] [-h]


# 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
}

# error
#    print error message
# usage
#    error_msg='error message'
#    error
error()
{
    echo "$error_msg" 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
}

# 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:
    tenki   display day tenki-yohou
     -w             display week tenki-yohou
     -a             display day and week tenki-yohou
     -i             display weather information
     -t todouhuken  set target todouhuken
     -p todouhuken  the same to -t
     -r retrycount  set retry count
     -h             print help message
     -v             print version'

# default configuration
#
yohouplace=tokyo
dayyohou=yes
weekyohou=no
information=no
retrycount=10

readonly yohouFQDN=www.jma.go.jp
readonly yohouport=80

readonly todoufuken_hokkaido='ËÌ³¤Æ»'
readonly daypath_hokkaido=/JMA_HP/jp/yoho/104.html
readonly weekpath_hokkaido=/JMA_HP/jp/week/104.html
readonly todoufuken_aomori='ÀÄ¿¹'
readonly daypath_aomori=/JMA_HP/jp/yoho/2.html
readonly weekpath_aomori=/JMA_HP/jp/week/2.html
readonly todoufuken_iwate='´ä¼ê'
readonly daypath_iwate=/JMA_HP/jp/yoho/3.html
readonly weekpath_iwate=/JMA_HP/jp/week/3.html
readonly todoufuken_miyagi='µÜ¾ë'
readonly daypath_miyagi=/JMA_HP/jp/yoho/4.html
readonly weekpath_miyagi=/JMA_HP/jp/week/4.html
readonly todoufuken_akita='½©ÅÄ'
readonly daypath_akita=/JMA_HP/jp/yoho/5.html
readonly weekpath_akita=/JMA_HP/jp/week/5.html
readonly todoufuken_yamagata='»³·Á'
readonly daypath_yamagata=/JMA_HP/jp/yoho/6.html
readonly weekpath_yamagata=/JMA_HP/jp/week/6.html
readonly todoufuken_fukushima='Ê¡Åç'
readonly daypath_fukushima=/JMA_HP/jp/yoho/7.html
readonly weekpath_fukushima=/JMA_HP/jp/week/7.html
readonly todoufuken_ibaragi='°ñ¾ë'
readonly daypath_ibaragi=/JMA_HP/jp/yoho/8.html
readonly weekpath_ibaragi=/JMA_HP/jp/week/8.html
readonly todoufuken_tochigi='ÆÊÌÚ'
readonly daypath_tochigi=/JMA_HP/jp/yoho/9.html
readonly weekpath_tochigi=/JMA_HP/jp/week/9.html
readonly todoufuken_gunma='·²ÇÏ'
readonly daypath_gunma=/JMA_HP/jp/yoho/10.html
readonly weekpath_gunma=/JMA_HP/jp/week/10.html
readonly todoufuken_saitama='ºë¶Ì'
readonly daypath_saitama=/JMA_HP/jp/yoho/11.html
readonly weekpath_saitama=/JMA_HP/jp/week/11.html
readonly todoufuken_chiba='ÀéÍÕ'
readonly daypath_chiba=/JMA_HP/jp/yoho/12.html
readonly weekpath_chiba=/JMA_HP/jp/week/12.html
readonly todoufuken_tokyo='Åìµþ'
readonly daypath_tokyo=/JMA_HP/jp/yoho/13.html
readonly weekpath_tokyo=/JMA_HP/jp/week/13.html
readonly todoufuken_kanagawa='¿ÀÆàÀî'
readonly daypath_kanagawa=/JMA_HP/jp/yoho/14.html
readonly weekpath_kanagawa=/JMA_HP/jp/week/14.html
readonly todoufuken_niigata='¿·³ã'
readonly daypath_niigata=/JMA_HP/jp/yoho/15.html
readonly weekpath_niigata=/JMA_HP/jp/week/15.html
readonly todoufuken_toyama='ÉÙ»³'
readonly daypath_toyama=/JMA_HP/jp/yoho/16.html
readonly weekpath_toyama=/JMA_HP/jp/week/16.html
readonly todoufuken_ishikawa='ÀÐÀî'
readonly daypath_ishikawa=/JMA_HP/jp/yoho/17.html
readonly weekpath_=/JMA_HP/jp/week/17.html
readonly todoufuken_fukui='Ê¡°æ'
readonly daypath_fukui=/JMA_HP/jp/yoho/18.html
readonly weekpath_fukui=/JMA_HP/jp/week/18.html
readonly todoufuken_yamanashi='»³Íü'
readonly daypath_yamanashi=/JMA_HP/jp/yoho/19.html
readonly weekpath_yamanashi=/JMA_HP/jp/week/19.html
readonly todoufuken_nagano='Ä¹Ìî'
readonly daypath_nagano=/JMA_HP/jp/yoho/20.html
readonly weekpath_nagano=/JMA_HP/jp/week/20.html
readonly todoufuken_gifu='´ôÉì'
readonly daypath_gifu=/JMA_HP/jp/yoho/21.html
readonly weekpath_gifu=/JMA_HP/jp/week/21.html
readonly todoufuken_shizuoka='ÀÅ²¬'
readonly daypath_shizuoka=/JMA_HP/jp/yoho/22.html
readonly weekpath_shizuoka=/JMA_HP/jp/week/22.html
readonly todoufuken_aichi='°¦ÃÎ'
readonly daypath_aichi=/JMA_HP/jp/yoho/23.html
readonly weekpath_aichi=/JMA_HP/jp/week/23.html
readonly todoufuken_mie='»°½Å'
readonly daypath_mie=/JMA_HP/jp/yoho/24.html
readonly weekpath_mie=/JMA_HP/jp/week/24.html
readonly todoufuken_shiga='¼¢²ì'
readonly daypath_shiga=/JMA_HP/jp/yoho/25.html
readonly weekpath_shiga=/JMA_HP/jp/week/25.html
readonly todoufuken_kyoto='µþÅÔ'
readonly daypath_kyoto=/JMA_HP/jp/yoho/26.html
readonly weekpath_kyoto=/JMA_HP/jp/week/26.html
readonly todoufuken_osaka='Âçºå'
readonly daypath_osaka=/JMA_HP/jp/yoho/27.html
readonly weekpath_osaka=/JMA_HP/jp/week/27.html
readonly todoufuken_hyogo='Ê¼¸Ë'
readonly daypath_hyogo=/JMA_HP/jp/yoho/28.html
readonly weekpath_hyogo=/JMA_HP/jp/week/28.html
readonly todoufuken_nara='ÆàÎÉ'
readonly daypath_nara=/JMA_HP/jp/yoho/29.html
readonly weekpath_nara=/JMA_HP/jp/week/29.html
readonly todoufuken_wakayama='ÏÂ²Î»³'
readonly daypath_wakayama=/JMA_HP/jp/yoho/30.html
readonly weekpath_wakayama=/JMA_HP/jp/week/30.html
readonly todoufuken_tottori='Ä»¼è'
readonly daypath_tottori=/JMA_HP/jp/yoho/31.html
readonly weekpath_tottori=/JMA_HP/jp/week/31.html
readonly todoufuken_shimane='Åçº¬'
readonly daypath_shimane=/JMA_HP/jp/yoho/32.html
readonly weekpath_shimane=/JMA_HP/jp/week/32.html
readonly todoufuken_okayama='²¬»³'
readonly daypath_okayama=/JMA_HP/jp/yoho/33.html
readonly weekpath_okayama=/JMA_HP/jp/week/33.html
readonly todoufuken_hiroshima='¹­Åç'
readonly daypath_hiroshima=/JMA_HP/jp/yoho/34.html
readonly weekpath_hiroshima=/JMA_HP/jp/week/34.html
readonly todoufuken_yamaguchi='»³¸ý'
readonly daypath_yamaguchi=/JMA_HP/jp/yoho/35.html
readonly weekpath_yamaguchi=/JMA_HP/jp/week/35.html
readonly todoufuken_tokushima='ÆÁÅç'
readonly daypath_tokushima=/JMA_HP/jp/yoho/36.html
readonly weekpath_tokushima=/JMA_HP/jp/week/36.html
readonly todoufuken_kagawa='¹áÀî'
readonly daypath_kagawa=/JMA_HP/jp/yoho/37.html
readonly weekpath_kagawa=/JMA_HP/jp/week/37.html
readonly todoufuken_ehime='°¦É²'
readonly daypath_ehime=/JMA_HP/jp/yoho/38.html
readonly weekpath_ehime=/JMA_HP/jp/week/38.html
readonly todoufuken_kouchi='¹âÃÎ'
readonly daypath_kouchi=/JMA_HP/jp/yoho/39.html
readonly weekpath_kouchi=/JMA_HP/jp/week/39.html
readonly todoufuken_fukuoka='Ê¡²¬'
readonly daypath_fukuoka=/JMA_HP/jp/yoho/40.html
readonly weekpath_fukuoka=/JMA_HP/jp/week/40.html
readonly todoufuken_saga='º´²ì'
readonly daypath_saga=/JMA_HP/jp/yoho/41.html
readonly weekpath_saga=/JMA_HP/jp/week/41.html
readonly todoufuken_nagasaki='Ä¹ºê'
readonly daypath_nagasaki=/JMA_HP/jp/yoho/42.html
readonly weekpath_nagasaki=/JMA_HP/jp/week/42.html
readonly todoufuken_kumamoto='·§ËÜ'
readonly daypath_kumamoto=/JMA_HP/jp/yoho/43.html
readonly weekpath_kumamoto=/JMA_HP/jp/week/43.html
readonly todoufuken_ooita='ÂçÊ¬'
readonly daypath_ooita=/JMA_HP/jp/yoho/44.html
readonly weekpath_ooita=/JMA_HP/jp/week/44.html
readonly todoufuken_miyazaki='µÜºê'
readonly daypath_miyazaki=/JMA_HP/jp/yoho/45.html
readonly weekpath_miyazaki=/JMA_HP/jp/week/45.html
readonly todoufuken_kagoshima='¼¯»ùÅç'
readonly daypath_kagoshima=/JMA_HP/jp/yoho/46.html
readonly weekpath_kagoshima=/JMA_HP/jp/week/46.html
readonly todoufuken_okinawa='²­Æì'
readonly daypath_okinawa=/JMA_HP/jp/yoho/4701.html
readonly weekpath_okinawa=/JMA_HP/jp/week/4701.html

# check the option arguments
#
while getopts wat:p:r:ihv option
do
    case "$option" in
    w)
        weekyohou=yes
        dayyohou=no
        ;;
    a)
        weekyohou=yes
        dayyohou=yes
        ;;
    i)
        information=yes
        ;;
    t|p)
        yohouplace=$OPTARG
        ;;
    r)
        retrycount=$OPTARG
        ;;
    h|\?)
        usage
        exit 0
        ;;
    v)
        version '$Revision: 1.34 $'
        exit 0
        ;;
    esac
done
shift $(($OPTIND - 1))

# day_tenki-yohou
#     display day tenki-yohou
# usage
#    day_tenki-yohou
#
day_tenki-yohou()
{
    local count=0
    local start=10
    w3m -dump "http://www.jma.go.jp$daypath" | LANG=C cut -c 29- | 
    while read line
    do
        count=$((1 + $count))
        [ $count -gt 14 -a "$(echo "$line" | grep '¨§')" ] && {
            echo "$line"
            break
        }
        [ $count -gt $start ] && echo "$line"
    done
}

# week_tenki-yohou
#     display week tenki-yohou
# usage
#    week_tenki-yohou
#
week_tenki-yohou()
{
    local count=0
    local start=6
    w3m -dump "http://www.jma.go.jp$weekpath" | LANG=C cut -c 25-100 | 
    while read line
    do
        count=$((1 + $count))
        [ "$(echo "$line" | grep '¨¦')" ] && {
            echo "$line"
            break
        }
        [ $count -gt $start ] && echo "$line"
    done | $(pager)
}

# weather-information
#     display week tenki-yohou
# usage
#    week_tenki-yohou
#
weather-information()
{
    [ yes = "$dayyohou" ] && {
        url="http://www.jma.go.jp$daypath"
        w3m -dump "$url" | LANG=C cut -c 25-100 | $(pager)
    }

    [ yes = "$weekyohou" ] && {
        url="http://www.jma.go.jp$weekpath"
        w3m -dump "$url" | LANG=C cut -c 25-100 | $(pager)
    }
}

# tenki-yohou
#
# weather place
#
eval todouhuken=\$todoufuken_$yohouplace
eval daypath=\$daypath_$yohouplace
eval weekpath=\$weekpath_$yohouplace

[ "$todouhuken" ] || 
    { error_msg="error: no such place: $yohouplace"; error; usage; exit 0; }

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

[ yes = "$information" ] && { weather-information; exit 0; }

[ yes = "$dayyohou" ] && day_tenki-yohou

[ yes = "$weekyohou" ] && 
    { [ yes = "$dayyohou" ] && echo; week_tenki-yohou; }
