#!/bin/sh
# WARNING: This file was auto-generated. Do not edit!
# $Log: mailzipper.in,v $
# Revision 2.1  2009-06-10 13:44:57+05:30  Cprogrammer
# mailzipper
#
#
TMPDIR=/tmp/mailZipper
OUTPUTDIR=/tmp/final # this will depend on your web server setup
URL=http://some.where.in.the.ocean/mail_files
EXTRACT=yes # yes implies compression
COMPRESS=yes
SECS=`date '+%s'`
if [ ! -d $OUTPUTDIR ] ; then
	/bin/mkdir -p $OUTPUTDIR
	if [ $? -ne 0 ] ; then
		exit 111
	fi
fi
/bin/cat > /tmp/msg_zipper.$$
if [ " $MAX_ATTACHMENT_SIZE" = " " ] ; then
	MAX_ATTACHMENT_SIZE=5000000
fi
mail_size=`/bin/ls -l /tmp/msg_zipper.$$ | awk '{print $5}'`
if [ $mail_size -le $MAX_ATTACHMENT_SIZE ] ; then
	exec 0</tmp/msg_zipper.$$
	/bin/rm -f /tmp/msg_zipper.$$
	/bin/cat
	exit 0
fi
/var/indimail/bin/822header </tmp/msg_zipper.$$ > /tmp/msg_zipper.header.$$
/var/indimail/bin/822body </tmp/msg_zipper.$$ > /tmp/md5.$$
exec 3</tmp/msg_zipper.header.$$
/bin/rm -f /tmp/msg_zipper.header.$$
md5=`md5sum /tmp/md5.$$ | awk '{print $1}'`
/bin/rm -f /tmp/md5.$$
/bin/mkdir -p $TMPDIR/"$$"."$SECS"
if [ $? -ne 0 ] ; then
	/bin/rm -f /tmp/msg_zipper.$$
	/bin/rm -r $TMPDIR/"$$"."$SECS"
	exit 111
fi
if [ " $EXTRACT" = " yes" ] ; then
	/var/indimail/bin/ripmime -i /tmp/msg_zipper.$$ --name-by-type -d $TMPDIR/"$$"."$SECS"
	if [ $? -eq 0 ] ; then
		count=`ls $TMPDIR/"$$"."$SECS" | wc -l`
		if [ $count -eq 1 ] ; then # no attachments
			exec 0</tmp/msg_zipper.$$
			/bin/rm -f /tmp/msg_zipper.$$
			/bin/rm -r $TMPDIR/"$$"."$SECS"
			/bin/cat
			exit 0
		else
			/bin/rm -f /tmp/msg_zipper.$$
		fi
	else
		/bin/rm -f /tmp/msg_zipper.$$
		/bin/rm -r $TMPDIR/"$$"."$SECS"
		exit 111
	fi
	COMPRESS=yes
else
	/bin/mv /tmp/msg_zipper.$$ $TMPDIR/"$$"."$SECS"/$md5.$$_$SECS.eml
	if [ $? -ne 0 ] ; then
		/bin/rm -f /tmp/msg_zipper.$$
		/bin/rm -r $TMPDIR/"$$"."$SECS"
		exit 111
	fi
fi
cat 0<&3
echo
echo "Your mail has been detached and saved at the following location"
if [ " $COMPRESS" = " yes" ] ; then
	zip -q -D -r -j -m $OUTPUTDIR/$md5.$$_$SECS.zip $TMPDIR/"$$"."$SECS"
	if [ $? -ne 0 ] ; then
		/bin/rm -r $TMPDIR/"$$"."$SECS"
		exit 111
	fi
	echo "$URL"$OUTPUTDIR"/$md5."$$"_"$SECS".zip"
else
	/bin/mv $TMPDIR/"$$"."$SECS"/$md5."$$"_"$SECS".eml $OUTPUTDIR
	if [ $? -ne 0 ] ; then
		/bin/rm -r $TMPDIR/"$$"."$SECS"
		exit 111
	fi
	echo "http://some.where.in.the.ocean/$OUTPUTDIR/$md5."$$"_"$SECS".eml"
fi
/bin/rm -r $TMPDIR/"$$"."$SECS"
echo "Beware that detaching the attachment may have impacted mail"
echo "signatures, virus scanning etc. This document is available at the"
echo "above location for a limited amount of time. Make sure to save"
echo "a local copy."
echo
echo "unfaithfully yours"
echo "IndiMail"
