#!/bin/sh

find_font() {
	local font fontlist
	fontlist=$(convert -list font | awk '$1=="Font:" { print $2 }')
	for font in "$@" ; do
		if echo $fontlist | grep -q $font ; then
			echo $font
			return
		fi
	done
}

font=$(find_font Helvetica-Bold Liberation-Sans-Bold DejaVu-Sans-Condensed-Bold)
if [ -z "$font" ] ; then echo "Cannot find any fonts" ; exit 1 ; fi

draw_with_footer() {
	input_file=$1;
	output_file=$2;
	shift; shift

	convert $input_file -fill orange -font "$font" +dither                 \
	  -pointsize 11                                                        \
	    -gravity southwest                                                 \
	      -draw "text 5,5 '© 2001-2017'"                                   \
	    -gravity southeast                                                 \
	      -draw "text 10,5 '$VERSION'"                                     \
	    "$@"                                                               \
	    $output_file
}

if [ $# = 4 ]; then
	draw_with_footer "$1" "$4"                                          \
	  -gravity north                                                    \
	  -draw "image over 0,18 0,0 '$2'"                                  \
	  -gravity south                                                    \
	  -draw "image over 0,30 0,0 '$3'"
else
	draw_with_footer "$1" "$2"
fi
