:
# NAME:
#	photoweb - prepare pics for display on the web
#
# SYNOPSIS:
#	photoweb [options]
#
# DESCRIPTION:
#	By default this script looks at all image files (.jpg etc) in
#	the current directory except those with basenames ending in -w
#	or -t.  For each image "file"."ext" that is greater than 80k
#	(see '-s') it creates a scaled down web image in
#	"file"-w."ext" which is more efficiently viewed via the web
#	(default is 760x700 pixels).  It also creates a thumbnail
#	"file"-t."ext" which is used in the generated index.htm to
#	reference the first of "file".htm, "file"-w."ext" or
#	"file"."ext" that can be found.
#
#	Options:
#
#	-T	Produce a tiled index.htm, handy if lots of pics.
#
#	-W "wide"
#		When doing a tiled index.htm make the table "wide" (7)
#		columns. The default of 7  works well for most
#		browsers. 
#
#	-R "rotate"
#		The file "rotate" (.rotate) contains a list of
#		rotations that certain pics need.  The format is
#		pic:rotate and is used when generating the thumbnail
#		and web images.
#
#	-Q "quality"
#		Maintain "quality" (94) when rotating.
#
#	-i	Make interlaced thumbnail and web image files.
#
#	-I "mode"
#		Sets the interlace "mode" (Plane).
#
#	-s "skip"
#		The -w file will be skipped if the orignal image file
#		is < "skip" (80) Kb.
#
#	-t "thumb"
#		The thumbnail is constrained to "thumb" (100) pixels
#		on its longest side.
#
#	-w "XxY"
#		The web image is constrained within "XxY" (760x700).
#
#	-H "hdr"
#		If "hdr" (.hdr) exists it is included at the start of
#		index.htm. 
#
#	-D "desc"
#		If "desc" (.desc) exists, then we look for a line that
#		contains "file":"comment" and if found, "comment" is
#		included in the index.htm unless '-T' is in effect.
#
#	-F
#		We produce a index that contains only the pics refered
#		to in "desc" and in the order listed.
#
#	-h	Produce a .htm file (if missing) for each picture
#		which displays the WEB image as a pointer to the
#		original.  This format also allows extra descriptive
#		text to be included with the picture.  
#
#	-N "style"
#		Print the name of the picture above the thumbnail.
#		If "style" is not one of ('base','full') it is ignored.
#	
# SEE ALSO:
#	mpics.pl(1), ImageMagik
#	
# AUTHOR:
#	Rodney Campbell <rodney@telstra.com.au>,
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: photoweb,v 1.17 2003/01/12 09:19:25 sjg Exp $
#
#	@(#)Copyright (c) 1998-2002 Rodney Campbell, Simon J. Gerraty
#	
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to use, modify and distribute this source code 
#	is granted subject to the following conditions.
#	1/ that the above copyright notice and this notice 
#	are preserved in all copies and that due credit be given 
#	to the author.	
#
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

case `echo -n .` in -n*) N=; C="\c";; *) N="-n"; C=;; esac

# add . debug.sh to $rc if you
# have it and want to debug things.
Debug() {
        :
}

rc=.photowebrc
RAWDIR=raw

opt_str=Tis:t:w:H:I:D:W:hFR:Q:N:C:
eval `date '+YEAR=%Y MONTH=%m'`
opt_C=$YEAR
[ -s .hdr ] && opt_H=.hdr || opt_H=	# default header file/string
opt_D=.desc			# default description file
opt_R=.rotate			# default list of rotations
opt_Q=94			# quality to maintain when rotating
opt_I=Plane			# default interlace mode
opt_i=				# turn on interlace mode if set
opt_s=				# skip -w if original < opt_s
opt_t=100			# constraining size of -t file
#opt_w=640x540			# constraining size of -w file
opt_w=760x700			# constraining size of -w file
opt_W=7				# how many columns in tile mode
opt_assign_eval=yes		# handle var=val args

