#! /bin/sh
# @(#) mkindex.sh 1.2@(#)
#
# mkindex.sh -- create a collection of picture galleries.
#

pnmthumb()
{
    pnmscale -xysize 200 200 > $$
    pbmmake -white 200 200 | \
              pnmcomp -align=center -valign=middle $$ | \
              cjpeg -optimize -quality 25 > $1
    rm -f $$
}

pngthumb() {
    pngtopnm "$1" | pnmthumb $2
}


jpegthumb() {
    djpeg "$1" | pnmthumb $2
}


pngscreen() {
    pngtopnm "$1" | pnmscale -xysize 800 600 | cjpeg
}


jpegscreen() {
    djpeg "$1" | pnmscale -xysize 800 600 | cjpeg
}


pngsize() {
    file "$1" | awk -F: '{print $2}' | sed -ne 's/^.*,\(.*\) x \(.[^,]*\),.*$/\1 \2/p'
}


jpegsize() {
    jhead "$1" | sed -ne 's/Resolution *: *\([^ ]*\) *x *\([^ ]*\)/\1 \2/p'
}


header() {
    if [ "$needheader" ]; then
	test -r .title && title=`cat .title`
	
	cat - << EOF
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional //EN">
<HTML>
<HEAD>
   <meta name="GENERATOR" content="http://www.pell.portland.or.us/~orc/Code/automat">
   <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
   <TITLE>${title:-Photo Gallery}</TITLE>
</HEAD>
<BODY BGCOLOR=white>
EOF
	echo "<DIV ALIGN=JUSTIFY>"
	echo "<table align=center width=\"80%\">"
	if [ -r .header ]; then
	    echo "<tr><td colspan=4>"
	    cat .header
	    echo "<HR>"
	    echo "</td></tr>"
	fi
	unset needheader
    fi
    if [ $col -eq 0 ]; then
	echo "<tr>"
	col=0
    elif [ $col -eq 4 ]; then
	echo "</tr><tr>"
	col=0
    fi
}

footer() {
    if [ ! "$needheader" ]; then
	test "$col" -gt 0 && echo "</tr>"
	if [ -r .footer ]; then
	    echo "<tr><td colspan=4><HR>"
	    cat .footer
	    echo "</td></tr>"
	fi
	echo "<tr><td colspan=4 align=right>"
	echo "<hr>"
	echo "<a href=\"http://www.pell.portland.or.us/~orc/Code/automat/\">"
	echo "<img src=\"/icons/automat.jpg\" valign=center align=right alt=\"mkindex.sh\">"
	echo "<font size=\"-2\">album created<br>`date +'%b %e, %Y'`</font></a>"
	echo "</td></tr></table>"
	echo "</DIV>"
	echo "</BODY></HTML>"
    fi
}

append() {
    if [ "$images" ]; then
	images="$images,\"$1\""
	big="$big,\"$2\""
    else
	images="\"$1\""
	big="\"$2\""
    fi
}

picture() {
    echo "<center>"
    SEP=
    if [ "$WITH_PHP" ]; then
	echo "<a href=\"view.php?next=$1\">"
    elif [ "$2" = "+" ]; then
	echo "<a href=\".lg.$1.jpg\">"
	SEP="</a><a href=\"$3\">"
    else
	echo "<a href=\"$3\">"
    fi
    echo "<img border=1 src=\".$1.jpg\">"
    echo "$SEP$4</a>"
    echo "</center>"
}


