#!/bin/sh

# ===========================================================
# Convert files from one format into another.
#
# The GNUmed client expects to be able to run this command
# in a systemwide way, ie. it needs to be accessible in the
# executable $PATH (such that "which gm-convert_file" gives a
# useful answer). There can be several copies per system in
# which way users can override a system default script with
# their own.
#
# Typical locations for this script would be
#
#	/usr/bin/
#	/usr/local/bin/
#	~/bin/
#
# This is just an example. You must install ImageMagick
# to actually use it.
#
# ===========================================================
# GNUmed calling API:
INPUT_FILE="$1"
TARGET_MIME="$2"
TARGET_EXTENSION="$3"
TARGET_FILENAME="$4"

# ----------------------------------------------------------
# requires imagemagick
CONVERTER1="convert -regard-warnings -verbose "
CONVERTER2="convert -verbose -debug All"
CMD_LINE="${INPUT_FILE} ${TARGET_FILENAME}"


if test -z ${TARGET_FILENAME} ; then
	echo "=============================================================================================="
	echo "Usage:"
	echo " $0 <input file> <target mime type> <target file extension> <target filename>"
	echo ""
	echo "Given:"
	echo " $0 ${CMD_LINE}"
	echo "=============================================================================================="
	exit 1
fi


rm -f ${TARGET_FILENAME}
RUN_CONVERTER="${CONVERTER1} ${CMD_LINE}"
${RUN_CONVERTER}
RESULT="$?"
if test ${RESULT} -eq 0 ; then
	exit 0
fi
echo "error (${RESULT}): ${RUN_CONVERTER}"
rm -f ${TARGET_FILENAME}


RUN_CONVERTER="${CONVERTER2} ${CMD_LINE}"
${RUN_CONVERTER}
RESULT="$?"
if test ${RESULT} -eq 0 ; then
	exit 0
fi
echo "error (${RESULT}): ${RUN_CONVERTER}"
exit ${RESULT}
# ===========================================================
