#!/usr/bin/perl -w
#
# Usage:	cat mailbox.txt | formail -s cpmsg.pl 
#
#		where 'cpmsg.pl' is the name of this script
#
# Purpose:	Called by formail once for each mail message in a Berkeley-
#		format mailbox
#

$maildir  = "$ARGV[0]";
if (!$maildir) { die "Usage: $0 $maildir"; }

# Formail increments this number for each message. The 
# leading "0"'s must be removed (e.g. 001 becomes 1)

$filenum  = ($ENV{FILENO} - 0) + 1;
$file = "$maildir/$filenum.";

open (OUTFILE,"> $file") or die("Could not open '$file' for write - $!");

while (<STDIN>) {
	chomp;

	print OUTFILE "$_\015\012";  # Add CRLF to each line!
}

close OUTFILE;

