#!/bin/bash
#
# thumbs.sh directory
#
# A BASH script to generate thumbnails and JPEGs for websites from a GIMP .xcf format input file
# (c) Malcolm Smith 2004-08-10...2006-05-20
# Licensed for free use under the GNU GPL; see http://www.gnu.org/
# Requires: ImageMagick

# This script will convert all .xcf files in the directory supplied as an argument
# (but ignores any _ORIGINAL.xcf files) to both a thumbnail and a JPEG and put
# them in a new directory called ./WebJPEGs and ./WebJPEGs/tn

# ToDo:
#   Sort out why it doesn't _IGNORE files
#   Fix auto-sizing
#   Hack the ls output to produce HTML tags ready for copying onto web pages ;-)

inputfileformat='xcf'       # only process GIMP .xcf files
tnheight=100                # Height of thumbnail
scale=33                    # third-sized JPEG
jpegquality=85              # JPEG compression quality
thumbquality=75             # thumbnail compression quality
count=0                     # Counts the number of files processed
clear
date
echo -e '\nThis shell script generates JPEGs and thumbnails from .xcf files'
echo -e 'Please wait - processing takes a while...\n'

# the directory to use, the first argument to the thumbs command; program exits with a message if none given
directory=${1:?"You must specify which directory to process as an argument, e.g.  thumbs.sh ~/MyPics/"}
# if they don't already exist, make directories to put the JPEGs in
if ! [ -e "${directory}WebJPEGs" ]
then
  mkdir ${directory}WebJPEGs
  echo 'Creating a directory '$directory'WebJPEGs'
fi
if ! [ -e "${directory}WebJPEGs/tn" ]
then
  mkdir ${directory}WebJPEGs/tn
  echo 'Creating a directory '$directory'WebJPEGs/tn'
fi

# process all valid files in that directory
for filename in $( ls $directory*.$inputfileformat );
do
  # ignore *_IGNORE* files - these are pictures not deemed good enough to be processed
  # ignore *_ORIGINAL* files - these files will not be processed; use CROPped or TOUCHed versions instead
  # ignore *---* files - these are CROPped fragments which have been manually edited, and don't need processing
  if [ "${filename}" = "*_IGNORE*" ] || [ "${filename}" = "*_ORIGINAL*" ] || [ "${filename}" = "*---*" ]
  then
    echo $filename' is an invalid file; not processing.'    # Damn - this doesn't work. I bet my syntax is wrong? :-/
  else
    width=$(identify -format %w $filename)     # find image size
    height=$(identify -format %h $filename)

# Damn - this still doesn't work properly either...
# The idea is to automatically choose a scaling factor depending on the size of the original
# which will result in an image that fits in a common or garden browser window
#
#    if [ $(($width)) -gt $(($height)) ]        # determine if image is in landscape or portrait format
#    then
#      scale=$((100 / $(($height / 600))))      # reduced-sized JPEG  <---------- FLOATING POINT ARITHMETIC REQUIRED?
#    else
#      scale=50                                 # half-sized JPEG     <---- COMMENT OUT AS DESIRED...
#      scale=33                                 # third-sized JPEG
#      scale=25                                 # quarter-sized JPEG
#    fi

    # define output filenames for $imagename and $thumbname
    file=${filename%.$inputfileformat}                       # strip off .xcf
    photopath=${file%_*}                                     # strip off anything after _
    photoname=${photopath##*/}                               # strip off directories
    imagename=${directory}WebJPEGs/${photoname}.jpg          # define filename of JPEG image
    thumbname=${directory}WebJPEGs/tn/${photoname}.jpg       # define filename of thumbnail

    echo 'File number '$((count+1))' is called '$filename' and is sized '$width'x'$height', to be scaled at '$scale'%.'
#    echo 'Photo number '$((count+1))' is called '$photoname
#    echo 'Image number '$((count+1))' is called '$imagename
#    echo 'Thumb number '$((count+1))' is called '$thumbname

    # if a file $imagename already exists, add -1 to the name to make it original
    while [ -e "$imagename" ]
    do
      imagename=${imagename%.jpg}-1.jpg
      echo 'File '$imagename' exists, renaming it.'
    done
    while [ -e "$thumbname" ]
    do
      thumbname=${thumbname%.jpg}-1.jpg
      echo 'File '$thumbname' exists, renaming it.'
    done

    # Create image and thumbnail
    echo 'Creating image '$imagename
    echo 'and thumbnail  '$thumbname
#    echo 'from filename  '$filename
    convert -size $scale% $filename -resize $scale% +profile "*" \
      -compress JPEG -quality $jpegquality $imagename
    convert -size x$tnheight $filename -resize x$tnheight +profile "*" \
      -compress JPEG -quality $thumbquality $thumbname

    echo -e 'OK\n'
    count=$((++count))
  fi
done

echo 'Directory traversed - created '$count' JPEG images with thumbnails.'
ls -lR ${directory}WebJPEGs > ${directory}WebJPEGs/dirlist
ls --color=always -lR ${directory}WebJPEGs
date

