#!/bin/csh -f

#  $Header: /cvsroot/mpdist/mpdist/mimep/shells/printmim,v 1.1.1.1 2002/04/12 16:47:26 richbastard Exp $
#
#  Copyright (c) Mikael Cam.
#                All rights reserved.
#
# This software is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This software is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
# License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this software; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

	if (($#argv >= 2) && ($?MIMEPOUT)) then

set CONTENT=`echo $1 | sed -e 'y%ABCDEFGHIJKLMNOPQRSTUVWXYZ%abcdefghijklmnopqrstuvwxyz%'`
		
# This shell is called by metamail to treat each content-type of a MIME message

if ($?MIMEPTMPDIR) then
	set TMPDIR=$MIMEPTMPDIR
else
	set TMPDIR=/tmp
endif

	set TMPFNAME=$TMPDIR/MIMEP$$


	switch ($CONTENT)
		
		case image/gif:
			cp ${2} ${TMPFNAME}.gif
			convert ${TMPFNAME}.gif ${TMPFNAME}.ps
if (-e $TMPFNAME.ps) then
			echo "\begin{figure}[H]" >> $MIMEPOUT
			echo "\centerline{\includegraphics{"${TMPFNAME}.ps"}}" >> $MIMEPOUT
			echo "\end{figure}" >> $MIMEPOUT
endif
			/bin/rm $TMPFNAME.gif 
		breaksw

		case image/jpeg:
			cp ${2} $TMPFNAME.jpeg
			convert $TMPFNAME.jpeg $TMPFNAME.ps
if (-e $TMPFNAME.ps) then
			echo "\begin{figure}[H]" >> $MIMEPOUT
			echo "\centerline{\includegraphics{"$TMPFNAME.ps"}}" >> $MIMEPOUT
			echo "\end{figure}" >> $MIMEPOUT
endif
			/bin/rm $TMPFNAME.jpeg 
		breaksw

		case image/tiff:
			cp ${2} ${TMPFNAME}.tiff
			convert ${TMPFNAME}.tiff ${TMPFNAME}.ps
if (-e $TMPFNAME.ps) then
			echo "\begin{figure}[H]" >> $MIMEPOUT
			echo "\centerline{\includegraphics{"${TMPFNAME}.ps"}}" >> $MIMEPOUT
			echo "\end{figure}" >> $MIMEPOUT
endif
			/bin/rm $TMPFNAME.tiff 
		breaksw
	
		case image/x-xwd:
			cp ${2} $TMPFNAME.xwd
			convert $TMPFNAME.xwd $TMPFNAME.ps
if (-e $TMPFNAME.ps) then
			echo "\begin{figure}[H]" >> $MIMEPOUT
			echo "\centerline{\includegraphics{"$TMPFNAME.ps"}}" >> $MIMEPOUT
			echo "\end{figure}" >> $MIMEPOUT
endif
			/bin/rm $TMPFNAME.xwd 
		breaksw

		case text/enriched:
			ertf2latex -onlybody ${2} >> $MIMEPOUT
		breaksw

		case text/richtext:
			ertf2latex -onlybody ${2} >> $MIMEPOUT
		breaksw

		case application/postscript:

			cp ${2} ${TMPFNAME}.ps
			iseps ${TMPFNAME}.ps
		if(${status} == 1) then
if (-e $TMPFNAME.ps) then
			echo "\begin{figure}[H]" >> $MIMEPOUT
			echo "\centerline{\includegraphics{"${TMPFNAME}.ps"}}" >> $MIMEPOUT
			echo "\end{figure}" >> $MIMEPOUT
endif
		else
		echo "Postscript included..." >> $MIMEPOUT 	
			if($?MIMEPPRINTER) then
				lpr -P${MIMEPPRINTER} ${TMPFNAME}.ps
			else 
				lpr ${TMPFNAME}.ps
				 
			endif
		endif
		breaksw

		case text/plain:
			echo "\begin{flushright}" >> ${MIMEPOUT}
			echo "\begin{verbatimtab}" >> ${MIMEPOUT}
			text2latex ${2} | expand | cutlines >> ${MIMEPOUT}
			echo "\end{verbatimtab}" >> ${MIMEPOUT}
			echo "\end{flushright}" >> ${MIMEPOUT}	
		breaksw

		case message/external-body:

			xterm -e showexternal $2 $3 $4 $5 $6 $7 $8
			mimepurl $3 $4 $6 $5
		breaksw

		case message/rfc822:
			if($?MIMEPPRINTER) then
				mp -printer ${MIMEPPRINTER} ${2}
			else 
				mimep ${2}
			endif
		breaksw

		default:
			echo "Erreur de printmim!" >> ${MIMEPOUT}
		breaksw
	endsw

if ($?MIMEPBOUND) then
	if(${MIMEPBOUND} != "none") then
		echo "\begin{center}" >> $MIMEPOUT			
		if(${MIMEPBOUND} == "default") then
			if($?MIMEPRULEH) then 
			echo "\rule{\textwidth}{"${MIMEPRULEH}"pt}" >> $MIMEPOUT
			endif
		else
		
			echo "\includegraphics{"${MIMEPBOUND}"}" >> $MIMEPOUT
		endif
	echo "\end{center}" >> $MIMEPOUT
	
	endif

endif

endif
