#!/bin/sh
# Compose from Image files to www page HTML sets
# $Id: img2www,v 2.1 2006/03/05 02:47:27 yuuji Exp $
# (c)1997-2000 by HIROSE Yuuji [yuuji@gentei.org]

SCALE=0.2
SCALE="-xsize 60 -ysize 45"
INDEX=index.html
noindex=0
nomini=0
tojis=/usr/local/bin/nkf
# bg='bgcolor="white"'
bg=white
back=none
fg=black
TABLE=''
TB=0
TW=200
TW2=300
CNT=0
CJPEG=cjpeg
DJPEG=djpeg
CONVERT=convert
cssfile=img2www.css
paragraphmargin=0.5ex
formhead=''
formtail=''
perl=`which perl`
cgi2args="./cgi2args.cgi"
timesort=''


while [ $# -ne 0 ];
do
    case "$1" in
	-s)     shift;  SCALE=$1 ;;
	-4)	SCALE="-xsize 48 -ysize 36" ;;	# ǥեȤˤ
	-6)	SCALE="-xsize 60 -ysize 45" ;;
	-1)	SCALE="-xsize 120 -ysize 90" ;;
	-ix)	shift;  INDEX=$1 ;;
	-ni)	noindex=1 ;;			# index.htmlʤ
	-nm)	nomini=1 ;;			# mini򥹥å
	-t)	TABLE='   ' ;;			# <table>⡼
	-tb)	shift; TB=$1 ;;			# <table> border
	-tw)	shift; TW=$1 ;;			# <table>¦WIDTH
	-tw2)	shift; TW2=$1 ;;		# <table>¦WIDTH
	-T)	TABLE='   '
		SCALE="-xsize 120 -ysize 90" ;; # table浤ڥС
	-time)	timesort=1 ;;			# 祽
	-bg)	shift; bg="$1" ;;
	-back)	shift; back="url($1)" ;;
	-fg)	shift; fg="$1" ;;
	-h)	shift; heading="$1" ;;		# htmlκǽH1
	-css)	shift; cssfile="$1" ;;
	-pm)	shift; paragraphmargin="$1" ;;
	-to)	shift; toaddress="$1" ;;
	-me)	shift; maintainer="$1" ;;
	-order)
	  if [ -x $cgi2args ]; then
	    formhead="<form method=\"GET\" action=\"$cgi2args\">
ʤΥᥤ륢ɥ쥹: <input name=\"email\" size=\"40\" maxlength=\"40\"><br>
<input type=\"hidden\" name=\"execcmd\" value=\"./order.pl\">
<input type=submit value=\"ʸ\"><input type=reset value=\"ľ\">"
	    formtail="</form>"
	    TABLE='   '				# -T ·뤳
	    SCALE="-xsize 120 -ysize 90"
	  else
	    echo "ȥǥ쥯ȥ cgi2args.cgi ɬפǤ."
	    echo "(http://www.gentei.org/software/ ˤ)"
	  fi
	  ;;
	*)
		if [ -s "$1" ]; then
			files="$files $1"
		fi ;;
    esac
    shift
done

if [ "$files" = "" ]; then
	myname=`basename $0`
	cat<<_EOU_
ʤ󤫥եꤷƤ͡
$myname jpgե򤬤äɤ߹ǡͥե
<img src..>ɤ߹ index.html ޤͥ
åȸ礭jpgФ褦ʥ󥯤ĥäƤޤ

