#!/bin/sh

# ===========================================================
# Print documents via system tools.
#
# 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-print_doc" 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/
#
# See
#
#	client/pycommon/gmPrinting.py::known_printjob_types
#
# for a list of print job type tags.
#
# ===========================================================
# $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/external-tools/gm-print_doc,v $
# $Id: gm-print_doc,v 1.7 2010-01-19 15:49:07 ncq Exp $
# ===========================================================

TYPE="$1"			# this is a tag as to what type of print job this is
shift 1
FILES="$@"			# this is / those are the file(s) to print


# detect printer manager
CALL_PRINTER_MANAGER=""

# kprinter ?
if [ "${CALL_PRINTER_MANAGER}" = "" ]; then
	A=`which kprinter`
	if [ $? = 0 ]; then
		CALL_PRINTER_MANAGER="kprinter -c ${FILES}"
	fi
fi

# gtklp ?
if [ "${CALL_PRINTER_MANAGER}" = "" ]; then
	A=`which gtklp`
	if [ $? = 0 ]; then
		CALL_PRINTER_MANAGER="gtklp -i ${FILES}"
	fi
fi

# Darwin/MacOSX ?
if [ "${CALL_PRINTER_MANAGER}" = "" ]; then
	SYSTEM=`uname -s`
	if [ ${SYSTEM} = "Darwin" ]; then
		CALL_PRINTER_MANAGER="open -a Preview ${FILES}"
		#CALL_PRINTER_MANAGER="open ${FILES}"
	fi
fi

# generic badness ?
if [ "${CALL_PRINTER_MANAGER}" = "" ]; then
	A=`which acroread`
	if [ $? = 0 ]; then
		CALL_PRINTER_MANAGER="acroread ${FILES}"
	fi
fi

# nothing found ?
if [ "${CALL_PRINTER_MANAGER}" = "" ]; then
	echo ""
	echo "Cannot find any of kprinter, gtklp, MacOSX/Darwin, or acroread."
	echo "Cannot print document(s)."
	echo ""
	exit 127
fi


# start printing
if [ "${TYPE}" = "generic_document" ]; then
	${CALL_PRINTER_MANAGER}
	exit $?
fi


if [ "${TYPE}" = "medication_list" ]; then
	${CALL_PRINTER_MANAGER}
	EXIT_CODE=$?
	rm -f ${FILES}
	exit ${EXIT_CODE}
fi


echo ""
echo "Unknown print job type <${TYPE}>. Cannot print document(s)."
echo ""

exit 127
# ===========================================================
# MacOSX:
#
# alternate Mac methods to open the PDF in Preview
# (uncomment one of the following)
# open -a Preview <filename> or
# osascript -e 'tell application "Preview" to open ((POSIX file "fullySpecifiedFilenameInQuotes") as text)' 
#
# Figured I may as well also capture a link to the following
# method, in case we should in future (on Macs) pass
# parameters into an AppleScript:
#
# 	http://developer.apple.com/mac/library/qa/qa2001/qa1111.html
#
#	http://forums.macosxhints.com/showthread.php?s=&threadid=19736
#
#
# Windows:
#
# - freeware 'PrintFile" (as at 2009) supports batch command piping
# - use "AcroRd32.exe /s /o /h /p $FILES" (requires Acrobat Reader to be installed)
# - refer to http://www.robvanderwoude.com/printfiles.php#PrintPDF
#
# ===========================================================
# $Log: gm-print_doc,v $
# Revision 1.7  2010-01-19 15:49:07  ncq
# - properly detect Darwin
# - error message on unknown job type
#
# Revision 1.6  2010/01/17 22:43:04  ncq
# - much improve printer interface detection
#
# Revision 1.5  2010/01/15 12:44:10  ncq
# - add docs on possible Windows handling
#
# Revision 1.4  2010/01/13 21:52:23  ncq
# - document how Mac might work
#
# Revision 1.3  2010/01/11 22:04:50  ncq
# - support either of kprinter / gtklp
#
# Revision 1.2  2010/01/01 21:24:19  ncq
# - documentation
# - improved exit code use
# - generic_document
#
# Revision 1.1  2009/12/23 13:08:08  ncq
# - renamed to please Debian
#
# Revision 1.1  2009/12/21 23:04:26  ncq
# - default print script
#
#