#!/bin/sh

key=DateTimeOriginal
if [ "$1" = "-k" ]; then
  shift
  key="$1"
  shift
fi

moncmd="montage -label %f\\n%[EXIF:$key] -geometry 120x120+20+20"

if [ $# -eq 0 ]; then
  echo "usage: thumbindex [-k <EXIF key>] <image file> ..."
  echo "thumbindex generates images containing 25 thumbnails each of the image files given on the command line." | fmt
  exit 0;
fi

pagenr=1

while [ $# -ge 25 ]; do

  for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; do
    thispage[$i]=$1
    shift
  done

  if [ $pagenr -lt 10 ]; then
    $moncmd "${thispage[@]}" thumbindex0$pagenr.jpg
  else
    $moncmd "${thispage[@]}" thumbindex$pagenr.jpg
  fi
  pagenr=$((pagenr+1))

done

if [ $# -eq 0 ]; then
  exit 0
fi

if [ $pagenr -lt 10 ]; then
  $moncmd "$@" thumbindex0$pagenr.jpg
else
  $moncmd "$@" thumbindex$pagenr.jpg
fi


