#!/bin/sh
# asimplegal.sh v.0.1
# http://code.dayid.org/asimplegal/
# Originally written and distributed by Dayid Alan <code@dayid.org>
# Published under the Pancake Public License v.1.0
# available at: http://code.dayid.org/ppl/ppl.txt
#
# Rename all files to remove silly spaces from the filename.
# We don't need to work with stupid windowisms.
for file in *; do
    new=`echo ${file} | tr ' ' '_'`
    if [ "${new}" != "${file}" ]; then
       mv "${file}" "${new}"
       echo "renamed ${file}" -> "${new}"
    fi
done
# Take a look at all the files in the cwdrent dir, but ignore hidden files:
ls -l | awk '{ print $9 }' | grep -v 'mediums.html' | grep -v 'index.html' > .gal-$$
# Create the header for the index file and echo it into the index.html:
echo "<!--#include virtual=\"/fragment/top.fhtml\" --><div id=\"gallery\">" > index.html
echo -n '<a href="http://www.dayid.org/">www.dayid.org</a> / ' > .gal-$$D
# Find out and assign to variable our cwdrent working directory:
cwd=`pwd | awk -F "public_html/" '{ print $2 }'`
dir=`echo ${cwd} | cut -d "/" -f "1"`
olddir=''
# Create a nice heading with all parent dirs to show where we are.
while [ `echo ${cwd} | wc -w` -gt '0' ]; do
      echo -n "<a href=\"${olddir}/${dir}\">${dir}</a> / " >> .gal-$$D
      cwd=`echo ${cwd} | awk -F "${dir}/" '{ print $2 }'`
      olddir=${olddir}/${dir}
      dir=`echo ${cwd} | cut -d "/" -f "1"`
done
# Add all the links for the parent directories to the index:
cat .gal-$$D >> index.html
# Add a line to the index file before we start putting files:
echo '<hr>' >> index.html
# See how many directories we have - if any:
test=`ls -l | grep -c ^d`
# If there are subdirectories, create a table to contain them and links to them.
if [ ${test} -gt '0' ]; then
   echo '<table width="100%">
   <tr><td><b>Folder:</b></td><td><b>Description:</b></td></tr>' >> index.html
# For each subdirectory, see if there's a description in .htaccess, and create a link to the subdir.
   for dir in `ls -l | grep ^d | awk '{ print $9 }'`; do
# Check for and get descriptions:
       if [ -f .htaccess ]
          then desc=`grep ${dir} .htaccess 2>/dev/null | cut -d '"' -f "2"`
          else desc=''
       fi
       echo "<tr><td><a href=\"${dir}\">${dir}</a></td><td>${desc}</td></tr>" >> index.html
       grep -v "${dir}" .gal-$$ > .gal-$$G
       mv .gal-$$G .gal-$$
   done
# After all the subdirectories are added finish the table and add a line.
   echo '</table><hr>' >> index.html
fi
# Define filetypes to be processed by imagemagick's convert, and process them:
for ext in `echo jpg png bmp tif jpeg gif JPG PNG BMP TIF JPEG GIF`; do
    check=`ls -l *.${ext} 2> /dev/null | grep -v '\.m\.'${ext} | grep -v '\.t\.'${ext} | wc -l | awk '{ print $1 }'`
# If there are more than 0 of any filetype, process them.
    if [ ${check} -gt '0' ]; then
       grep -v "\.${ext}" .gal-$$ > .gal-$$G
       mv .gal-$$G .gal-$$
       for img in `ls *.${ext} | grep -v '\.m\.'${ext} | grep -v '\.t\.'${ext}`; do
           echo -n "${img}"
           base=`basename ${img} .${ext}`
# Create the medium image.
           if [ -f ${base}.m.${ext} ]; then
              echo -n "   Medium for ${img} exists." > /dev/null; else	
              echo -n "   No medium for ${img} yet, creating it..."
              convert ${img} -resize ">x600" ${base}.m.${ext}
              convert ${base}.m.${ext} -resize ">600" ${base}.m.${ext}
           fi
# Create the thumbnail image.
           if [ -f ${base}.t.${ext} ]; then 
              echo -n "   Thumbnail for ${img} exists." > /dev/null; else
              echo -n "   No thumbnail for ${img} yet, creating it..."
              convert ${base}.m.${ext} -thumbnail ">x75" ${base}.t.${ext}
              convert ${base}.t.${ext} -thumbnail ">75" ${base}.t.${ext}
           fi
# Create the html for the image.
           if [ -f ${base}.html ]; then
              echo "   HTML for ${img} exists." > /dev/null; else
              echo "   No HTML for ${img} yet, creating it..."
              echo '<!--#include virtual="/fragment/top.fhtml" -->' > ${base}.html
              echo -n '<div id="gallery">' >> ${base}.html
              cat .gal-$$D >> ${base}.html
              echo "<a href=\"${base}.${ext}\">${base}.${ext}</a><hr><a href=\"./index.html\"><img src=\"${base}.m.${ext}\" alt=\"${base}.${ext}\"></a>" >> ${base}.html
              echo '</div><!--#include virtual="/fragment/bottom.fhtml" -->' >> ${base}.html
              chmod 444 ${base}.html
           fi
           grep -v "${base}.html" .gal-$$ > .gal-$$G
           mv .gal-$$G .gal-$$
# Add a link to the html/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 that we don't need to rewrite them continuously.
           chmod 444 *.${ext}
       done							
    fi
done
test=`wc -l .gal-$$ | awk '{ print $1 }'`
if [ ${test} -gt '0' ]; 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
       desc=`file ${file} | awk -F ": " '{ print $2 }'`
       shortdesc=`echo ${desc} | cut -nc "1-41"`
       if [ `echo ${shortdesc} | wc -m` -gt '38' ]
          then shortdesc=${shortdesc}...
       fi
       size=`ls -lh ${file} | awk '{ print $5 }'`
       modify=`stat ${file} | grep "Modify" | cut -d " " -f "2"`
       shortfile=`echo ${file} | cut -c "1-40"`
       if [ `echo ${file} | wc -m` -gt '38' ]
          then shortfile=${shortfile}...
       fi
       echo -n "<tr><td><a href=\"${file}\" alt=\"${desc}\" title=\"${desc}\">${shortfile}</a></td>" >> index.html
       echo        "<td>${shortdesc}</td><td>${size}bytes</td><td>${modify}</td></tr>"               >> index.html
   done
   echo '</table>' >> index.html
fi
echo '</div>' >> index.html
echo '<!--#include virtual="/fragment/bottom.fhtml" -->' >> index.html
check=`ls -l | grep ^d | wc -l | awk '{ print $1 }'`
# If there are subdirectories, descend into them, run this script, then back out.
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*

