#!/usr/bin/perl 
##########################################
use Jcode;
use MIME::Base64;
umask (022);

##########################################
	# mailing list dir & name is require in the command line

$mlists_path = $ARGV[0];
$mlist_rc = $ARGV[1];
require 'timelocal.pl';

###### print "$mlists_path \n";
###### mkdir($mlists_path,0755) || die "cannot mkdir $mlists_path \n";

if ($mlists_path eq "" || $mlists_path eq "") {
	print "Usage : mlist2html dir_name ml-name \n";
	exit;
	}

##########################################
	# check the directory where to archive MLs
	# working directory is $mlists_path

        # if dir "$mlists_path" does not exist, make it

if (! -d $mlists_path ){
        mkdir($mlists_path,0755) || die "cannot mkdir $mlists_path \n";
        }

##########################################
	# default setting
@monat = (dummy,January,February,March,April,May,June,July,August,September,October,November,December);

foreach $i (0 .. $#monat) {
	$monthname{substr($monat[$i],0,3)} = $i;
	}
@week=(Sun,Mon,Tue,Wed,Thu,Fri,Sat);

$in_header = 1 ;
$headers = '';

##########################################
        # read mail from STDIN
        # all header is passed
	# Date is got from Date: header
	# write selected headers & body to /tmp/tmp_$$ ( $$=pid of this script)

#open(OUT,"> /tmp/tmpxxx");
$key = 0;$str = "";$enc = "";$enckey = 0;
while (<STDIN>) {
	if ($in_header) {
                s/utf-8/ISO-2022-JP/i;
                s/us-ascii/ISO-2022-JP/g;
                s/8bit/7bit/g;
                s/format=flowed//g;
		if (/^$/) {
			$in_header =0 ;
                        $str .= $headers;
		}
		else {
		      if (/^Date: /) {
                         if (/\+0900/){$key =1;}
                         $date = $_;
                          }
			# for 2 or more line subject
			if (/^\s+(.*)/) {
				chop($headers);
				s/^\s+// ;
			}
			$headers .= $_ ;
		}
	}

	if (! $in_header){
             $str .= $_;
	}
   }

##########################################
	# get year month day from Date: of mail
	#	format is 
	#		Date: Sun, 16 Mar 1997 02:18:50 +0900
	#	or
	#		Date: 16 Mar 1997 02:18:50 +0900


$date =~ s/Date:\s+//;
$_ = $date; s/\ \(\w+\)//;

#print OUT "DATE = $_\n";

if(!/^\d+/){
   ($dname,$day,$mname,$year,$time,$diff) = split(/\s+/);

#print OUT "Dummy=",$dummy,"Dname=",$dname,"Day=",$day,"Mname=",$mname,"Year=",$year,"Time=",$time,"Diff=",$diff,"\n";
}else{
   ($day,$mname,$year,$time,$diff) = split(/\s+/);
}

#print OUT "Dname=",$dname,"Day=",$day,"Mname=",$mname,"Year=",$year,"Time=",$time,"Diff=",$diff,"\n";
#close(OUT);

    ($hour,$min,$sec) = split(/:/, $time);
    $mon = $monthname{$mname};
    $mon = $mon -1 ;
    $time = &timelocal($sec,$min,$hour,$day,$mon,$year);
    $timediff = substr($diff,0,3);
    $time += (9 - $timediff) * 3600;
    ($sec,$min,$hour,$day,$mon,$year,$wday, $yday, $isdst) = localtime($time);
    $year = $year + 1900;
    $mon = $mon +1;
    $mname = substr(@monat[$mon],0,3);

if( $key == 0){

    $hour = '0' . $hour; $hour = substr($hour,-2);
    $min = '0' . $min; $min = substr($min,-2);
    $sec = '0' . $sec; $sec = substr($sec,-2);

$str .= "\n-----------------------------------------------------\n";
$str .= "Sent Date (by JST):@week[$wday], $day $mname $year $hour:$min:$sec\n";
}
	# convert month_name to number

$mon = $monthname{$mname};
$datum = $year . substr('0'.$mon, -2);
	# $datum     = 199703
$mon_path = "$mlists_path/$datum" ;
        # mon_path = directory of this month

#print OUT "MonPath= ",$mon_path,"\n";
#close(OUT);
#########################################################
	# until now, setting is over now making dirs and files

	# if dir "$mon_path" does not exist, then it is new month
	# so make "$mon_path" and 
	# remake $mlists_path/index.html

if (! -d $mon_path ) {

	mkdir($mon_path,0755) || die "cannot mkdir $mon_path \n";

        open (INDX,"> $mlists_path/index.html") ;
        select(INDX);

        # making new.html
	# using $mlists_path/.header.html as header
	#       $mlists_path/.footer.html as footer of new html
	#
	$header_file = "$mlists_path/.header.html";
	if (! -e $header_file) {

 	        print "<HTML><HEAD><TITLE> \n" ;
		print "Archive of $st_name \n";
		print "</TITLE></HEAD><BODY> \n";
		print "<H1>Archive of $mlist_rc</H1><HR><MENU>\n";
		}
	else {
		open (HEADER,"$header_file");
		while(<HEADER>) {
			print $_ ;
			}
		close (HEADER);
		}

        @dirs = <$mlists_path/*>;

        # @dirs is list of directories (which name is starting by digit) 

        @dirs = reverse(@dirs);
        print "\n" ;
	while(<@dirs>) {
		s#.*/## ;
               if (/^[1-9]+/) {		# ignore if dir does not start digit
			$dname = $_;			# $dname = 199703 
        	        $yy = substr($dname,0,4);               # $yy = 1997
              		$mm = substr($dname,4,2);               # $mm =   02
               		$mmm = $monat[$mm];                     # $mmm = Feburar

 			print "<LI><A HREF=\"$_/\"> $mmm $yy </A> \n";
			}
		}
	print "\n";

        $footer_file = "$mlists_path/.footer.html";
        if (! -e $footer_file) {
		print "</MENU></BODY></HTML> \n";
                }
        else {
                open (FOOTER,"$footer_file");
                while(<FOOTER>) {
                        print $_ ;
                        }
                close (FOOTER);
                }
        
	close(INDX);

        } 

#################################################################
#################################################################
	# write to new html of the day
	# now using MHonArc
	# set args for MHonArc to $mharg

$mhonarc = '/usr/local/bin/mhonarc';
$mhlib	 = '/usr/local/lib/MHonArc';

if (-e "$mhlib/$mlist_rc\.rc") {
	$mh_rc = "$mhlib/$mlist_rc\.rc";
	}
	else	{
		$mh_rc = "$mhlib/common.rc";
	}

$mharg = "-add -rcfile $mh_rc ";
$mharg = $mharg . " -title \'$mlist_rc of $mname, $akt_year\'" ;
$mharg = $mharg . " -ttitle \'$mlist_rc(by thread) of $mname, $akt_year\'";
$mharg = $mharg . " -outdir $mon_path";

$str .= "\r\n";

#open(TMP, "> /home/nakamura/temp.txt");
#print TMP $str;
#close(TMP);

open(TMP, "| $mhonarc $mharg");
print TMP $str;
close(TMP);

##############################################################
	# change mod to readable
system("chmod -R 0755 $mon_path");
##############################################################
	# remove tmp file

unlink("/tmp/tmp_$$");
###############################################################
# That's end.
###############################################################
