#!/bin/sh
# Change the font variable to point to your font location
font="/Library/Fonts/digital-7 (mono).ttf"

if [ $# -eq 0 ]
   then
      cat << _EOF_

USAGE: $0 file1 file2 ..., or
       $0 *.jpg, or
       $0 dir/*.jpg
       ...

_EOF_
      exit
fi

while [ "$1" != "" ]; do
        # Skip directories
        if [ -d "$1" ]; then
                shift
                continue
        fi
        # Skip already converted files (may get overwritten)
        if [[ $1 == *_DT* ]]
        then
                echo "------  Skipping: $1"
                shift
                continue
        fi

        # Work out a new file name by adding "_DT" before file extension
        file=$1
        echo "######  Working on file: $file"
        filename=${file%.*}
        extension=${file##*.}
        output=${filename}_DT.${extension}

        # Get the file dimension
        dim=$(identify -format "%w %h" "$file")
        width=${dim%% *}
        height=${dim#* }

        # Decide the font size automatically
        if [ $width -ge $height ]
        then
                pointsize=$(($width/30))
        else
                pointsize=$(($height/30))
        fi

        echo "        Width: $width, Height: $height. Using pointsize: $pointsize"

        # The real deal here
        convert "$file" -gravity SouthEast -font "$font" -pointsize $pointsize -fill white -annotate +$pointsize+$pointsize "%[exif:DateTimeOriginal]" "$output"

        shift
done

exit 0