$myname λȤ:
   $myname [ץ] *.jpg
   ץϰʲ̤(ϥǥե)
	-s Ψ		JPG饵ͥȤΨ
			-s 0.5 Ȥȸ0.5ܡ
			-s "-xsize 120 -ysize 90" Ȥ 120x90 ˤʤޤ
	-4		-s "-xsize 48 -ysize 36" Ʊ
	-6		-s "-xsize 60 -ysize 45" Ʊ
	-1		-s "-xsize 120 -ysize 90" Ʊ
	-ni		index.html ʤ(No Index)
	-t		tableĶȤǤФ
	    -tb		tableborder(ǥե=0)
	    -tw		tableκ¦(̿ʬ)($TW)
	    -tw2 	tableα¦(ʸʬ)($TW2)
	-T		-t -1 Ʊθ浤ڥץ
	-time		*.jpg ե뷲¤ѤƤHTML
	-bg		htmlեbgcolor
	-back		htmlեbackground
	-fg		htmlեtext
	-css		cssե̾(img2www.css)
	-pm		cssѥ饰դξ岼ޡ($paragraphmargin)
	-order		̿ʸե ʲ2ץʻ
	    -to	ADDR	ʸԤemailɥ쥹ADDR˻
	    -me ADDR	ʸڡδԤΥɥ쥹ADDR˻


$myname ˤ netpbm  jpeglib(djpeg)ɬפǤUNIXǲ򤹤ˤ
礫ʤġʤΤФ˥󥹥ȡ뤷ޤ礦
_EOU_
	exit 0;
fi

catimg () {
    case $1 in
      *.jpg|*.jpeg)
	$DJPEG $1 ;;
      *.png)
        $CONVERT $1 PNM:- ;;
    esac
}

readcomment () {
    case $1 in
      *.jpg|*.jpeg)
	rdjpgcom $1 ;;
      *.png)
        identify -ver $1 | awk '/Comment:/{print $2}' ;;
    esac
}

if [ $noindex -eq 0 ]; then
	if [ ! -s $INDEX ]; then
	  init=1
	  echo "<html>"					>>$INDEX
	  echo "<head><title>No TITLE</title>"		>>$INDEX
	  echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssfile\">" >>$INDEX
	  echo "</head>"				>>$INDEX
	  echo ""					>>$INDEX
	  echo "<body>"					>>$INDEX
	  [ "$heading" ] && echo "<h1>$heading</h1>"	>>$INDEX
	fi
	[ "$TABLE" ] && echo "$formhead<table border=\"$TB\">"|$tojis >>$INDEX
fi
if [ "$timesort" ]; then
    files=`ls -tr $files`
fi
for i in $files
do
	case $i in
	    # *.gif)		# No more GIF!!
	    # cat=giftopnm ;;	# No more Unisys!!!
	    *.jpg|*.jpeg)
		cat=djpeg ;;
	esac
	case $i in
	    *mini*)
		echo "Skipping $i" ;;
	    *)
		minibase=`basename $i|sed 's/\(.*\)\..*/\1/'`
		minifile=`dirname $i`"/tn_${minibase}.jpg"
		html=`dirname $i`"/$minibase.html"
		imglink=`basename $i`
		echo mini=$minibase, mf=$minifile
		title=`readcomment $i`
		if [ "$title" = "" ]; then
			title="$i"
		fi
		# get image size in pixel
		size=`catimg $i|pnmfile -| \
		 sed 's/[^0-9]*\([1-9][0-9]*\) by \([0-9]*\).*/width="\1" height="\2"/'`
		if [ ! -f $minifile -o $nomini -eq 0 ]; then
		 catimg $i | pnmscale $SCALE | ppmquant 128 |\
		    $CJPEG -progressive > $minifile
		fi
		minisize=`$DJPEG $minifile|pnmfile -| \
		 sed 's/[^0-9]*\([1-9][0-9]*\) by \([0-9]*\).*/width="\1" height="\2"/'`
		if [ $noindex -ne 1 ]; then
		  if [ "$TABLE" ]; then
		    echo " <tr>"			>>$INDEX
		    echo "  <td width=\"$TW\">"		>>$INDEX
		  fi
		  echo "$TABLE<a href=\"$html\">"	>>$INDEX
		  echo "$TABLE<img src=\"$minifile\" align=\"middle\" alt=\"Photo\"">>$INDEX
		  echo "$TABLE     $minisize></a>" >> $INDEX
		  if [ "$TABLE" ]; then
		    echo "  </td>"			>>$INDEX
		    CNT=`expr $CNT + 1`
		    echo "	<td width=\"$TW2\">"	>>$INDEX
		    if [ "$formhead" ]; then
		      echo "	<input type=checkbox name=\"$i\">$i" >>$INDEX
		    else 
		      echo "	 $title<br>"		>>$INDEX
		      echo "	 Ȥ$CNT"|$tojis>>$INDEX
		    fi
		    echo "	</td>"			>>$INDEX
		    echo " </tr>"			>>$INDEX
		  fi
		fi
		echo "<html>"				 >$html
		echo "<head><title>$title</title>"	>>$html
		echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssfile\">" >>$html
		echo "</head>" 				>>$html
		echo "<body>"				>>$html
		echo "<p class=\"c\" align=\"center\">"	>>$html
		echo "<img src=\"$imglink\" alt=\"photo\" $size>" >>$html
		echo "</p><hr><p class=\"c\">"		>>$html
		echo "<!-- ιԤäƤ˥ -->"|$tojis >>$html
		echo "<!-- 󤻤ɤ <p class=\"l\"> Ѥ -->" | $tojis >>$html
		echo "</p>"				>>$html
		echo "</body>"				>>$html
		echo "</html>"				>>$html
		;;
	esac
