#!/bin/sh

function help() {
	echo "usage: ${0} [OPTIONS]"
	echo
	echo "where OPTIONS are:"
	echo " -g geometry  geometry to place the signature"
	echo " -h           show this help"
	echo " -i file      input file"
	echo " -l [NorthWest|NorthEast|SouthWest|SouthEast]"
	echo "              use location to embed image"
	echo " -o file      output file"
	echo " -s file      signature file"
	echo
	echo "Default location is SouthEast, default geometry is '+10+10'."
}

GEOMETRY="+10+10"
INFILE=""
LOCATION="SouthEast"
OUTFILE=""
SIGFILE="/usr/share/eworm/artwork/sig.png"

while getopts "g:hi:l:o:s:" opt; do
	 case ${opt} in
		 g)
			 GEOMETRY="${OPTARG}"
			 ;;
		 h)
			 help
			 exit 0
			 ;;
		 i)
			 INFILE="${OPTARG}"
			 ;;
		 l)
			 case "${OPTARG}" in
				 NorthWest)
					 LOCATION="NorthWest"
					 ;;
				 NorthEast)
					 LOCATION="NorthEast"
					 ;;
				 SouthWest)
					 LOCATION="SouthWest"
					 ;;
				 SouthEast)
					 LOCATION="SouthEast"
					 ;;
				 *)
					 echo "Location '${OPTARG}' is unknown." >&2
					 help
					 exit 1
					 ;;
			 esac
			 ;;
		 o)
			 OUTFILE="${OPTARG}"
			 ;;
		 s)
			 SIGFILE="${OPTARG}"
			 ;;
	 esac
done

if [ -z "${INFILE}" ]; then
	echo "No input file given." >&2
	help
	exit 1
elif [ ! -r "${INFILE}" ]; then
	echo "Could not read input file '${INFILE}'." >&2
	help
	exit 1
fi

if [ -z "${OUTFILE}" ]; then
	echo "No output file given." >&2
	help
	exit 1
fi

if [ ! -r "${SIGFILE}" ]; then
	echo "Could not read signature file '${INFILE}'." >&2
	help
	exit 1
fi

composite -geometry "${GEOMETRY}" -gravity "${LOCATION}" "${SIGFILE}" "${INFILE}" "${OUTFILE}"
