#!/bin/sh
# Creates simple web galleries using ImageMagick and Apache Virtual Includes
#
# Originally written and distributed by Dayid Alan <code@dayid.org>
# http://code.dayid.org/asimplegal
#
# Published under the Pancake Public License v 1.0: http://code.dayid.org/ppl/ppl.txt
#
# Define important variables:
# - Lines including user-adjustable variables are preceeded by ##.
#
## URL for the gallery (do not include http:// or a trailing /)
GALURL='www.dayid.org'
#
# Relative directory of your document root.
# "public_html" by default, sometimes "html" or "web" pending machine setup
## Should have a trailing /
RELDIR='public_html/'
#
# Remove spaces in filenames:
for FILE in *; do
    NEW=`echo ${FILE} | tr ' ' '_'`
    [ "${NEW}" != "${FILE}" ] && mv ${FILE} ${NEW} && echo "Renamed: ${FILE} to ${NEW}"
done
#
## Inserted to your HTML files before the thumbnails/links:
TOP="<!--#include virtual=\"/fragment/top.fhtml\" --><div id=\"gallery\">"
BOTTOM="</div><!--#include virtual=\"/fragment/bottom.fhtml\" -->"
PAGE="${TOP}"
#
# Create the header for index file and echo it into index.html:
echo "<!--#include virtual=\"/fragment/top.fhtml\" --><div id=\"gallery\">" > index.html
#
# Start the breadcrumb/header/directory links
echo -n "<a href=\"http://${GALURL}\">${GALURL}</a> / " > .GALBREAD
#
# Find out where we are, and make breadcrumb links:
CURDIR=`pwd | awk -F "${RELDIR}" '{ print $2 }'`
DIRDIR=`echo ${CURDIR} | cut -d "/" -f "1"`
OLDDIR=""
while [ `echo ${CURDIR} | wc -w` -gt '0' ]; do
      echo -n "<a href=\"${OLDDIR}/${DIRDIR}\">${DIRDIR}</a> / " >> .GALBREAD
      CURDIR=`echo ${CURDIR} | awk -F "${DIRDIR}/" '{ print $2 }'`
      OLDDIR="${OLDDIR}/${DIRDIR}"
      DIRDIR=`echo ${CURDIR} | cut -d "/" -f "1"`
done
#
# Add the breakdrumbs to index.html:
cat .GALBREAD >> index.html
# Add a line before files:
echo '<br><a href="javascript:javascript:history.go(-1)">BACK</a><hr>' >> index.html
#
# Take note of all files in CURDIR - ignoring hidden:
find . -maxdepth "1" -type "f" | sed -e 's/\.\///g' | grep -v ^\\. | grep -v "mediums.html" | grep -v "index.html" > .GAL
#
# Take note of all directories in CURDIR - ignoring hidden: 
find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | sort -n > .GALDIRS
DIRS=`wc -l .GALDIRS | awk '{ print $1 }'`
# If there are directories, create a table to contain them and link them
if [ ${DIRS} -gt '0' ]; then
   echo '<table width="100%"><tr><th>Folder:</th><th>Description</th></tr>' >> index.html
# For each directory, check for description in .htaccess and use it if possible:
   for DIR in $( find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | sort -n ); do
       DESCR=""
       [ -f .htaccess ] && DESCR=`grep \"${DIR}\" .htaccess 2>/dev/null | cut -d '"' -f "2"`
       echo "<tr><td><a href=\"${DIR}\">${DIR}</a></td><td>${DESCR}</td></tr>" >> index.html
       grep -v "${DIR}" .GALDIRS > .GALTEMP && mv .GALTEMP .GALDIRS
   done
# Close the table and add another horizontal line
   echo '</table><hr>' >> index.html
fi
#
# Define filetypes to be processed and process them:
for EXT in `echo jpg png bmp tif jpeg gif JPG PNG BMP TIF JPEG GIF`; do
# Make sure there are files with that extension before trying to process that extension
    CHECK=$( find . -maxdepth "1" -name "*.${EXT}" -type "f" 2> /dev/null | grep -v '\.m\.'${EXT} | grep -v '\.t\.'${EXT} | wc -l | awk '{ print $1 }' )
    if [ ${CHECK} -gt '0' ]; then
# Remove that extension from the list of files to be processed
       grep -v "\.${EXT}" .GAL > .GALTEMP && mv .GALTEMP .GAL
       for IMG in `find . -maxdepth "1" -name "*.${EXT}" -type "f" | grep -v '\.m\.'${EXT} | grep -v '\.t\.'${EXT} | sort -n`; do
           echo -n "${IMG}"
           BASE=`basename ${IMG} .${EXT}`
# Create the medium image.
           if [ -f ${BASE}.m.${EXT} ]; then
              echo -n "   Medium OK" > /dev/null; else	
              echo -n "   No medium, creating..."
              convert ${IMG} -resize ">x600" ${BASE}.m.${EXT}
              convert ${BASE}.m.${EXT} -resize ">600" ${BASE}.m.${EXT}
           fi
# If the medium image is the same as the fullsize, remove the medium and link the fullsize to it.
	   MATCH="N"
