#!/bin/sh

#
# JKF 27/06/2002 (C) Julian Field
#
# This script will create an "mbox" format file called "spam.<dirname>"
# in the current directory for each of the directories which are passed
# on the command-line to this script. So you can do
#    cd /var/spool/MailScanner/quarantine
#    df2mbox *
# and it will create a "spam.<dirname>" file in the current directory,
# where each <dirname> is one of the directory names passed on the
# command line. This file will contain all the messages that were
# quarantined in their entirety.
# So if you have
#    Quarantine Whole Message = no
# the only thing in these directories will be spam, not viruses (but
# it will include spam that contained viruses, so be careful!).
#
# Version 1.1 Include date format fix
#

parentdir=`pwd`
export parentdir

while [ -n "$1" ]
do
  dir="$1"
  shift

  echo -n "Processing directory $dir..."
  if [ \! -d $dir ]; then
    echo $dir is not a directory
    exit 1
  fi

  cd $dir

  for qf in *-H
  do
    id=`echo $qf | sed 's/-H$//'`
    
    perl -e 'while(<>){++$i==1 and s/-H$//,$id=$_,next;$i==3 and chomp,print("From $_ ".`date "+%a %b %d %T %Y"`),next;/^XX$/ and $j=1,next;$j?$j--:0 and /(\d+)/,$k=$1,next;$k?$k--:0 and print("X-MailScanner-Recipient: $_"),next;/^$/ and next;/^(\d{3,})([^\d])\s(.*)/s and ($2 ne "*" or $l=0) and $l = $1 - length($3)+1,print($3),next;$l and $l-=length,print}' < $qf

    echo
    sed -e '1d' < $id-D
    echo
  done > mbox.$dir

  cd $parentdir
  mv $dir/mbox.$dir spam.$dir
  echo
done