done
cssfile="`dirname $i`/$cssfile"
cat>$cssfile <<EOF
body {color: $fg; background: $bg $back;}
p.l {margin: $paragraphmargin $paragraphmargin;}
p.c {margin: $paragraphmargin $paragraphmargin; text-align: center;}
/* ޤǤºݤ˻ѤƤ. ʲϥץ. */
/* ȤаĤطʤpinkˤä... */
body.hoge {background: pink;}
/* ƤʤΤɲøpinkˤhtml body  <body class="hoge"> ˤOK */
EOF

##mkorder function
mk_order () {
    perl=`which perl`
    if [ -x /usr/lib/sendmail ]; then
	sendmail=/usr/lib/sendmail
    elif [ -x /usr/sbin/sendmail ]; then
	sendmail=/usr/sbin/sendmail
    else
	sendmail=sendmail
    fi
    tojis=`which nkf`
    while [ "$toaddress" = "" ]; do
	echo -n "ʸͤemailɥ쥹: "
	read toaddress
    done
    while [ "$maintainer" = "" ]; do
	echo -n "Υڡδ(¿ʬʤ)emailɥ쥹: "
	read maintainer
    done
    cat>order.pl<<EOF
#!$perl

@list='';
\$sendmail='$sendmail';
\$replyto='$toaddress';
\$mailto='';
\$mailtainer='yuuji@itc.keio.ac.jp';
\$tojis='$tojis';

while (\$ARGV[0] =~ /^-/) {
    if (\$ARGV[0] =~ /^-email/) {
	\$mailto = \$ARGV[1];
    } elsif (\$ARGV[1] =~ /on/i) {
	push(@list, \$ARGV[0]);
    }
    shift; shift;
}

print "<html><head><title>Accepted</title><head><body>";

if (\$mailto) {
    open(MAIL, "|\$tojis | sendmail \$mailto");
    print MAIL "To: \$mailto
From: Photo Ordering System <\$mailtainer>
Subject: Confirmation of your order
Reply-to: \$replyto

ʲμ̿ʸޤʲΥꥹȤǤäƤ뤫ɤǧ
ɤСΥᥤΤѤ \$replyto ˤ
";

    print MAIL join("\n", @list);
    close(MAIL);
    print "ʸꥹȤޤΤǹäƤ뤫ɤǧƤ";
    
} else {
    print "ᥤ륢ɥ쥹ɬƤ";
}
print "</body>\n</html>";
EOF
    chmod +x order.pl
}

if [ "$noindex" -eq 0 ]; then
	if [ "$TABLE" ]; then
	  echo "</table>"				>>$INDEX
	  if [ "$formhead" ]; then
	    $tojis<<EOF>>$INDEX
<input type=submit value="ʸ">
$formtail
EOF
	  mk_order
	  fi
	fi
	if [ "$init" -ne 0 ]; then
	  echo ""					>>$INDEX
	  echo "</body>"				>>$INDEX
	  echo "</html>"				>>$INDEX
	fi
fi
