#!/bin/bash -norc
# Create thumb-nail jpeg images for all jpeg files in a directory.
# Also a .html template file is created with the references, proper
# thumb-nail pixel sizes and original file size and date for each image.
# Use the -h switch to avoid writing the html header, the -t switch
# to not write the trailer and the -n switch for neither.
# Find the path to the current directory (no trailing /)
CURPATH=`pwd`
# Pick out the fragment of the path that is needed in a URL. We assume here
# that the directory is somewhere in the G0 ftp area specified either
# in absolute terms or using a link (ftp) from the codagz home directory.
HTMLPATH=`echo $CURPATH | sed "s#/home/codagz/ftp/##g"`
HTMLPATH=`echo $HTMLPATH | sed "s#/pub/ftp/pub/G0/##g"`
# Make sure that the "thumbs" directory exists.
mkdir thumbs
# Create a blank html file
rm README.html
if [ ${1:-NOARG} != -h -a ${1:-NOARG} != -n ]; then
# Add a header to the html file.
echo 'Generating HTML header.'
cat <<-endhtml >README.html
-title-
-title-
endhtml
else
echo 'No HTML header generated.'
fi
# Begin the table
cat <<-endhtml >>README.html
endhtml
# Create an index to keep track of when to start a new line
declare -i FILENO
declare -i ITEMNO
FILENO=0
# For each jpeg file, generate a thumb-nail and add a line
# to the html file.
for FILE in `ls *.jpg`; do
echo 'Processing ' $FILE
# Generate a name for the intermediate file
PNMNAME=/tmp/`basename $FILE .pnm`
# Convert the input file to a thumb-nail file pnm format
# file that fits in a 100X100 pixel box.
djpeg -pnm $FILE | pnmscale -xysize 100 100 > $PNMNAME
# Find the width and height of the resulting file.
STATS=(`pnmfile $PNMNAME`)
# Now convert the pnm thumb-nail file to a jpeg file
# in the thumbs sub-directory and get rid of the intermediate
# file.
cjpeg -optimize $PNMNAME >./thumbs/thumb_$FILE
rm $PNMNAME
# Get the file size in kbytes (0'th element of this array)
DISKSIZE=(`du -k $FILE`)
# Get the file creation date
DATE=(`ls -l $FILE`)
# Start a new table row if necessary
ITEMNO=$FILENO%6
if [ $ITEMNO -eq 0 ]; then
cat <<-endhtml >>README.html
endhtml
fi
# Add an entry to the html file for this picture.
cat <<-endhtml >>README.html
${DATE[5]} ${DATE[6]} ${DATE[7]}
${DISKSIZE[0]} kb
|
endhtml
# Update the file counter
FILENO=$FILENO+1
# Finish table row if necessary
ITEMNO=$FILENO%6
if [ $ITEMNO -eq 0 ]; then
cat <<-endhtml >>README.html
endhtml
fi
done
# Finish the table row if that hasn't already happened
if [ $ITEMNO -ne 0 ]; then
cat <<-endhtml >>README.html
endhtml
fi
# Finish the table.
cat <<-endhtml >>README.html
endhtml
if [ ${1:-NOARG} != -t -a ${1:-NOARG} != -n ]; then
# Add the trailer to the file.
echo 'Generating HTML trailer.'
cat <<-endhtml >>README.html
endhtml
else
echo 'No HTML trailer generated.'
fi