#!/usr/pkg/bin/perl 
#
# 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

if ( -d $mlist_path ) { chdir $mlist_path }

## open(TMP,">/tmp/log.$$");
## $pwd = `pwd`;
## print TMP "Current: $pwd\n";
## print TMP "mlists_path: $mlists_path\n";
## print TMP "mlist_path: $mlist_path\n";

@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 ;

##########################################
        # read mail from STDIN
        # header is separated to from,date,subject,messageid
	# write selected headers & body to /tmp/tmp_$$ ( $$=pid of this script)

open(TMP,"> /tmp/tmp_$$");

while (<STDIN>) {	
	if (/^$/) {
		$in_header = 0;
        	}		
	if ($in_header) {
#	if (/^From:/ && $in_header){ $from = $_ ;}
	if (/^Date:/ && $in_header){	$date = $_ ;}
#	if (/^Subject:/ && $in_header) {$subject = $_ ; }
#	if (/^Message-id:/i && $in_header){$messageid = $_ ;}
#	if (/^References:/ && $in_header){$references = $_ ;}
#	if (/^Content-Type:/ && $in_header){$content = $_ ;}
#	if (/^X-Sequence:/ && $in_header){$xsequence = $_ ;}
	}
		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);
if ( $akt_year < 1970 ) { $akt_year += 100 }
	# $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/pkg/bin/mhonarc';
$mhlib	 = '/usr/pkg/share/mhonarc';

## print STDERR "mhlib($mhlib)\n";
## print STDERR "mlist_name($mlist_name)\n";

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

## $mharg = "-add -rcfile $mh_rc ";
## $mharg = "-add -rcfile ../.mhonarc.mrc";
$mharg = "-add";
$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";

## print("cat /tmp/tmp_$$ | $mhonarc $mharg\n");
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.
###############################################################