here=`/bin/pwd`
dir=`basename $here`
case "$dir$MONTH" in
1212) ;;
12*)  opt_C=`expr $YEAR - 1`;;
esac

case `du -s -k /dev/null 2>/dev/null` in
0*)	du_k=-k;;
*)	du_k=;;
esac

for d in $HOME .
do
    [ -s $d/$rc ] && . $d/$rc
done

. setopts.sh

case "$here" in
*pics/*) title=${title:-`expr $here : '.*pics/\(.*\)'`};;
esac
title=${title:-`expr $here : '.*/\([1-9][0-9][0-9].*\)'`}

INTERLACE="${opt_i:+-interlace $opt_I}"
TITLE="${TITLE:-$NAME : Photo Album : ${title:-$opt_C/$dir}}"

# these are the colors used in various places
BG_COLOR=${BG_COLOR:-#FFFFFF}
TEXT_COLOR=${TEXT_COLOR:-#000044}
LINK_COLOR=${LINK_COLOR:-#CC99FF}
VLINK_COLOR=${VLINK_COLOR:-#FF6666}
TTOPBG_COLOR=${TTOPBG_COLOR:-#006633}
TCELBG_COLOR=${TCELBG_COLOR:-#FFFF99}
FONT_FACE=${FONT_FACE:-"Verdana, Arial, Helvetica"}
FONT_COLOR=${FONT_COLOR:-#FFFFFF}
HR_COLOR=${HR_COLOR:-#0000FF}

if [ ${opt_W:-1} = 1 ]
then
	opt_T=
fi
if [ $# = 0 ]; then 
	set -- `ls -1 *.gif *.jpg *.pnm *.tif *.tiff *.bmp 2>/dev/null | grep -v -- '-[wt].jpg'`
fi

cat <<!EOF! > index.htm.new

<HTML>
<HEAD>
  <TITLE>$TITLE</TITLE>
  <LINK REV="made" HREF="mailto:$EMAIL">
<META NAME="Author" CONTENT="$NAME">
<META HTTP-EQUIV="Reply to" CONTENT="$EMAIL">
<META NAME="Description" CONTENT="$TITLE">
<META NAME="Keywords" CONTENT="$NAME">
<META NAME="ROBOTS" CONTENT="ALL">
</HEAD>
<BODY BGCOLOR=$BG_COLOR TEXT=$TEXT_COLOR LINK=$LINK_COLOR VLINK=$VLINK_COLOR>

<P><BR></P>

<H1 ALIGN=CENTER>
`[ -s ${opt_H:-/dev/null} ] && cat $opt_H || echo "${opt_H:-$TITLE}"`
</H1>

<P><BR></P>

<TABLE WIDTH=100% CELLPADDING=3 CELLSPACING=0 BORDER=0>
!EOF!

if [ x$opt_T != x ]
then
	cat <<!EOF >> index.htm.new

<TR VALIGN="TOP">
	<TD BGCOLOR="$TTOPBG_COLOR" ALIGN=CENTER WIDTH=100%>
	<FONT SIZE="-1" FACE="$FONT_FACE" COLOR="$FONT_COLOR" SIZE=3>
	Click the thumbnail images below to see a larger version
	<BR>
	</FONT>
	</TD>
</TR>
</TABLE>

<TABLE WIDTH=100% CELLPADDING=3 CELLSPACING=0 BORDER=0>
!EOF
fi

COPYRIGHT_URL=${COPYRIGHT_URL:-$URL}
COPYRIGHT_NAME=${COPYRIGHT_NAME:-$NAME}
case "$COPYRIGHT_URL" in
none)	COPYRIGHT=;;
*)	COPYRIGHT="<CENTER>
  <ADDRESS ALIGN=\"CENTER\">
    <P ALIGN=\"CENTER\">
    <FONT SIZE=\"-1\">
    <A HREF=\"$COPYRIGHT_URL\">$COPYRIGHT_NAME</A><BR>
    Copyright &#169; $opt_C
    </FONT>
  </ADDRESS>
</CENTER>"
	;;
esac

newer() {
	ls -1t $* 2>/dev/null | head -1
}

doPic() {
	f=$1
	Debug $f
	case "$f" in
	*-[tw].jpg)	return;;
	*.htm|*.html)	return;;
	esac
	if [ ! -s $f ]; then
		echo "Skipping $f"
		return
	fi
	FILEBASE=`expr $f : '^\(.*\)\.[^\.]*$'`
	EXT=`expr $f : '.*\.\([^\.]*\)$'`
	Debug $f $FILEBASE
	# The print lab wants .tif's so make sure we
	# don't duplicate index entries.
	ok=`grep ${FILEBASE}- index.htm.new`
	[ "x$ok" != x ] && continue
	
	FILESIZE=`du -s $du_k $f | cut -f1`
	if [ -s $opt_D ]; then
		DESC=`grep "^$FILEBASE" $opt_D | cut -d' ' -f2-`
	else
		DESC=
	fi
	case "$opt_N" in
	full)	desc=$f;;
	base)	desc=$FILEBASE;;
	*)	desc=;;
	esac
	if [ -s $opt_R ]; then
		Debug $f $FILEBASE $f:rotate $FILEBASE:rotate rotate
		ROTATE=`grep "^$FILEBASE" $opt_R | cut -d: -f2 | tail -1`
		if [ "x$ROTATE" != x ]; then
			ROTATE="-rotate $ROTATE ${opt_Q:+-quality} $opt_Q"
		fi
	else
		ROTATE=
	fi
	Debug $f $FILEBASE
	echo $N "${FILEBASE} [${FILESIZE}Kb] ->$C"

	#
	# Do the thumbnail first so we can return early if that's
	# all we want.  Handy for 1st pass - before running mkrotate.
	#
	DIM=
	if [ `newer $f ${FILEBASE}-t.jpg` !=  ${FILEBASE}-t.jpg ]
	then
	    Debug $f $FILEBASE $f:convert $FILEBASE:convert convert    
	    $DOIT convert $ROTATE -geometry $opt_t ${INTERLACE} $f ${FILEBASE}-t.jpg
	    Debug $f $FILEBASE 
	else
		DIM=`grep '${FILEBASE}[-\.]' index.htm 2>/dev/null | sed 's,.*\(WIDTH.*\)BORDER.*,\1,'`
	fi
	case "$DIM" in
	*=*)	eval "$DIM"
		;;
	*)
		eval `identify ${FILEBASE}-t.jpg | sed 's,.* \([1-9][0-9]*\)x\([^ ]*\) .*,DIM=\1x\2 WIDTH=\1 HEIGHT=\2,'`
		;;
	esac
	echo $N " Thumbnail [${WIDTH}x${HEIGHT}] ->$C"

	case "$opt_w" in
	no*|skip|"")
                WEB=${FILEBASE}-w.jpg
                echo " Skipping $WEB etc."
		JustThumbnails=yes
		;;
	*)	JustThumbnails=no;;
	esac

