#!/bin/sh
# h-bmp - Send BMP image file to Feel H"/Air H" handyphone system
# (C)2002, 2003 by HIROSE Yuuji [yuuji@gentei.org]
# $Id: h-bmp,v 1.8 2009/10/05 11:23:51 yuuji Exp $
# Last modified Sat Jan 15 03:40:55 2005 on firestorm
# Update Count: 72
#
# ★利用法
# h-bmp 画像ファイル 送信先……
# 
# 「画像ファイル」を Treva 規格(なにゃそれ)に則ってBMPに直したものを指定
# したアドレスに送る。送信先はスペースで区切って複数書ける。
# 起動すると「1行メッセージ」を求めて来るのでなんか入れてリターンを押す。
#
# オプションは -t と -s と -m がある。くわしくはこのスクリプトを引数なし
# で起動すると出てくる。
# 
# % h-bmp
# 
# ★インストール
# nkf と convert(ImageMagick) と mewencode が必要。
# このスクリプトを h-jpg という名前にしてインストールするとデフォルトの
# 変換画像フォーマットがJPEGになる。
# 
# % chmod +x h-bmp
# % ln -s h-bmp h-jpg
# 
# ちなみに ImageMagick は ftp://ring.gr.jp/graphics/ImageMagick/ にある
# ImageMagick-バージョン.tar.bz2 を持って来てインストール、
# mewencode は ftp://ftp.mew.org/pub/Mew/ にある mew-バージョン.tar.gz
# を持って来てインストールする。
# 
# ★用途
# ないよなあ。
# 最近多い「当社のこのWebページの画像をi-modeで表示すると半額です!」
# とかいうの、「貴様の会社はどこもの手下か!」と、むかつくのに反抗するた
# め? いえいえ、「好きな画像をまいらぶりーえっぢちゃんの壁紙にする」ため?
# いえいえ、単にえっぢ端末にBMPを送って表示できるか実験してみたらあっさ
# りできちゃったんで作ってみたくなっただけっす。ほなさいなら。
# 
# ★こぴー&改造
# 御自由にどうぞ。する価値無いけど。はい。

revision=`expr "$Revision: 1.8 $" : '.*: \([0-9.]*\)'`

myname=`basename $0`
sz="128x96"
format="BMP"
angle='0'
case $0 in
    *[jj][Pp][Gg]|*[Jj][Pp][Ee][Gg])
	format=jpg ;;
    *bmp|*BMP)
	format=bmp ;;
    *mid|*MID)
	format=mid ;;
esac

while [ x"$1" != x"" ]; do
    case "$1" in
	-t) shift; recipients="$recipients $1" ;;
	-s) shift; sz="$1"	;;
	-m) shift; comment="$1" ;;
	-[Rr][Ll])
	    sz=132x162 ;;
	-[Rr][Mm])
	    sz=132x96 ;;
	-[Rr][Ss])
	    sz=96x84 ;;
	-[Ll][Ee]*)
	    sz=120x144 ;;
	-[Aa][Hh]*)
	    sz=240x320 ;;
	-j) format=jpg ;;
	-q) shift; quality="-quality $1" ;;
	-b) format=bmp ;;
	-r) raw=1 ;;
	-R) shift; angle="$1" ;;
	*)  break ;;
    esac
    shift
done
rotate="-rotate $angle"

if [ x"$1" = x"" ]; then
  echo Usage: $myname [Options] 'ImageFile(s...)'
  echo Options are...
  cat  <<_EOF_ | nkf
	-t address	Send mail to \`address'. Multiple -t available.
			address さん宛に送る。-t は何個でも可。
	-s XxY		Resize image size to XxY
			画像サイズを XxY にするのだ
	-R #		Rotate image #-degree in clock-wise
			時計まわりに #度 画像を回転
	-m 'Message'	Set one-line comment to 'Message'
			1行コメントを 'Message' にするのだ
	-RL		Equivalent to '-s 122x162'
			-s 122x162 とおなじ(Sanyo RZ-J700 Large)
	-RM		Equivalent to '-s 96x72'
			-s 76x72 とおなじ(Sanyo RZ-J700 Medium)
	-LE		Equivalent to '-s 120x144'
			-s 120x144 とおなじ(Sanyo Leje; H-SA3001V Large)
	-b		Convert to BMP (h-bmp defaults to this)
			BMPにする(h-bmpのデフォルト動作)
	-j		Convert to JPEG (h-jpg defaults to this)
			JPEGにする(h-jpgのデフォルト動作)
	-q QUALITY	JPEGにする場合のquality(デフォルトは75)
	-r		Raw file.  convertせずにそのまま送る。
Ex)
% $myname -s 96x72 -t yuuji@gentei.org hoge.jpg
 -> Send $format-ized hoge.jpg to yuuji@gentei.org after resized to 96x72 
    yuuji@gentei.org に${format}化した hoge.jpg を96x72に直したのを送るのだ

% $myname -j -m '翔子写真だ' shoko.gif evidence.png
 -> View jpg-converted image files with message encoded with base64 format.
    shoko.gif, evidence.png を $sz にリサイズしてJPGに直してエンコードした
    ものをページャで見る

% $myname -r -t yuuji@gentei.org hoge-*.mid
 -> Send all *.mid files to yuuji@gentei.org encoded with base64 format.
    Note that MIDI files should be sent without conversion(-r).
    カレントディレクトリにある *.mid ファイルをbase64エンコードして送信する。
    MIDIは画像変換しないので -r オプションが必要。
_EOF_
  exit 0
fi



form () {
    if [ x"$recipients" != x"" ]; then
	for r in "$recipients"; do
	    echo To: "$r"
	done
    fi
    cat <<_EOF_
Mime-Version: 1.0
Content-Type: Multipart/Mixed; Boundary="PmailDX00"
Content-Transfer-Encoding: 7bit
X-Mailer: h-bmp version $revision

--PmailDX00
Content-Type: Text/Plain;charset=iso-2022-jp

_EOF_

    echo "$comment" | nkf
    # Send images
    i=1
    for img in "$@"; do
	base=`basename $img`
	if [ "$raw" ]; then
	    fn=`date +%d%H%M-$i`-$base
	else
	    fn=`date +%y%m%d%H%M-$i.$format`
	fi
	i=`expr $i + 1`
	cat <<_EOF_

--PmailDX00
Content-Type: Application/Octet-stream; Name="$fn"
Content-Transfer-Encoding: Base64

_EOF_
	if [ $raw ]; then
	    cat $img
	else
	    convert $quality $rotate -geometry $width -crop $sz $img $format\:-
	fi | mewencode
	#echo ''
    done
    echo '--PmailDX00--'
}

width=`expr $sz : '\([0-9]*\)x'`
if [ x"$comment" = x"" ]; then
    echo "1行メッセージを入力して下さい"|nkf
    read comment
    echo '送信中です...'
fi

if [ x"$recipients" = x"" ]; then
    send="${PAGER:-less}"
else
    send="sendmail -t"
fi

form "$@" | $send


# Local variables:
# fill-prefix:	"# "
# End:
