#!/bin/bash

BILLING_PATH="$HOME/Scripts/Billing"

echo "-----------------------------------------------" >> $HOME/process.log
echo "Got Mail!" >> $HOME/process.log

mkdir -p $HOME/tmp
TEMPDIR=`mktemp -d -p $HOME/tmp`
if [ ! -d "$TEMPDIR" ]
then
	echo "  $PROGRAM_NAME: Error creating temp directory" >> $HOME/process.log
	exit -1
fi

echo "TEMPDIR=$TEMPDIR" >> $HOME/process.log

echo "Environment:" > $TEMPDIR/env.txt
set >> $TEMPDIR/env.txt

MAILFILE=$TEMPDIR/email.txt
cat - > $MAILFILE

$BILLING_PATH/process-order.sh $TEMPDIR $MAILFILE >> $HOME/process.log

# Cleanup temporary files
if [ -d "$TEMPDIR" ]
then
	if [ `dirname $TEMPDIR` == "$HOME/tmp" ]
	then
		# If debugged and don't want to store the intermediate files
		# then remove the temp files.
		# For now it is probably better not to delete these though
		#rm -rf $TEMPDIR
		echo "Done!" >> $HOME/process.log
                echo "===============================================" >> $HOME/process.log
	fi
fi