if [ $JustThumbnails != yes ]; then
	if [ $FILESIZE -gt ${opt_s:-80} -o "x$ROTATE" != x ]; then
		WEB=${FILEBASE}-w.jpg
	else
		WEB=$f
	fi
	if [ `newer $f $WEB` != $WEB ]
	then
	    Debug $f $FILEBASE $f:convert $FILEBASE:convert convert
	    $DOIT convert $ROTATE -geometry "$opt_w>" ${INTERLACE} $f $WEB
	    Debug $f $FILEBASE 
	fi
	[ -s $WEB ] || { echo " Skipped $WEB"; return; }
	WEBSIZE=`du -s $du_k $WEB | cut -f1`
	echo " Web Image [${WEBSIZE}Kb]"

	if [ x$opt_h != x ]
	then
		if [ ! -s $FILEBASE.htm ]
		then
			Debug $f $FILEBASE $f:htm $FILEBASE:htm htm
			TIF=$FILEBASE.tif
			if [ -f $TIF -a $f != $TIF ]; then
				TIFSIZE=`du -s $du_k $TIF | cut -f1`
				TIF="<P><A HREF=\"$TIF\">$TIF</A> uncompressed tiff (${TIFSIZE}KB)<P>"
			else
				TIF=
			fi
			if [ -s $RAWDIR/$f ]; then
                                RAWSIZE=`du -s $du_k $RAWDIR/$f | cut -f1`
                                RAW="<P><A HREF=\"$RAWDIR/$f\">Original .$EXT (${RAWSIZE}KB)</A></P>"
			else
                                RAW=
			fi
			cat <<!EOF > $FILEBASE.htm
