#!/bin/sh
title=$1

html=index2.html
html2=index.html
mkthumb=1

thumbdir=thumbs

maxsimple=5
maxdetail=3

sim='simplifiée'
det='détaillée'

if [ "$title" == "" ]
then 
  title=`pwd | sed 's/.*\///g'`
fi

i=1
j=1

entete ()
{
  file=$1
  if [ $file == $html ]
  then
    file2=$html2
    simpl=$det
    simpl2=$sim
  else
    file2=$html
    simpl=$sim
    simpl2=$det
  fi

  cat >$file <<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>$title</title>
  <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
  <link href="/css/general.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 align=center> $title </h1> 


<table width="100%" border="1" cellspacing="0" cellpadding="2" bgcolor="#dbcece" frame="hsides" rules="none">
  <tr align=center>
    <td width="30%">
      <a href="/photos/">Retour a la page photos</a>
    </td>
    <td>
      <a href="$file2">Version $simpl2</a>
    </td>
  </tr>
</table>
<br>

<table width="98%" align=center border="1" cellspacing="0" cellpadding="2" bgcolor="#eeeeee" frame="border" rules="all">
EOF

}

entete $html
entete $html2

if [ ! -d "$thumbdir" ]
then
  mkdir "$thumbdir"
fi


ls | grep -v html$ | grep -v sh$ | while read file
do

  if [ ! -d $file ]
  then
    ext=${file##*.}
    prefix=`echo ${file/.$ext} | sed 's/_/ /g'`

    if [ "$mkthumb" == "1"  ]
    then
      if [ ! -f "$thumbdir/${file/.$ext}.jpg" ]
      then
        convert "${file}" -geometry 160x120 "$thumbdir/${file/.$ext}.jpg"
      fi
    fi

    if [ $i -eq 1 ]
    then
      echo "  <tr align=center valign=top>" >>$html
    fi

    if [ $j -eq 1 ]
    then
      echo "  <tr align=center valign=top>" >>$html2
    fi

    echo "    <td>" >>$html
    echo "      <a href=\"$file\">" >>$html
    echo "        <img src=\"$thumbdir/${file/.$ext}.jpg\" alt=\"${file}\">" >>$html
    echo "      </a>" >>$html
    echo "      <h3>$prefix</h3>" >>$html
    echo "      <pre>" >>$html
    exiftags -i $file >>$html
    echo "      </pre>" >>$html
    echo "    </td>" >>$html

    echo "    <td>" >>$html2
    echo "      <a href=\"$file\">" >>$html2
    echo "        <img src=\"$thumbdir/${file/.$ext}.jpg\" alt=\"${file}\">" >>$html2
    echo "      </a>" >>$html2
    echo "      <h3>$prefix</h3>" >>$html2
    echo "    </td>" >>$html2

    i=`expr $i + 1`
    if [ $i -gt $maxdetail ]
    then
      echo "  </tr>" >>$html
      i=1
    fi

    j=`expr $j + 1`
    if [ $j -gt $maxsimple ]
    then
      echo "  </tr>" >>$html2
      j=1
    fi
  fi
done


if [ $i -ne 1 ]
then
  echo "  </tr>" >>$html
fi

if [ $j -ne 1 ]
then
  echo "  </tr>" >>$html2
fi
echo "</table>" >>$html
echo "</body></html>" >>$html

echo "</table>" >>$html2
echo "</body></html>" >>$html2



