#! /bin/bash

if [ "$2" == "" ]; then
   echo " ** usage: mk_preview.sh <title> <imageDir>" > /dev/stderr
   exit 1
fi

COLS=4
TITLE=$1
DIR=$2

cat <<EOF
 <html>
    <head><title>$TITLE</title></head>
    <style> @import "sheet.css"; </style>
    <body>
      <div class='title'>
       <h2>$TITLE</h2>
      </div>

      <div class='preview'>
       <table><tr>
EOF

row=0
col=0

for i in $DIR/*thumb*; do
   #echo " ::: $col ? $row"
   if [ "$col" == "0" ]; then
       if [ "$row" != "0" ]; then
           echo "         </tr>"
           echo "         <tr>"
       fi
   fi
   i=${i/\/\//\/}
   base=${i##*/}
   base=${base%_thumb*}
   img=${i/_thumb./_tag.}
   page="$base.html"
   
   echo "            <td><center><a href='$page'><img src='$i' /></a><br/>"
   echo "                <a href='$page'>$base</a></center></td>"

   cat > $page <<EOF
 <html>
    <head><title>$TITLE : $base</title></head>
    <style> @import "sheet.css"; </style>
    <body>
      <div class='title'>
       <h2>$TITLE : $base</h2>
      </div>

      <div class='image'>
       <table><tr><td>
          <img class='full' src='$img' />
       <td></tr></table>
      </div>
    </body>
 </html>
EOF


   col=$((col+1))
   if [ "$col" == "$COLS" ]; then
       col=0;
       row=$((row+1))
   fi
done
cat <<EOF
       </tr></table>
      </div>
    </body>
 </html>
EOF
