#! /bin/bash -e

USAGE=( \
  "[ obsolete - see annotate_image.sh ]\n"
  "annotate_pic {-help|-info}\n"
  "annotate_pic [-debug] {SOURCE_IMG} {TEXT_PTSIZE} {TEXT_COLOR} {STROKE_COLOR} {COMMAND}...\n"
)

paux="${HOME}/lib/make_annotate_image_commands.py"

debug=0
debug_opts=( )
while [[ ( $# -ge 1 ) && ( "/$1" =~ /- ) ]]; do
  if [[ ( "/$1" == "/-help" ) || ( "/$1" == "/--help" ) || \
      ( "/$1" == "/-info" ) || ( "/$1" == "/--info" ) ]]; then
    echo "USAGE: ${USAGE[@]}" 1>&2 
    ${paux} -info
    exit 0
  elif [[ "/$1" == "/-debug" ]]; then
    debug=1; shift
    debug_opts=( -debug True )
  else
    echo "** invalid option '$1'" 1>&2;
    echo "USAGE: ${USAGE[@]}" 1>&2 
    exit 1
  fi
done

src="$1"; shift   # Source image
text_size="$1"; shift;
text_color="$1"; shift;
stroke_color="$1"; shift;

tmp="/tmp/$$"

# Get source image size:
size=( `convert ${src} -print "%[fx:w] %[fx:h]\n" null: `)
echo "image size = ${size[@]}" 1>&2 

echo "convert ${src} \\" > ${tmp}.cmds

${paux} \
    ${debug_opts[@]} \
    -imagesize ${size[@]} \
    -textsize ${text_size} \
    -textcolor ${text_color} \
    -color ${stroke_color} \
    "${@}" \
  >> ${tmp}.cmds

echo "  ${tmp}.png" >> ${tmp}.cmds

if [[ ${debug} -ne 0 ]]; then
  echo "draw commands:" 1>&2
  cat ${tmp}.cmds 1>&2
fi

source ${tmp}.cmds

# display ${tmp}.png

cat ${tmp}.png
rm ${tmp}.png ${tmp}.cmds