<HTML>
<HEAD>
<TITLE>$FILEBASE:</TITLE>
<META NAME="Author" CONTENT="$NAME">
<META HTTP-EQUIV="Reply to" CONTENT="$EMAIL">
<META NAME="Description" CONTENT="$TITLE">
<META NAME="Keywords" CONTENT="$NAME">
<META NAME="ROBOTS" CONTENT="ALL">
</HEAD>
<BODY>
<H1>$FILEBASE</H1>
<A HREF="$f"><IMG SRC="$WEB" ${ROTATE:+ALIGN="LEFT"} BORDER=0 ALT="${FILESIZE}KB JPEG"></A>
<P>$DESC
<P>$TIF
<P>$RAW
<BR CLEAR="ALL">
<HR>
$COPYRIGHT
</BODY>
</HTML>
!EOF
			Debug $f $FILEBASE
		fi
	fi
	for x in .htm .html
	do
		[ -s $FILEBASE$x ] || continue
		WEB=$FILEBASE$x
		break
	done
fi
if [ x$opt_T = x -o $n = 0 ]
then
    echo '<TR VALIGN="TOP">' >> index.htm.new
fi

	cat <<!EOF >> index.htm.new

	<TD BGCOLOR="$TCELBG_COLOR" WIDTH=106>
	${desc}<BR><A HREF="$WEB"><IMG SRC="${FILEBASE}-t.jpg" WIDTH=${WIDTH} HEIGHT=${HEIGHT} BORDER=0 ALT="${WEBSIZE}KB JPEG"></A>
	</TD>
!EOF

if [ x$opt_T = x ]
then
	cat <<!EOF >> index.htm.new

	<TD BGCOLOR="$TTOPBG_COLOR" WIDTH=100%>
	<FONT SIZE="-1" FACE="$FONT_FACE" COLOR="#FFFFFF" SIZE=3>
	
	<BR>
	Click the image to the left to see a larger version [${WEBSIZE}KB JPEG]
	<BR><BR>$DESC
	</FONT>
	</TD>
</TR>
!EOF
else
	n=`expr $n + 1`
	if [ $n = $opt_W ]
	then
	    n=0
	    echo '</TR>' >> index.htm.new
	fi
fi
}

n=0
if [ "x$opt_F" != x -a -s ${opt_D:-/dev/null} ]
then
	set -- `sed -e 's/[ 	].*//' -e 's/$/\*/' $opt_D`
fi

for f in $*
do
	doPic $f
done

if [ x$opt_T != x -a $n != 0 ]
then
    while [ $n != $opt_W ]
    do
	n=`expr $n + 1`
	echo "<TD BGCOLOR=\"$TCELBG_COLOR\" WIDTH=106>&nbsp</TD>"
    done >> index.htm.new
    echo '</TR>' >> index.htm.new
fi

cat <<!EOF >> index.htm.new

</TABLE>

<P>
<HR NOSHADE COLOR="$HR_COLOR" ALIGN=LEFT WIDTH=400>

<P>
$COPYRIGHT
</BODY>
</HTML>
!EOF

/etc/rc_d/install_new.sh index.htm
