#! /bin/sh

# Here is a script for making thumbnails and an index.html file
# for a directory of jpegs.
#
# It expects to see a bunch of .jpg or .JPG files in the current
# working directory.  It chmod a+r's all of them, moves all the
# .JPG files to .jpg if it doesn't cause a conflict, creates thumbnails
# for all of the images, and emits a simple HTML file to easily
# browse the images.

# For best results, you'll have images with names like
# "Jason-before-the-game.jpg" (underbars are treated the same).
# The resulting HTML file will have a name like "Jason before the game"
# displayed under the thumbnail.

# The thumbnail filenames are based on the image filename.  If an image
# is called foo.jpg, the thumbnail is foo-t.jpg.

# If there is no index.html file present, this script creates one.
# If there is an index.html file already, this script sends the
# HTML it wants to create to stdout.

# If you want to leave images out of the HTML index, list them in
# a file called DONTSHOW.txt (list the full filenames).  

# If you pass a list of filenames to the script on the command line, it
# will only create a table for those images.  Specifically, if you pass
# filenames on the command line, it doesn't create any of the HTML header
# or trailer bits, so it is easy to include the table in an already
# extant HTML file.

# RCS ID:  $Id: makethumbs.sh,v 1.25 1999/05/27 02:21:01 molenda Exp $

# Written by Jason Molenda, jasonmt@molenda.com, 1998-09-13.  This script 
# is placed in the public domain.

###############################
### OPTION PROCESSING
###############################

init ()
{
  MAXTHUMBSIZE=150
  MAXTHUMBHEIGHT=$MAXTHUMBSIZE
  MAXTHUMBWIDTH=$MAXTHUMBSIZE
  COLUMNS=5
  SHOW_FILESIZE=1
  USE_TWO_WINDOWS=0
}

help ()
{
  echo "Usage: `basename $0` [--maxthumbsize=n] [--columns=n] " >&2
  echo "       [--show-filesize] [--use-two-windows] [filenames...]" >&2
  echo "" >&2
  echo "--maxthumbsize    Maximum size of thumbnails, in pixels, default $MAXTHUMBSIZE" >&2
  echo "--columns         Number of thumbnails per line, default $COLUMNS" >&2
  echo -n "--show-filesize   Show image size in kbytes below thumbnail, default is " >&2
       ([ $SHOW_FILESIZE -eq 1 ] && echo enabled || echo disabled) >& 2
  echo -n "--use-two-windows Bring up a new window to see images, default is " >& 2
       ([ $USE_TWO_WINDOWS -eq 1 ] && echo enabled || echo disabled) >& 2
  echo "" >& 2
  echo "Run this script in a directory of JPEG files to create thumbnail images and" >& 2
  echo "an index.html. If an index.html is already present, its HTML is sent to stdout." >& 2
  echo "" >& 2
  echo "You may list the JPEG files on the command line if you prefer.  By default," >& 2
  echo "`basename $0` works on every JPEG file in the current working directory." >& 2
  echo "" >& 2
  echo "This script written by Jason Molenda, jasonmt@molenda.com.  It is placed" >&2
  echo "in the public domain.  This is the version string: " >&2
  echo '$Id: makethumbs.sh,v 1.25 1999/05/27 02:21:01 molenda Exp $' >&2
}

process_options ()
{
  while [ $# -gt 0 ]
  do
    optarg="`echo $1 | sed 's,^[^=]*=,,'`"
    case "$1" in
      --maxthumbsize=* | --max-thumb-size=*) MAXTHUMBSIZE="$optarg" 
                                             MAXTHUMBHEIGHT=$MAXTHUMBSIZE
                                             MAXTHUMBWIDTH=$MAXTHUMBSIZE ;;
      --maxthumbheight=* | --max-thumb-height=*) MAXTHUMBHEIGHT="$optarg" ;;
      --columns=* | --cols=*) COLUMNS="$optarg" ;;
      --show-filesize*) SHOW_FILESIZE=1 ;;
      --use-two-windows*) USE_TWO_WINDOWS=1 ;;
      -h | --help | -v | --version | -V)
         help
         exit 1 ;;
      -*)
         echo "`basename $0`: ERROR: Unrecognized option \"$1\"" >&2 ;;
      *)
        files="$files $optarg" ;;
    esac
    shift
  done
}

find_needed ()
{
  for i in djpeg cjpeg pnmscale
  do
    found=0
    for j in `echo $PATH | sed -e 's,^:,. ,' -e 's,:, ,g'`
    do
      if [ -x $j/$i ]
      then
        found=1
        break
      fi
    done
    if [ $found -ne 1 ]
    then
      echo ERROR: Could not find needed utility \"${i}\".  Aborting. >&2
      if [ $i = djpeg -o $i = cjpeg ]
      then
        echo ERROR: You may be able to find a copy of this at ftp://ftp.uu.net/graphics/jpeg >&2
      fi
      if [ $i = pnmscale ]
      then
        echo ERROR: You need to install the \"pbmplus\" utilities. >&2
      fi
      exit 1
    fi
  done
}

###############################
### MAIN SCRIPT
###############################

init
find_needed
process_options $*

umask 022

