#!/bin/bash

###############################################################################
#
# mkindex-html.sh Version 3.1 - By Peter Larkowski and Daniel Drown, 2006-12-5
# 
# Description: A script to turn a dir full o pics into a simple table of
#              thumbnails linked to the original image
#
# Changelog: 2006-12-05 <ddrown> - Version 3.1 Have floating divs auto-sized with javascript
#            2005-07-21 <ddrown> - Version 3.0 Changed to use floating divs
#            2005-07-13 <plarkowski> - Version 2.0 Added fix to support spaces in files and dirs
#            2004-06-03 <plarkowski> - Version 1.0
#
###############################################################################

# Parse command line
if [ x"$1" = x"-h" ] || [ x"$1" = x ]; then
  echo "Usage: mkindex-html.sh TITLE"
  echo ""
  echo "TITLE:   Title for the page"
  echo ""
  exit 1
fi

cat << EOF > index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
   <HEAD>
      <TITLE>$1</TITLE>
<style>
div.float {
 float: left;
 border: 10px solid white;
}
div.float p {
 text-align: center;
}
HR {
 clear: both;
}
</style>
<script><!--
function fixheight(newheight,divlist) {
  for(var j = 0; j < divlist.length; j++) {
    divlist[j].style.height = newheight + "px";
  }
}

function setheights() {
  divs = document.getElementsByTagName("div");
  maxheight = 0;
  lasttop = 0;
  lastdivs = new Array();
  for(var i = 0; i < divs.length; i++) {
    if(divs[i].className == "float") {
      if(lasttop != divs[i].offsetTop) {
        fixheight(maxheight,lastdivs);
        maxheight = 0;
        lastdivs = new Array();
      }
      if(divs[i].offsetHeight > maxheight) {
        maxheight = divs[i].offsetHeight;
      }
      lastdivs.push(divs[i]);
      lasttop = divs[i].offsetTop;
    }
  }
  fixheight(maxheight,lastdivs);
}
--></script>
   </HEAD>
   <BODY bgcolor='white' onload="setheights();">
   <span style="float: left">
    <H2>$1</H2>
   </span>
   <span style="float: right">
    <H2><A href='..'>Up</A></H2>
   </span>
   <HR size=1 width="100%">
EOF

for i in *.[jJ][pP][eE][gG] *.[jJ][pP][gG]; do
  if echo $i | grep -q -- -thumb.jpeg; then
    echo skipping thumbnail $i
  else
    if [ -f "$i" ]; then
      THUMB=`echo $i | sed 's/\.[jJ][pP][eE]*[gG]$/-thumb.jpeg/'`
      echo "$i" -\> $THUMB
      if [ ! -f "$THUMB" ]; then
        convert -geometry 10% "$i" "$THUMB"
      fi
      cat << EOF >> index.html
   <div class=float><a href='$i'><img border=0 src='$THUMB'><p>$i</p></a></div>
EOF
    fi
  fi
done

cat << EOF >> index.html
    <HR size=1 width="100%">
    <FONT size='-1'>
      All pictures Copyright [me]
      <BR>Last Modified on `date`.
    </FONT>
  </BODY>
</HTML>
EOF


