#!/usr/bin/perl

=head1 NAME

mbox2mh - mbox2mh convert UNIX mbox to mh spool.

=head1 SYNOPSIS

$ mbox2mh directory

STDIN: UNIX mbox format text

directory: mh directory

=head1 DESCRIPTION

Example:

$ cat hoge.mbox | mbox2mh ~/Mail/

=head1 AUTHOR

Yasumichi Akahoshi <yasumichi@users.sourceforge.jp>

=cut

# declaration to use module and library.
use Jcode;
require 'w2l-tools.pl';

# check arguments.
if(@ARGV != 1) {
	die "Invalid arguments.\n";
}
$objdir = $ARGV[0];

# Include trailing back slash.
if ($objdir =~ /[^\/]$/) {
	$objdir = $objdir."/";
}


# get first filename.
$dname = &get_first_name($objdir);

# read standard in
foreach $line (<STDIN>) {
	$line =~ s/\r\n/\n/g;	# convert code of new line
	$line =~ s/^(Content-Type: .*charset=).*/\1iso-2022-jp/;
	# split mail data.
	if($line =~ /^From .* (Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]* [0-9]*:[0-9]*:[0-9]* [0-9]*$/)
	{
		close(FILE) if <FILE>;
		open(FILE, ">$objdir$dname");
		$dname++;
		next;
	}
	$line = jcode($line)->h2z->jis;
	print FILE $line;
}

close(FILE);