# this script is for making publically-accessable HTML pages; therefore
# the following is always correct (for me, anyway :-).
chmod a+x .


TMPFILE=/tmp/makethumbs.$$
trap "rm -f $TMPFILE; exit" 0 1 2 15

dirname="`pwd | sed -e 's,^.*/,,' -e 's,_, ,g'`"

# Is the user passing filenames on the command line?
if [ -n "$files" ]
then
  FILELIST="$*"
  HEADER=0
else
  FILELIST=`ls -1 | egrep '\.jpg$|\.JPG$|\.JPEG$'`
  HEADER=1
  if [ ! -f index.html ]
  then
    exec > index.html
  fi
fi

if [ -z "$FILELIST" ]
then
  echo Error!  No JPEG \(\"foo.jpg\"\) files in this directory!  Exiting. >&2
  exit 1
fi

if [ $HEADER -eq 1 ]
then
  echo '
<HTML>
 <HEAD>'
echo " <TITLE>$dirname</TITLE>"
echo '
  <STYLE TYPE="text/css">
   <!--
    A:link,A:visited,A:active {font-weight: bold; text-decoration: none}
   -->
  </STYLE>
 </HEAD>
  <BODY BGCOLOR="Black" TEXT="#White" LINK="#Silver" VLINK="gray" ALINK="White">

<TABLE CELLPADDING=8 CELLSPACING=0 WIDTH=85% BORDER=1 ALIGN=CENTER>
<TR>
<TD BGCOLOR="black">

'
  echo ""
  echo "<h1 align=\"center\">$dirname</h1>"
  echo ""
fi

echo '<div align="center">'
echo '<table>'

c=0
first=1

ls -1 | egrep '\.JPG$|\.jpeg$|\.JPEG$' > $TMPFILE
if [ -s $TMPFILE ]
then
   while read i
   do
     j=`echo "$i" | sed -e 's,.JPG$,.jpg,' -e 's,.jpeg$,.jpg,' -e 's,.JPEG$,.jpg,'`
     if [ ! -f "$j" ]
     then
       mv "$i" "$j"
     fi
   done < $TMPFILE
   FILELIST=`ls -1 | egrep '\.jpg$|\.JPG$|\.JPEG$'`
fi

rm -f $TMPFILE

for i in $FILELIST
do

  if echo "$i" | grep '\-t\.jpg' >/dev/null 2>&1
  then
    continue
  fi

  if grep "$i" DONTSHOW.txt >/dev/null 2>&1
  then
    continue
  fi

  chmod a+r "$i"
  j=`echo "$i" | sed -e 's,.JPG$,-t.jpg,' -e 's,.jpg$,-t.jpg,'`
  if [ $c -eq 0 ]
  then
    if [ $first -eq 1 ]
    then
      echo ""
      echo '<tr>'
      first=0
    else
      echo '  </td>'
      echo '</tr>'
      echo ""
      echo '<tr>'
    fi
    echo '  <td align="center">'
    echo -n '    '
  else
    echo '  </td>'
    echo '  <td align="center">'
    echo -n '    '
  fi

  if [ ! -f "$j" ]
  then
    djpeg -ppm < "$i" | pnmscale -xysize $MAXTHUMBWIDTH $MAXTHUMBHEIGHT | cjpeg -optimize > "$j"
  fi

  tninfo=`djpeg -ppm < "$j" | pnmfile`
  tnx=`echo $tninfo | awk '{print $4}'`
  tny=`echo $tninfo | awk '{print $6}'`

  title=`echo "$j" | sed -e 's,-t.jpg,,' -e 's,-, ,g' -e 's,_, ,g'`

  if [ $SHOW_FILESIZE -eq 1 ]
  then
    bytes=`ls -l "$i" | awk '{print $5}'`
    kbytes=`expr $bytes / 1024`
  fi

  i=`echo "$i" | sed 's, ,%20,g'`
  j=`echo "$j" | sed 's, ,%20,g'`
  echo -n "<a "

  if [ $USE_TWO_WINDOWS -eq 1 ]
  then
    echo -n "target=\"display\" "
  fi

  echo -n "href=\"${i}\"><img src=\"$j\" width=\"${tnx}\" height=\"${tny}\" alt=\"\"><br>${title}</a>"

  if [ $SHOW_FILESIZE -eq 1 ]
  then
    echo -n " (${kbytes}k)"
  fi

  echo ""

  c=`expr $c + 1`

  if [ $c -eq $COLUMNS ]
  then
    c=0
  fi

done

echo '  </td>'
echo '</tr>'
echo ""

echo '</table>'
echo '</div>'

echo '
</TD>
</TR>
<TR>
<TD ALIGN=center BGCOLOR="Black"> '
echo "`date '+%m.%d.%y @ %H:%M'` by `whoami`"
echo '
</FONT>
</TD>
</TR>
</TABLE>

<!-- 

Jason Molenda ~ jasonmt@molenda.com.  
makethumbs.sh,v 1.25 1999/05/27 02:21:01

w/ mpeterson layout changes

-->

</BODY>
</HTML>

'

if [ $HEADER -eq 1 ]
then

  echo ""

  echo '</body>'
  echo '</html>'
fi

exit 0
