#!/usr/bin/perl 
#
# 1999.11.09	version 0.07
#	Thanks Mr. Yukihiro KAGETA <kageta@pfu.co.jp>
#	for patch to Y2K problem and Date format.
#	modify for Subject in 2 or more lines 
#
# 1998.07.18	include X-Sequence: header
# 1998.04.23	version 0.06
#	Thanks Mr. Atsushi Murai <amurai@spec.co.jp>.
#	Add  Contents-Type header for MHonArc
# 1997.04.09	version 0.05
#	make index.html using header & footer file
# 1997.04.08	version 0.04
#	usage: mlist2hmlt where ml_name 
# 1997.04.04	version 0.03
#	use mhonarc for mail conversion
# 1997.03.29	version 0.01
#	translate to perl script and modified by nob@makioka.y-min.or.jp
#       easy lock system is installed
#       time to separate html document if from Date: header of the mail,
#               not from localtime
# 	by MURATA Nobuhiro <nob@makioka.y-min.or.jp>
#       	http://www.y-min.or.jp/~nob/ML/mlist2htmlpl.html
#
# this script is  converted from "mlist2html" , which is written by
#        Markus Stumpf
#               <stumpf@informatik.tu-muenchen.de>
#               http://www.informatik.tu-muenchen.de/~stumpf/
#
# I suppose that a lot of bugs are in here.
# please let me know, if you find a bug by mail.
#
##########################################

umask (022);

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

$mlists_path = $ARGV[0];
$mlist_name  = $ARGV[1];

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

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

$mlist_path  = "$mlists_path/$mlist_name" ;

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

if (! -d $mlist_path ){
        mkdir($mlist_path,0755) || die "cannot mkdir $mlist_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;
	}

$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(TMP,"> /tmp/tmp_$$");

while (<STDIN>) {	

	if ($in_header) {
		if (/^$/) {
			$in_header =0 ;
			print TMP $headers;
		}
		else {
			if (/^Date: /) {$date = $_; }

			# for 2 or more line subject
			if (/^\s+(.*)/) {
				chop($headers);
				s/^\s+// ;
			}
			$headers .= $_ ;
		}
	}

	if (! $in_header){
		print TMP "$_" ;
		}

	}

close(TMP);

##########################################
	# 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;
($dname,$day,$mname,$year,$time) = /^(.*)(\d+) (\w+) (\d+) (.*)$/; 

	# convert month_name to number

$mon = $monthname{$mname};

#$akt_year = substr('19'.$year,-4);
$akt_year = ($year > 80) ? substr('19'.$year,-4) : substr('20'.$year,-4);
	# $akt_year  = 1997  
$akt_month = substr('0'.$mon, -2);
	# $akt_month = 03 
$datum = $akt_year . $akt_month . substr('0'.$day,-2);
	# $datum     = 19970309
$i_datum = $akt_year . $akt_month;
	# $i_datum   = 199703

	# mon_path = directory of this month

$mon_path = "$mlist_path/$i_datum" ;

#########################################################
	# 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 $mlist_path/index.html

if (! -d $mon_path ) {

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

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

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

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

        @dirs = <$mlist_path/*>;

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

        @dirs = reverse(@dirs);
        print "<UL> \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 "</UL> \n";

        $footer_file = "$mlist_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/share/MHonArc';

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

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

system("cat /tmp/tmp_$$ | $mhonarc $mharg");


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

##############################################################
	# remove tmp file

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