#!/bin/sh

# 'img_annotate.sh'
#
# - adds a text to the bottom of an image
# - at present, I assume white background
# - only for jpeg images
#
# -----------------------------------------------------------------
# Creation:  2002-04-16, JHa
# Revisions: 2002-04-16, JHa (first version)
#            2002-10-20, JHa (misc small things)
#            2002-10-30, JHa (default font is now Verdana 12 pt)
#            2002-12-28, JHa (some more error checking)
#            2004-12-24, JHa (changed height of text field)
#            2008-06-12, JHa (adapted to IM v6 'montage' and simplified)
# -----------------------------------------------------------------
# Copyright (c) 2002...2008 Joerg Hau <joerg.hau(at)dplanet.ch>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License, 
# version 2 as published by the Free Software Foundation.
#
# 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.
# -----------------------------------------------------------------

# Annotation text to be included in the final file
#
NOTICE="(c) Joerg Hau <joerg.hau(at)dplanet.ch>"


# if script is called w/o arguments, give usage instructions
#
if (( $# != 2 )) ;  then
    echo "Syntax: ${0##*/} file annotation"
    echo "To use this with a list of files, use e.g. 'cat file | xargs -l1 `basename $0`'"
    exit 1
fi

# check for some required executables, exit if not found
# (no need to check for 'convert'; is part of the same package)
#
MONTAGE=`which montage`
if [ ! -x $MONTAGE ] ; then
	echo "'montage' not found ... please install 'ImageMagick'."
	exit 1
fi

file=$1
shift

# remove filename extension (if present)
#
fn=${file%.*}

# re-build filename (this time it has the extension jpg)
#
file=${fn}.jpg

if [ ! -r $file ] ; then
	echo "'$file' not found; exiting."
	exit 1
fi

# must pack the whole stuff in double quotes
#
text=$1
#text=\"`echo $text`\"

echo -n "Working on $file: $text ..."

# hint: use 'convert -list font' to get the list of fonts ;-)
# New image has suffix "t" for "text" 
#
$MONTAGE -geometry +0+10 -font Verdana-Regular -label "$text" $file ${fn}t.jpg

# check status of last cmd, abort if not OK
#
if [ $? != 0 ]; then
	echo " *** Error occurred. Exiting."
	exit 1
fi

echo " done".

exit 0