# Don't do this check for animated GIFs, since it will hose things up:
           MEDFILE=`identify ${BASE}.m.${EXT} | awk '{ print $3 }'`
	   FULLFILE=`identify ${BASE}.${EXT} | awk '{ print $3 }'`
	   if [ $MEDFILE = $FULLFILE ];
	      then echo "Medium matches Full-size, linking image"
                   rm ${BASE}.m.${EXT} && ln -s ${BASE}.${EXT} ${BASE}.m.${EXT}
	           MATCH="Y"
	   fi
# Create the thumbnail image.
           if [ -f ${BASE}.t.${EXT} ]; then 
              echo -n "   Thumbnail OK" > /dev/null; else
              echo -n "   No thumbnail, creating..."
              convert ${BASE}.m.${EXT} -thumbnail ">x75" ${BASE}.t.${EXT}
              convert ${BASE}.t.${EXT} -thumbnail ">75" ${BASE}.t.${EXT}
           fi
# Create the html for the medium image.
           if [ -f ${BASE}.html ]; then
              echo -n "   HTML OK" > /dev/null; else
              echo -n "   No HTML, creating..."
              echo "${TOP}" > ${BASE}.html
              cat .GALBREAD >> ${BASE}.html
              echo "<a href=\"${BASE}.html\">${BASE}.html</a><br><a href=\"javascript:javascript:history.go(-1)\">BACK</a><br><hr><a href=\"${BASE}full.html\"\"><img src=\"${BASE}.m.${EXT}\" alt=\"${BASE}.m.${EXT}\"></a>" >> ${BASE}.html
              echo "${BOTTOM}" >> ${BASE}.html
           fi
# Create the html for the fullsize image.
	   if [ $MATCH = Y ];
              then ln -s ${BASE}.html ${BASE}full.html
              else if [ -f ${BASE}full.html ]; then
                      echo "   FULL HTML OK" > /dev/null; else
                      echo "   No FULL HTML, creating..."
                      echo "${TOP}" > ${BASE}full.html
                      cat .GALBREAD >> ${BASE}full.html
                      echo "<a href=\"${BASE}full.html\">${BASE}full.html</a><br><a href=\"javascript:javascript:history.go(-1)\">BACK</a><br><hr><a href=\"${BASE}full.html\"\"><img src=\"${BASE}.${EXT}\" alt=\"${BASE}.${EXT}\"></a>" >> ${BASE}full.html
                      echo "${BOTTOM}" >> ${BASE}full.html
                   fi
           fi
# Make the html read-only
           chmod 444 ${BASE}.html ${BASE}full.html
           grep -v "${BASE}" .GAL > .GALTEMP && mv .GALTEMP .GAL
# Add a link for this image to the index page
           echo "<a href=\"${BASE}.html\"><img src=\"${BASE}.t.${EXT}\" alt=\"${BASE}.${EXT}\"></a>" >> index.html
           echo ""
# Make all images read-only so we're not rewriting them
           chmod 444 *.${EXT}
       done
    fi
done
# After all the images, insert links to files
TEST=`wc -l .GAL | awk '{ print $1 }'`
if [ ${TEST} -gt '1' ]; then
   echo -n '<hr><table width="100%">' >> index.html
   echo    '<tr><td><em>File</em></td><td><em>Filetype</em></td><td><em>Size</em></td><td><em>Last Modified</em></td></tr>' >> index.html
   for FILE in `cat .GAL`; do
       DESCR=`file ${FILE} | awk -F ": " '{ print $2 }'`
       SDESCR=`echo ${DESCR} | cut -nc "1-41"`
       if [ `echo ${SDESCR} | wc -m` -gt '38' ]
          then SDESCR=${SDESCR}...
       fi
       SIZE=`ls -lh ${FILE} | awk '{ print $5 }'`
       MODIFY=`stat ${FILE} | grep "Modify" | cut -d " " -f "2"`
       SFILE=`echo ${FILE} | cut -c "1-40"`
       if [ `echo ${FILE} | wc -m` -gt '38' ]
          then SFILE=${SFILE}...
       fi
       echo -n "<tr><td><a href=\"${FILE}\" alt=\"${DESCR}\" title=\"${DESCR}\">${SFILE}</a></td>" >> index.html
       echo        "<td>${SDESCR}</td><td>${SIZE}bytes</td><td>${MODIFY}</td></tr>"               >> index.html
   done
   echo '</table>' >> index.html
fi
echo "${BOTTOM}" >> index.html
# If there are subdirectories, descend into them, run this script, then back out.
CHECK=$( find . -maxdepth "1" -type "d" | sed -e 's/\.\///g' | grep -v ^\\. | wc -l | awk '{ print $1 }' )
if [ ${CHECK} -gt '0' ]; then
   for DIR in `ls -l | grep ^drw | awk '{ print $9 }'`; do
       cd ${DIR}
       echo "Now working in `pwd`"
       /bin/sh $0
       cd ..
   done
# If the directories are read-only (or r-x), do not process them.
   for DIR in `ls -l | grep ^dr- | awk '{ print $9 }'`; do
       echo "WARNING: ${DIR} not processed, read-only."
   done
# If no permissions to read directory, do not process them.
   for DIR in `ls -l | grep ^d--- | cut -d ":" -f "2" | awk '{ print $2 }'`; do
       echo "WARNING: ${DIR} not processed, unreadable."
   done
fi	
rm -f ./.GAL*

