#!/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 white -font "$font" +dither     \
	  -pointsize 11                                           \
	    -gravity southwest                                    \
	      -draw "text 5,5 'https://freedoom.github.io/'"      \
	    -gravity southeast                                    \
	      -draw "text 10,5 'Version: $VERSION'"               \
	    "$@"                                                  \
	    $output_file
}

if [ $# = 4 ]; then
	draw_with_footer "$1" "$4"                                          \
	  -gravity north                                                    \
	  -draw "image over -6,2 0,0 '$2'"                                  \
	  -pointsize 20                                                     \
	    -draw "fill black stroke-width 4 stroke black text -5,160 '$3'" \
	    -draw "text -5,160 '$3'"
else
	draw_with_footer "$1" "$2"
fi

