#!/bin/sh
#
# thumbnail_index - make an index.html file with image thumbnails
#
# Run this script in a web directory consisting of all or mostly image
# files.  It'll create an index.html of thumbnail images, putting
# the thumbnail files in a hidden subdirectory.
#
# It avoids unnecessary work by comparing the dates on already existing
# thumbnail files and not re-creating them if the corresponding original
# has not changed.
#
# It also refuses to overwrite an index.html file not created by this script.
#
# (C) 1995,1996 by Jef Poskanzer <jef@acme.com>.  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.
#
#
# Enhancement by Yoichi Hariguchi.
#
# 1. Slide Show (01/04/2005)
# 2. Back, Forward, and Back-to-Thumbnails buttons
# 3. Captions. Create .captions file in the same directory in a web
#    directory consisting of all image files. Each line of the .caption
#    file must have the following format:
#
#      <filename><TAB><caption>
# 
#    Note that the separator between the file name and the caption must
#    be the TAB character (just like makefile ;-) (03/30/2003)
# 4. If you have a small size picture directory (e.g. for access via web)
#    in addition to the original size picture directory, this program
#    can make a small picture a link to the original picture.
#    The name of the default original picture directory is "raw",
#    but it can be changed with the "-d" command line option.
#    You can use "http://www.hariguchi.org/software/mksmallpic.sh"
#    to make small pictures from the original size pictures. (03/09/2006)
# 5. If you have a file called ".comments" in the current directory,
#    the content of the file is included right below the title. You can
#    write any HTML statements in the file. (04/04/2006)
#    
#
# Output Examples are available at http://www.aya.us/
#
# $Id: ti,v 1.19 2006/04/05 00:17:49 yoichi Exp $
#


# Defaults.
recurse=no
size=70
bigsize=1280
colsperrow=5
rowspertable=10
indexname=index.html
captionname=.captions
commentname=.comments
title="Index of `pwd | cut -d'/' -f 4-`/"
usage="usage: `basename $0` [-r] [-s size] [-c cols] [-i indexname] [-d dir] [-t title]"
refresh=5
ss_html="_ss.html"              # slide show html file
suffixes="$ss_html .html"
prefix="s_"                     # prefix of small pictures
origPicDir="raw"                # directory for original size pictures

# Parse args.
while true ; do
    case "$1" in

	-r )
	recurse=yes
	shift
	;;

	-s )
	shift
	if [ $# -eq 0 ] ; then
	    echo $usage >&2
	    exit 1
	fi
	size="$1"
	shift
	;;

	-c )
	shift
	if [ $# -eq 0 ] ; then
	    echo $usage >&2
	    exit 1
	fi
	colsperrow="$1"
	shift
	;;

	-i )
	shift
	if [ $# -eq 0 ] ; then
	    echo $usage >&2
	    exit 1
	fi
	indexname="$1"
	shift
	;;

	-d )
	shift
	if [ $# -eq 0 ] ; then
	    echo $usage >&2
	    exit 1
	fi
	origPicDir="$1"
	shift
	;;

	-t )
	shift
	if [ $# -eq 0 ] ; then
	    echo $usage >&2
	    exit 1
	fi
	title="$1"
	shift
	;;

	-* )
	echo $usage >&2
	exit 1
	;;

	* )
	break
	;;
    esac
