#!/bin/sh
# genethumb.sh - Génération de thumbnails mortels de ta mère
# (c)  3 Jan 1998: version 0.0.4 by Samuel Hocevar <sam@via.ecp.fr>
#     22 May 2000: version 0.0.5 by Samuel Hocevar <sam@via.ecp.fr>
#     15 Nov 2000: version 0.0.6 by Samuel Hocevar <sam@zoy.org>
#                    with code from Sven Hartge <hartge@ds9.argh.org>
#     13 Dec 2000: version 0.0.7 by Samuel Hocevar <sam@zoy.org>
#                    now compatible with old ImageMagick versions
#                    added --rows option
#     27 Apr 2003: version 0.0.8 by Sam Hocevar <sam@zoy.org>
#                    removed all bashisms
#                    now compatible with all ImageMagick versions
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

version=0.0.8
THFILE=index.html
THDIR=.xvpics
THEXT=png      # change this to jpeg or whatever you want
THWIDTH=120    # thumbnail width
THHEIGHT=90    # thumbnail height
THCOLS=5       # change to set table width -- 0 for no tables

if [ "$1" != "" ]
then
  THCOLS=$1
fi

echo "genethumb.sh v$version -- report bugs to Samuel Hocevar <sam@zoy.org>"
echo "usage: genethumb.sh [number of columns]"

if [ -w $THFILE ]
then
  echo "saving $THFILE to $THFILE~"
  mv -f $THFILE $THFILE~
fi

if [ ! -d $THDIR ]
then
  echo "creating thumbnail directory $THDIR"
  mkdir $THDIR
fi

cat > $THFILE << EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <meta name="GENERATOR" content="genethumb.sh version $version" />
   <meta name="Description" content="thumbnails of $PWD" />
   <title>Index of $PWD</title>
   <style type="text/css">
      img { border: 1pt; }
      body { background: white; color: black;
             font-family: sans-serif, Arial, Helvetica;
             font-size: 9pt; margin: 5pt; }
      h2 { color: black; background: white; font-size: 14pt; }
      a { text-decoration: none; }
      td.center { text-align: center; }
      td.nowrap { white-space: nowrap; }
      blockquote.center { text-align: center; }
   </style>
</head>

<body>

<table border="0" width="100%"><tr><td>
<h2>Index of $PWD</h2></td>
<td align="right">generated by <a
 href="http://sam.zoy.org/projects/unix/genethumb.html">genethumb.sh</a>
 version $version </td></tr></table>

<hr />

<table cellspacing="5" border="0">
  <tr>
EOF

compteur=0
created=0
find . -type f -maxdepth 1 | sed 's,^\./,,' | while read file
do
  # on vérifie que l'image n'est pas déjà un thumbnail ou une page html, et a
  # une extension quelconque (à améliorer un peu)
  case "$file" in

    *~|*.html|*.htm)
      echo "* skipping $file"
      ;;

    *)
      failed=0
      newfile="$THDIR/$file"
      echo -n "* $file: "

      if [ -r "$newfile" ]
      then
        echo "thumbnail already exists."
      else
        convert -geometry ${THWIDTH}x${THHEIGHT} "$file" "$newfile" >/dev/null 2>&1
        if [ ! -r "$newfile" ]
        then
          echo "failed creating."
  	failed=1
        else
          echo "done."
        fi
      fi

      if [ "$failed" = "0" ]
      then

        created="`expr 0$created + 1`"
        if [ -e "$newfile.info" ]
        then
          read filesize coordinates oldcoords compat < "$newfile.info"
        fi

        if expr "$version" ">" "$compat" >/dev/null 2>&1
        then
          read oldcoords filesize << EOF
`identify -format '%wx%h %b' "$file" | head -1`
EOF
          coordinates=`identify -format '%wx%h' "$newfile" | head -1`
          rm -f "$newfile.info"
          echo $filesize $coordinates $oldcoords $version > "$newfile.info"
          echo "  $newfile.info written."
        fi

        j="`echo $file | cut -b1-20`"
        if [ "$file" != "$j" ]
        then
          j="`echo $file | cut -b1-17`..."
        fi

        cat >> $THFILE << EOF
      <td align="center">
        <a href="$file"><img alt="$file ($oldcoords)"
        width="`echo $coordinates | cut -f1 -dx`"
        height="`echo $coordinates | cut -f2 -dx`"
        src="$newfile" /><br />$j<br />$oldcoords ($filesize)</a>
      </td>
EOF
        compteur="`expr 0$compteur + 1`"
        if [ x"$compteur" = "x$THCOLS" ]
        then
          echo "  </tr><tr>" >> $THFILE
          compteur=0
        fi
      fi
  esac
done

echo "done."

cat >> $THFILE << EOF
  </tr>
</table>

</body>
</html>
EOF