MKINDEX=`basename $0`
if ! which $MKINDEX >/dev/null 2>/dev/null; then
    # set the path to include $0, if possible.
    D=`dirname $0`
    case "$D" in
    /*)	PATH="$PATH:$D" ;;
    *)  PATH="$PATH:`cd $D;pwd`" ;;
    esac
fi

case "$MKINDEX" in
*updindex*)	UPDATE=1;;
esac

needheader=1
thumbno=0
col=0

images=
big=

exec 1>$$.html

for x in "$@"; do
    ST=`file "$x" |  awk -F: '{print $2}' `
    URL=`echo "$x" | sed -e 's/ /\%20/g' -e 's/#/%23/g'`
    case "$ST" in
    *JPEG*|*PNG*|*directory*)
	    case "$ST" in
	    *JPEG*)
		    jpegthumb "$x" .$thumbno.jpg || continue;
		    header
		    T=`jpegsize "$x"`
		    h=`echo $T | awk '{print $1}'`
		    w=`echo $T | awk '{print $2}'`
		    if [ "$h" -a "$w" ]; then
			echo "<td bgcolor=grey>"
			if [ \( "$h" -gt 800 \) -o \( "$w" -gt 600 \) ]; then
			    jpegscreen "$x" > .lg.$thumbno.jpg
			    append .lg.$thumbno.jpg "$URL"
			    key="+"
			else
			    append "$URL" ""
			    key="-"
			fi
			picture $thumbno $key "$URL" "$h x $w JPEG"
			echo "</td>"
		    fi
		    thumbno=`expr $thumbno + 1`
		    ;;
	    *PNG*)
		    pngthumb "$x" .$thumbno.jpg || continue;
		    header
		    T=`pngsize "$x"`
		    h=`echo $T | awk '{print $1}'`
		    w=`echo $T | awk '{print $2}'`
		    if [ "$h" -a "$w" ]; then
			echo "<td bgcolor=grey>"
			if [ \( "$h" -gt 800 \) -o \( "$w" -gt 600 \) ]; then
			    pngscreen "$x" > .lg.$thumbno.jpg
			    append .lg.$thumbno.jpg "$URL"
			    key="+"
			else
			    append "$URL" ""
			    key="-"
			fi
			picture $thumbno $key "$URL" "$h x $w PNG"
			echo "</td>"
		    fi
		    thumbno=`expr $thumbno + 1`
		    ;;
	    *directory*)
		    test "$UPDATE" -a -f $x/index.html || (cd $x; $MKINDEX *)

		    if [ -f "$x"/.0.jpg ]; then
			header
			echo "<th><a href=\"$URL\">"
			echo "<img border=1 src=\"$URL/.0.jpg\">"
			echo "<br>[$x]</a><br>"
			if [ -r "$x"/.ct ]; then
			    CT=`cat "$x"/.ct`
			    if [ "$CT" -eq 1 ]; then
				echo "$CT photo"
			    else
				echo "$CT photos"
			    fi
			fi
			echo "</th>"
		    elif [ -f "$x"/index.html ]; then
			header
			echo "<th><a href=\"$URL\">[$x]</a></th>"
		    else
			col=`expr $col - 1`
		    fi
		    ;;
	    esac
	    col=`expr $col + 1`
	    ;;
    esac
done

footer

if [ -s $$.html ]; then
    rm -f index.html
    mv $$.html index.html

    if [ "$WITH_PHP" ]; then
	# build a picture viewer
	exec 1>view.php

	echo '<HTML>'
	echo "<!-- view.php created by mkindex.sh on" `date` " -->"
	echo '<?php'
	echo "    \$images = array($images);"
	echo "//  \$big    = array($big);"
	echo "    \$total  = $thumbno;"
	cat - << \EOF
$curr   = $HTTP_GET_VARS['next'];
$image  = $images[$curr];
 ?>
<HEAD><TITLE><?php echo $image; ?></TITLE></HEAD>
<BODY>
<?php
    if ($curr < $total - 1) {
	echo "<FORM METHOD=GET ACTION=view.php>";
    }
    echo "<table align=center>\n";

    echo "<tr><td align=center><img src=\"$image\"></td></tr>\n";

    if ($curr < $total - 1) {
	echo "<tr><td align=center>\n";
	printf("<INPUT TYPE=HIDDEN NAME=\"next\" VALUE=\"%d\">\n", $curr+1);
	echo "<INPUT TYPE=SUBMIT VALUE=\"view next image\">\n";
	echo "</td></tr>\n";
	echo "</table></FORM>\n";
    }
    else
	echo "</table>\n";
 ?>
</BODY></HTML>
EOF
    fi
fi
rm -f .ct
echo "$thumbno" > .ct
