#!/bin/sh
#
# Photoweb: makes nice html of jpegs in a directory.
#
# Version 3.0
#
# By: Phil Wherry <psw@wherry.com>, Eric Johnston <emj@postal.net>
#
# To install, just put it somewhere in your path.  Make sure you have
# the utilities cjpeg, djpeg, exiftags, and wwwis (aka wwwimagesize).
# To run, just get in your photo directory and run it:
#
#     cd directoryfullofjpegs/
#     photoweb
#
# $Id: photoweb,v 1.9 2003/06/02 17:49:45 ejohnst Exp $
#

CWD="`pwd`"
SSCALE="1/8"
MSCALE="1/2"
CAPTIONS="captions.txt"
COLUMNS=3
THUMBQUALITY=75
EXIFOPTS=""		# Add -l here if you have a removable lens.
CAMERAHDR="YES"		# Set to NO if you have a mix of cameras.

n=0

if [ -f $CWD/index.html ]; then
   echo "$CWD/index.html exists - stopping"
   exit 1
fi

title=`basename $CWD`

cat <<EOF > index.html
<html><head><title>$title Photo Index</title></head>
<! photoweb v3.0 http://johnst.org/sw/ - Phil Wherry & Eric Johnston>
<body><basefont face="Verdana, Arial, Helvetica">
<h2>$title</h2>
Photo Index
<p>

<table border=0 cellspacing=10><tr>
EOF

cat <<EOF > info.html
<html><head><title>$title Photo Info</title></head>
<! photoweb v2.7 http://johnst.org/sw/ - Phil Wherry & Eric Johnston>
</body>
<body><basefont face="Verdana, Arial, Helvetica">
<h2>$title</h2>
Photo Info
<p>

EOF

for jfile in *.JPG; do
   [ "X$jfile" = "X*.JPG" ] || mv $jfile `basename $jfile .JPG`.jpg
done

for jfile in *.jpg; do
   fname=`echo "$jfile" | tr -d "'\ \""`
   bname=`basename "$fname" .jpg`
   sname="$bname-s.jpg"

   # Let's ignore previously created thumbnails.
   t1=`basename "$bname" "-m"`
   t2=`basename "$bname" "-s"`
   [ "$bname" = "$t1" ] || continue
   [ "$bname" = "$t2" ] || continue

   # Rename files so they're somewhat standard.  (Back up file beforehand.)

   [ "$jfile" = "$fname" ] || [ -f "$jfile~" ] || cp -p "$jfile" "$jfile~"
   [ "$jfile" = "$fname" ] || mv -i "$jfile" "$fname"

   echo "Processing $fname ($jfile)"

   # Create thumbnail and medium-sized versions of the pics.

   [ -f "$bname-m.jpg" ] || \
      djpeg -scale $MSCALE "$fname" | cjpeg -quality 90 > $bname-m.jpg
   [ -f "$sname" ] || \
      djpeg -scale $SSCALE "$fname" | cjpeg -quality $THUMBQUALITY > $sname

   # Try to grab a caption for the photo.

   if [ -f $CAPTIONS ]; then
      capt=`grep "^$bname	" $CAPTIONS | tr "\"" "'" | cut -f2`
      subhead=`grep "^$bname	" $CAPTIONS | tr "\"" "'" | cut -f3`
   fi

   if [ "X$capt" != "X" ]; then
      echo "   $capt"
   fi

   # First time through, grab camera info.

   if [ $n -eq 0 ]; then
      case ${CAMERAHDR} in
      [Yy][Ee][Ss])
	 echo "<table border=0>" >> info.html
	 exiftags -cq $EXIFOPTS "$fname" | sed 's/: /	/' | \
	    awk 'BEGIN { FS = "	" } { printf "<tr><td align=right><font size=-1><i>%s:&nbsp;</i></font></td><td><font size=-1>%s</font></td></tr>", $1, $2 }' >> info.html
	 echo "</table><table border=0>" >> info.html
	 ;;

      *)
	 EXIFOPTS="-c $EXIFOPTS"
	 echo "<table border=0>" >> info.html
	 ;;
      esac
   fi

   n=$(($n + 1))

   # Write a subheading.

   if [ "X$subhead" != "X" ]; then
      cat <<EOF >> index.html
</tr><tr><td valign=bottom colspan=3><font size=+1><br><b>$subhead</b></font></td></tr><tr>
EOF
      n=1
   fi

   # Populate the index.

   cat <<EOF >> index.html
<td valign=top><br><a href="$bname-m.jpg" target=_blank><img src="$sname" alt="$fname"></a><br><font size="-2"><a href="$bname-m.jpg">[med]</a> |
<a href="$fname">[big]</a> | <a href="info.html#$fname">[info]</a><br>
<br></font><font size="-1">$capt</font></td>
EOF

   # Populate the Exif info.

   cat <<EOF >> info.html
<tr><td colspan=2 align=left><font size=-1><a name=$fname>
<br><b>$fname</b><br>&nbsp;</font></td></tr>
<tr><td valign=top><img src="$sname" alt="$fname">&nbsp;&nbsp;</td><td><table border=0>
EOF

   exiftags -iq $EXIFOPTS "$fname" | sed 's/: /	/' | \
      awk 'BEGIN { FS = "	" } { printf "<tr><td align=right><font size=-2><i>%s:</i></font></td><td><font size=-2>%s</font></td></tr>\n", $1, $2 }' >> info.html
   echo "</table></td></tr>" >> info.html

   [ $(($n % $COLUMNS)) -eq 0 ] && echo "</tr><tr>" >> index.html
done

cat <<EOF >> index.html
</tr></table>
<br><font size="-1"><hr><a href="http://johnst.org/sw/">photoweb</a></font>
</body>
</html>
EOF

wwwis index.html info.html
rm -f index.html~ info.html~
