#!/bin/bash
fs=("3.5" "4" "5.6" "8" "11" "16")
for d in $(find . -type d -mindepth 1)
do
  # echo "${d} start"
  cd "${d}"
  i=0
  for f in $(find . -iname "*.[jJ][pP][gG]" | sort)
  do
    fn="./${i}_f${fs[${i}]}.jpg"
    if [ ! "${f}" = "${fn}" ]
    then
      echo "mv \"${f}\" \"${fn}\""
      mv "${f}" "${fn}"
    fi
    i=$((i+1))
  done
  cd ..
  # echo "${d} end"
done
cp head.html index.html
i=0
for f in $(find . -iname "*.[jJ][pP][gG]" -mindepth 2 | sort)
do
  ff="${f/\.\//}"
  fn="${ff//\//-}"
  if [ ! -f "${fn}" ]
  then
    echo "generating preview '${fn}' for '${f}'..."
    convert "${f}" -geometry "600x450" -quality 70% "${fn}"
  fi
  ufn="${ff//\// }"
  ufn="${ufn//-/ }"
  ufn="${ufn//\.jpg/}"
  for j in 0 1 2 3 4 5
  do
    ufn="${ufn//${j}_/}"
  done
  echo "<a href=\"${f}\">${ufn}<img src=\"${fn}\" alt=\"${ufn}\"></a>" >> index.html
  i=$((i+1))
  if [ "${i}" = "2" ]
  then
    echo "<br />" >> index.html
    i=0
  fi
done
cat tail.html >> index.html