done
if [ $# -ne 0 ] ; then
    echo $usage >&2
    exit 1
fi

subdir=.thumbnails
nindexname=/tmp/tinind.$$
stamp='This file produced by thumbnail_index - do not edit.'
cwd=`pwd`
makefile=/tmp/timake.$$
infoscript=/tmp/tiinsc.$$
rm -f $nindexname $makefile $infoscript

if [ -f "$indexname" ] ; then
    if egrep -s "$stamp" "$indexname" > /dev/null 2>&1 ; then
	echo > /dev/null # Why the fuck doesn't an empty command work??
    else
	echo "thumbnail_index: $indexname exists, not produced by this script" >&2
	exit 1
    fi
fi

# Make temporary script to extract info.
cat > $infoscript << 'EOF'
#!/bin/sh

file="$1"
what="$2"
thumb="$3"

kb=`wc -c < "$file" | awk '{print int(($1+1023)/1024)}'`
# (Slower but more portable than using ls.)

set junk `"$what" "$file" | pnmfile` ; shift
width=$4
height=$6

set junk `djpeg "$thumb" | pnmfile` ; shift
twidth=$4
theight=$6

echo "$kb" "$width" "$height" "$twidth" "$theight"
EOF
chmod 755 $infoscript

if [ ! -d $subdir ] ; then
    mkdir $subdir
fi

(
    if [ ! -f $subdir/.parent.gif ] ; then
	(
	    cd $subdir
	    cat << 'EOF' | uudecode
begin 644 .parent.gif
M1TE&.#EA%``6`(```````/___R'Y!`$```$`+``````4`!8```(VC(^I".L'
MVENQQFFLKG3[FWVB-'Y0N9UH(X6,YC))&UA8[4VCPXI/R</E?J:?T(9)W7JW
%YJ$``#NL
`
end
EOF
    )
    fi
    if [ ! -f $subdir/.slideshow.gif ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .slideshow.gif
M1TE&.#=A2@`8`+,``````)65E8>'AU]?7U%141L;&\S,S`T-#;Z^OK"PL'IZ
M>FQL;$1$1#8V-B@H*````"P`````2@`8```$X]#(2:N]..O-N_]@*(YD:9YH
MJJYLZ[XIT@"T5!O'8=V;3`,VH,L!"%!X%V2&:)PH533$47A#$`]5`HTAG7:#
MAH9#(4$PME*L=*"3:+\3;*%IN-UFB\1M`%@(``P5<G0T!']"6@H*``0&?`L&
M!0!D=0,8/Y9U5)M@FIU3`)EVG)YZ!P@T.HL)&&:CG:^:/T(4KJ2Q=P`S6`H.
M@1JXMZ3`PK!"AT``6*S`!<823(=NH1L`SI[89P)_OSB`!D3=.S0.3L-7=65:
@/Q4_Y<\&MEP3?$:K,/CY^OO\_?[_``,*'$BP8(<(`#L`
`
end
EOF
    )
    fi
    if [ ! -f $subdir/.back.gif ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .back.gif
M1TE&.#=A&``8`/,``++`W`("`A(:$GBA;H:M?;+&KB8R(N?MYDER00``````
M`````````````````````"P`````&``8```$31#(2:N]..O-N_]@$("46(RD
M>!QG&*QKVZEPC&J!4-<R9NX\`RZ`0`R.A&1R<,N(B@-"=-D<0J72ZN9I/&HY
1SP'B.R.2/2*2>LUNNS,1`#L`
`
end
EOF
    )
    fi
    if [ ! -f $subdir/.forward.gif ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .forward.gif
M1TE&.#=A&``8`/,``++`W`("`AH:&G>=;8BN@)>WBYZZDNGOZ#Q9-@``````
M`````````````````````"P`````&``8```$2Q#(2:N]..O-N_]6$(#A(9)3
M<*PGJ:[L^+UP[-'U*LB:D.>&%B9@(!B/A,$`(;P0D<EE<U@T*IF\3:`0Q8("
/2:]+C)JBSNBT>HV)```[
`
end
EOF
    )
    fi
    if [ ! -f $subdir/.index.gif ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .index.gif
M1TE&.#EA&``8`,(``````,S,S)65E?___\S,_____________RP`````&``8
M```#2!BZW/XPRDEK!!BTK"4@8*=\H4<,A!A\J.JPZ3J>L;N0\0WF$*?ZEJ!P
A2!QE-D=32;?LT7Y/4TNVBMIFS6KVQ6$`B^"P>`Q.```[
`
end
EOF
    )
    fi
    if [ ! -f $subdir/.pause.gif ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .pause.gif
M1TE&.#=A,``8`)$``````.[N[O___P```"P`````,``8```"6(R/J<OM#Z.<
MM-J+L]Z\4P""7B4&Y1B59P@<:FN$"`N]L0O;9A[G<,/Z[62[7D^H`PJ-Q>;J
E27.<<,SDD)E:.GW5ZD^;F.*(0^B,C$JKU^RV^PV/3P3R1@$`.P``
`
end
EOF
	)
    fi
    if [ ! -f $subdir/.censored.png ] ; then
	(
	    cd $subdir
        cat << 'EOF' | uudecode
begin 644 .censored.png
MB5!.1PT*&@H````-24A$4@```4````"@`0,```!';2??````!E!,5$7___\`
M``!5PM-^`````7123E,`0.;89@```PU)1$%4>)SMUSUNVS`4`.`G"`@70UHS
M$.(5,C*H8%W%O0&+#%4!HU(NT%X@!^G(PD.WGH%!AJXLNG!0S/+1<6O3D4@'
M'=I";S%,?[:LI\>?!S#'''.\)$J9"!N="*U)<YENTV`N$Z^<]XF0)+JS8*XN
MZZMWLHA`":6AV^O'&,QMSP:V;1X5G8:95<N:?6WN=`1"IWG%BB45/`(STQ95
M4=,V!F$M"EIPRD4,U@X"=R\Q2'>01LOM"58J$1812*%N*<+8E*B@K6N$)`)9
MIIGQL)^&I56E1ICW^20D5A+I?S%6FMO]-U)KN(!(09X-*<0*\GQXF0:C=7LV
MO$J%<_Q]\8?+H;3;5&CY?OX=+GPG4Y(<PXM1Z.;>(233<.**+X;[M>P`ALO;
M#C8]K`+8G$)#,MO#-RER98WB*^U67S\4P.N!Y#]ZL$J4!N&-<0<2/Q3`5UM"
M-@YJP89.*]Z9SH`?"F#S@9"L!R-$71-PD+<M^*$`+@N"]Z"%X`N$:ZX0GJ8'
MH=NV%!<"$+;\WOU'$NYD!=0%R8<#**Z_*/!#87H*EPL/U0YB9OS0*01V!`?,
M/'L6NN=U`)7?)$[N&F%^!.4"R,D9R\/%#NK%TR]2\$,A+$F)EQ:B]0D78-;@
MAT)(2(EWK44S$'>>%'BF]$.GD+B-SA4%TP0Z![$HR#:`"X28$"L%D1?0?!+`
ML,S"A/^*L/Y&0R6Z&<[Q3\?KSW+DD]7QV_LQ6`;KX"ADJ;!+A285ZJ`]&8.9
M2H3[WK6.P:=5\/>2-`I=@GJW30[8;^8:UF.P=`E2-X;=8;]9#ID=@^XX;_5;
MLZ38;[)M/@6U[BA?8+]9560S!BO(I'Y/.6"_R2LR>C,4LHU:TZ;'?I.ORPF8
M?Y0M90K[S3>FF((66EH:;/HZ-0D-"`H[")-0(O0=;#L!*WCH+QWT_:;(QV&1
M/4@NZ(IBO]FZK4D]R]PC)/?:".K:3==O+AG9C$%B;]V!AW8&^\T&'^%M_RQT
MR>F4H(W&?A.+XOOHG'&GM2M:*NPWW1'F_?@"$(9,A7/,,<?_'S\!CEEP*RJ1
.%%4`````245.1*Y"8((`
`
end
EOF
	)
    fi
    cat << EOF
<!-- $stamp -->
<HTML><HEAD><TITLE>$title</TITLE></HEAD>
<BODY>
<H2>$title</H2>
EOF
    if [ -f "$commentname" ]; then
        cat "$commentname"
        echo "<br><br><hr>"
    fi
    cat << EOF

<A HREF="$subdir/slideshow.html"><img src="$subdir/.slideshow.gif"></a>
<P>
<TABLE><TR>
<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><A HREF="../index.html"><IMG BORDER=0 WIDTH=20 HEIGHT=22 SRC="$subdir/.parent.gif"></A></TD></TR><TR><TD ALIGN=CENTER><SMALL><A HREF="../index.html">Parent dir</A></SMALL></TD></TR></TABLE></TD>
EOF
    row=0
    col=1
    prev=''
    html=''
    for file in * ; do
	if [ "$col" -ge "$colsperrow" ] ; then
	    col=0
	    row=`echo $row + 1 | bc`
	    if [ "$row" -ge "$rowspertable" ] ; then
		row=0
		echo "</TR></TABLE><TABLE><TR>"
	    else
		echo "</TR><TR>"
	    fi
	fi
	case "$file" in
	    "$indexname")             what=SKIP ;;
	    *.gif)    ext=.gif ;    what='giftopnm' ;;
	    *.GIF)    ext=.GIF ;    what='giftopnm' ;;
	    *.jpg)    ext=.jpg ;    what='djpeg' ;;
	    *.JPG)    ext=.JPG ;    what='djpeg' ;;
	    *.jpeg)   ext=.jpeg ;   what='djpeg' ;;
	    *.JPEG)   ext=.JPEG ;   what='djpeg' ;;
	    *.tif)    ext=.tif ;    what='tifftopnm' ;;
	    *.tif.Z)  ext=.tif.Z ;  what='(zcat|tifftopnm)' ;;
	    *.tiff)   ext=.tiff ;   what='tifftopnm' ;;
	    *.tiff.Z) ext=.tiff.Z ; what='(zcat|tifftopnm)' ;;
	    *.r)      ext=.r ;      what='rasttopnm' ;;
	    *.r.Z)    ext=.r.Z ;    what='(zcat|rasttopnm)' ;;
	    *.x)      ext=.x ;      what='xbmtopbm' ;;
	    *.x.Z)    ext=.x.Z ;    what='(zcat|xbmtopbm)' ;;
	    *.xbm)    ext=.xbm ;    what='xbmtopbm' ;;
	    *.xbm.Z)  ext=.xbm.Z ;  what='(zcat|xbmtopbm)' ;;
	    *.xwd)    ext=.xwd ;    what='xwdtopnm' ;;
	    *.xwd.Z)  ext=.xwd.Z ;  what='(zcat|xwdtopnm)' ;;
	    *.fs)     ext=.fs ;     what='fstopgm' ;;
	    *.fs.Z)   ext=.fs.Z ;   what='(zcat|fstopgm)' ;;
	    *.pbm)    ext=.pbm ;    what='cat' ;;
	    *.pbm.Z)  ext=.pbm.Z ;  what='zcat' ;;
	    *.pgm)    ext=.pgm ;    what='cat' ;;
	    *.pgm.Z)  ext=.pgm.Z ;  what='zcat' ;;
	    *.ppm)    ext=.ppm ;    what='cat' ;;
	    *.ppm.Z)  ext=.ppm.Z ;  what='zcat' ;;
	    *.pnm)    ext=.pnm ;    what='cat' ;;
	    *.pnm.Z)  ext=.pnm.Z ;  what='zcat' ;;
	    *~)       ext=~ ;       what='SKIP' ;;
	    *)                      what=FILE ;;
	esac
	case $what in
	    SKIP)
		continue
	    ;;
	    FILE)
		if [ -d "$file" ] ; then
		    if [ ! -f $subdir/.folder.gif ] ; then
			(
			    cd $subdir
			    cat << 'EOF' | uudecode
begin 644 .folder.gif
M1TE&.#EA%``6`,(``````)EF,S,S,\S____,F0```````````"'Y!`$```,`
M+``````4`!8```-4.+K<_C#*21FX^$Y`NN]`Q'UDIBE`0'IJB1)!+,<K'([U
M&@M7G@<\G(\%[`U)1>%Q9SRR@DXB--I)4F%6F%;%W<)X@]1L/!:`P^:T>LT3
(+$SPC#L!`#L%
`
end
EOF
			)
		    fi
		    nf=`ls "$file" | egrep -v "$indexname" | wc -l | awk '{print $1}'`
		    echo "<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><A HREF=\"$file/index.html\"><IMG BORDER=0 WIDTH=20 HEIGHT=22 SRC=\"$subdir/.folder.gif\"></A></TD></TR><TR><TD ALIGN=CENTER><SMALL><A HREF=\"$file/index.html\">$file/</A></SMALL></TD></TR><TR><TD ALIGN=CENTER><SMALL>${nf} files</SMALL></TD></TR></TABLE></TD>"
		    if [ $recurse = yes ] ; then
			echo "$file/" >&2
			( cd "$file" ; thumbnail_index -r -s "$size" -c "$colsperrow" -i "$indexname" )
		    fi
		else
		    if [ ! -f $subdir/.file.gif ] ; then
			(
			    cd $subdir
			    cat << 'EOF' | uudecode
begin 644 .file.gif
M1TE&.#EA%``6`,(``````)F9F3,S,\S______P```````````"'Y!`$```,`
M+``````4`!8```-0*+J\\W`(0JN=(KYI.\\1UU4BN(W7!8DH(+CPB5)`;0/R
M3`5\@$LZ2^_'"A)\.>.1J*P@@<UETO@L!JM1*;2)C7:Y3"]1T"N;>:_(;<W^
$)0``.TLZ
`
end
EOF
			)
		    fi
		    kb=`ls -l "$file" | awk '{print int(($5+1023)/1024)}'`
		    echo "<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><A HREF=\"$file\"><IMG BORDER=0 WIDTH=20 HEIGHT=22 SRC=\"$subdir/.file.gif\"></A></TD></TR><TR><TD ALIGN=CENTER><SMALL><A HREF=\"$file\">$file</A></SMALL></TD></TR><TR><TD ALIGN=CENTER><SMALL>${kb}K</SMALL></TD></TR></TABLE></TD>"
		fi
	    ;;
	    *)
		thumb=$subdir/`echo "$file" | sed "s/$ext\$/_t.jpg/"`
		if [ -f "$thumb" -a ! -s "$thumb" ] ; then
		    rm -f "$thumb"
		fi
		info=$subdir/`echo "$file" | sed "s/$ext\$/_t.info/"`
		if [ -f "$info" -a ! -s "$info" ] ; then
		    rm -f "$info"
		fi
		cat << EOF > $makefile
all:		$thumb $info
$thumb:		$file
	@echo "$file" >&2
	@$what "$file" | pnmscale -xy "$size" "$size" | cjpeg > "$thumb"
$info:		$thumb
	@$infoscript "$file" "$what" "$thumb" > "$info"
EOF
		make -f $makefile | egrep -v 'is up to date' >&2
		rm -f $makefile
		set junk `cat "$info"` ; shift
		kb="$1"
		width="$2"
		height="$3"
		twidth="$4"
		theight="$5"
        next=`echo "$file" | sed 's/\..*$//'`
        if [ "$html" ]; then
            for suffix in $suffixes
            do
               sed "s%<!--@@NEXT@@-->%<A HREF=\"${next}${suffix}\"><IMG SRC=\".forward.gif\"></a>%" \
                              $subdir/${html}${suffix} > /tmp/${html}${suffix}
                mv /tmp/${html}${suffix} $subdir/${html}${suffix}
                echo "<A HREF=\"${next}${suffix}\"><IMG SRC=\".forward.gif\"></a>" >> $subdir/${html}${suffix}
                if [ $suffix = $ss_html ]; then
                    echo "<A HREF=\"${html}.html\"><IMG SRC=\".pause.gif\"></a>" >> $subdir/${html}${suffix}
                else
                    echo "<A HREF=\"${html}${ss_html}\"><IMG SRC=\".slideshow.gif\"></a>" >> $subdir/${html}${suffix}
                fi
                cat << 'EOF' >> $subdir/${html}${suffix}
<HR>
<SMALL>Produced by enhanced 
<A HREF="http://www.hariguchi.org/software/thumbnail_index">thumbnail_index</A>.
The original software is <A HREF="http://www.acme.com/software/thumbnail_index/">here</A>.
</SMALL>
</BODY></HTML>
EOF
            done
            sed "s%<!--@@NEXT_SLIDE@@-->%<META HTTP-EQUIV=\"Refresh\" Content=\"5; URL=${next}${ss_html}\">%" $subdir/${html}${ss_html} > /tmp/${html}${ss_html}
            mv /tmp/${html}${ss_html} $subdir/${html}${ss_html}
            prev=$html
        fi
        html=$next

        for suffix in $suffixes
        do
            cat << 'EOF' > $subdir/${html}${suffix}
<!-- $stamp -->
<HTML>
<!--@@NEXT_SLIDE@@-->
<HEAD><TITLE>
EOF
            echo "$file" >> $subdir/${html}${suffix}
            cat << 'EOF' >> $subdir/${html}${suffix}
</TITLE></HEAD>
<BODY>
EOF
            if [ "$prev" ]; then
                echo "<A HREF=\"${prev}${suffix}\"><IMG SRC=\".back.gif\"></a> " >> $subdir/${html}${suffix}
            else
                echo "<A HREF=\"../${indexname}\"><IMG SRC=\".back.gif\"></a> " >> $subdir/${html}${suffix}
                if [ $suffix = "$ss_html" ]; then
                    cat << 'EOF' > $subdir/slideshow.html
<HTML>
<META HTTP-EQUIV="Refresh" Content="0; URL=
EOF
                   echo ${html}${suffix}\"\> >> $subdir/slideshow.html
                    cat << 'EOF' >> $subdir/slideshow.html
</HTML>
EOF
                fi
            fi
            if [ -f "$captionname" ]; then
                caption=`egrep "$file" $captionname | cut -d'	' -f 2`
                if [ "$caption" ]; then
                    caption=" --- $caption"
                fi
            fi
            cat << EOF >> $subdir/${html}${suffix}
<A HREF="../$indexname"><IMG SRC=".index.gif"></a>
<!--@@NEXT@@-->
EOF
            if [ $suffix = $ss_html ]; then
                echo "<A HREF=\"${html}.html\"><IMG SRC=\".pause.gif\"></a>" >> $subdir/${html}${suffix}
            else
                echo "<A HREF=\"${html}${ss_html}\"><IMG SRC=\".slideshow.gif\"></a>" >> $subdir/${html}${suffix}
            fi
            if [ "$width" -gt "$bigsize" ]; then
                disp_width=$bigsize
            else
                disp_width=$width
            fi
            cat << EOF >> $subdir/${html}${suffix}
$caption
<HR>
EOF
            if ls -l $file | cut -f 1 -d ' ' | egrep '.......-..' >/dev/null ;
            then
                echo "<IMG SRC=\".censored.png\">" >> $subdir/${html}${suffix}
            else
                origFile=`echo $file | sed "s/$prefix//"`
                if [ -f $origPicDir/$origFile ]; then
                    echo "<A HREF=\"../$origPicDir/$origFile\"><IMG WIDTH=$disp_width SRC=\"../$file\"></A>" >> $subdir/${html}${suffix}
                else
                    echo "<IMG WIDTH=$disp_width SRC=\"../$file\">" >> $subdir/${html}${suffix}
                fi
            fi
            cat << EOF >> $subdir/${html}${suffix}
<BR>
<HR>
EOF
            if [ "$prev" ]; then
                echo "<A HREF=\"${prev}${suffix}\"><IMG SRC=\".back.gif\"></a> " >> $subdir/${html}${suffix}
            else
                echo "<A HREF=\"../${indexname}\"><IMG SRC=\".back.gif\"></a> " >> $subdir/${html}${suffix}
            fi
            echo "<A HREF=\"../$indexname\"><IMG SRC=\".index.gif\"></a> " >> \
                                                      $subdir/${html}${suffix}
        done
        if [ "$width" -le "$size" -a "$height" -le "$size" ] ; then
            echo "<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><IMG WIDTH=$width HEIGHT=$height SRC=\"$file\"></TD></TR><TR><TD ALIGN=CENTER><SMALL>$file</SMALL></TD></TR><TR><TD ALIGN=CENTER><SMALL>${kb}K ${width}x${height}</SMALL></TD></TR></TABLE></TD>"
        elif [ "$width" -ge "$bigsize" ]; then
            echo "<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><A HREF=\"$subdir/${html}${suffix}\"><IMG WIDTH=$twidth HEIGHT=$theight SRC=\"$thumb\"></A></TD></TR><TR><TD ALIGN=CENTER><SMALL><A HREF=\"$file\">$file</A></SMALL></TD></TR><TR><TD ALIGN=CENTER><SMALL>${kb}K ${width}x${height}</SMALL></TD></TR></TABLE></TD>"
        else
            echo "<TD ALIGN=CENTER><TABLE><TR><TD ALIGN=CENTER><A HREF=\"$subdir/${html}${suffix}\"><IMG WIDTH=$twidth HEIGHT=$theight SRC=\"$thumb\"></A></TD></TR><TR><TD ALIGN=CENTER><SMALL><A HREF=\"$file\">$file</A></SMALL></TD></TR><TR><TD ALIGN=CENTER><SMALL>${kb}K ${width}x${height}</SMALL></TD></TR></TABLE></TD>"
        fi
	    ;;
	esac
	col=`echo $col + 1 | bc`
    done

    if [ "$html" ]; then
        for suffix in $suffixes
          do
          sed "s%<!--@@NEXT@@-->%<A HREF=\"../${indexname}\"><IMG SRC=\".forward.gif\"></a>%" \
                              $subdir/${html}${suffix} > /tmp/${html}${suffix}
                mv /tmp/${html}${suffix} $subdir/${html}${suffix}
                echo "<A HREF=\"../${indexname}\"><IMG SRC=\".forward.gif\"></a>" >> $subdir/${html}${suffix}
                if [ $suffix = $ss_html ]; then
                    echo "<A HREF=\"${html}.html\"><IMG SRC=\".pause.gif\"></a>" >> $subdir/${html}${suffix}
                else
                    echo "<A HREF=\"${html}${ss_html}\"><IMG SRC=\".slideshow.gif\"></a>" >> $subdir/${html}${suffix}
                fi
                cat << 'EOF' >> $subdir/${html}${suffix}
<HR>
<SMALL>Produced by enhanced 
<A HREF="http://www.hariguchi.org/software/thumbnail_index">thumbnail_index</A>.
The original software is <A HREF="http://www.acme.com/software/thumbnail_index/">here</A>.
</SMALL>
</BODY></HTML>
EOF
        done
        sed "s%<!--@@NEXT_SLIDE@@-->%<META HTTP-EQUIV=\"Refresh\" Content=\"5; URL=../index.html\">%" $subdir/${html}${ss_html} > /tmp/${html}${ss_html}
            mv /tmp/${html}${ss_html} $subdir/${html}${ss_html}
    fi

    cat << 'EOF'
</TR></TABLE>
<HR>
<SMALL>Index produced by enhanced 
<A HREF="http://www.hariguchi.org/software/thumbnail_index">thumbnail_index</A>.
The original software is <A HREF="http://www.acme.com/software/thumbnail_index/">here</A>.</SMALL>
</SMALL></BODY></HTML>
EOF
) > $nindexname
rm -f "$indexname"
cp $nindexname "$indexname"
rm -f $nindexname $infoscript
