#!/usr/bin/perl -w
######################################
# Script for regenerating the W3C
# HTML archives for its mailing lists
# Author: Jose Kahan <kahan@w3.org> 
#
# $Id: update-period-wai,v 1.1 2005/03/08 09:57:49 daigo Exp $
#
######################################
# History:
# 6/Mar/97: The script is born
# 7/Mar/97: Added support for arguments
# 20/Dec/99: Changed the way we get the filenames
#            from an MH directory (the other one
#            was broken when the directory contained other filenames
#            that weren't digits exclusively.          
# 21/Jun/04: Debian ready
######################################
#
#USAGE: update-period options
#
# OPTIONS:
#
# -s source dir where the mails
#    are stored  
# -f file name of the first mail to process
# -l file name of the last  mail to process
#
# -t target dir for the html 
#
# -n name of the HTML archive
#   (e.g., www-html@w3.org AprJun96)
#
######################################

############################# 
# Custom Configuration      #
#############################

# hypermail binary exectuable
$hypermail = "/usr/local/bin/hypermail"; #path to the hypermail bin
# mailing list path
$w3path = "/home/lists"; 
$hm_conf_file = "/home/lists/.bin/.hmrc-wai";

## a directory where status info is stored
$archive_status = "ArchiveStatus"; 

## the following files are created automatically by this script and shouldn't be edited
## by hand

## name of the file with the markup for the navigation bar that will be included by hypermail
$hm_navbarup = "hm_navbar2up";
## name of the file with the extra markup for the HTML Head that will be included by hypermail
$hm_head = "hm_head"
;## name of the file with the extra markup for the HTML Head that will be included by hypermail
$hm_helpup = "hm_helpup";
;## name of the file with the extra markup for the HTML Head that will be included by hypermail
$hm_helpdown = "hm_helpdown";

# CSS base URL (we expect all the CSS files to be there)
$css_base = "http://www.w3.org/StyleSheets/Mail";

## the access right modes for the files we're creating
$ar_modes = 0666;

#############################
# Global variables          #
#############################

$list_name = "";  # www-html
$period = "";     # number of months per period
$first_msg = -1;  # first mail to process
$last_msg =  -1;  # last mail to process
$overwrite = 0;   # overwrite an existing hmail archive
$progress = 1;    # print progress report to stdout
$iso2022jp = 0;   # iso2022jp option for Japaneze mailing-lists.

$verbose_name = "";    # the desc of the list:  " www-html@w3.org JanFeb96"
$archive_name = "";    # the name of the archive (used in the mailtos)
$archive_type = undef;

# the css URLS
$messagelist_css = "";
$message_css = "";

##############################
# Main Program               #
##############################

{
## Configure the script

    &Configure;

#
# Read the Smartlist file names
#

    &ReadSmartListArchive;

#
# Regenerate the files
#

# First, concatenate all the mails together

    &concatenateMails;

# Next, use Hypermail to regenerate the archive

    &callHypermail;

# Finally, add either some error control, rm the temporary file, etc
    
    exit(0);

} ## Main

########################
### subroutines
########################

# Reads a smartlist archive and prepares the messages for processing

sub ReadSmartListArchive {

    opendir (MAILDIR, "$mail_dir") or die "Can't open $mail_dir: $!";
#@fileNames = grep (-T, map "$mail_dir/$_", readdir MAILDIR;
#@fileNames = grep (!/^\.\.?$/, readdir (MAILDIR));
    @fileNames = grep (/^\d*$/, readdir (MAILDIR));

    closedir MAILDIR;

    $totalfiles = $#fileNames;

#
# Sort the file names
#
    @fileNames = sort by_number @fileNames;
    
#
# Normalize the first and last file-names
#
    
    if ($first_msg == $last_msg) {
	$last_msg = 0;
    }

    for ($i = 0; $i<=$totalfiles; $i++) {
	if ($fileNames[$i] eq $first_msg) {
	    $first_msg = $i;
	    last;
	}
    }

    if ($last_msg) {
	for (; $i <=$totalfiles; $i++) {
	    if ($fileNames[$i] eq $last_msg) {
		$last_msg = $i + 1;
		last;
	    }
	}
    } else {
	$last_msg = $first_msg + 1;
    }

} ## ReadSmartListArchive



sub Configure {
    my $tmp;
    my $css_type;
    
    # get runtime options
    &getOpts();

    if ($list_name eq "" ||
	$first_msg == -1 ||
	$last_msg == -1 ||
	$period eq "" ||
	$verbose_name eq "" 
	) {
	die ("Usage: update-period -list <list name> -first <first_msg> -last <last_msg> -pdir <period-dirname> -desc <verbose description of the list> [-quiet] [-iso2022jp]\n");
    }

    $list_dir = "$w3path/$list_name";
    $mail_dir = "$list_dir/archive/latest";
    $period_dir = "$list_dir/Archive/$period";
    $catmail = "$list_dir/Archive/tmpcat";    #$opt_j/tmpcat"; 

    # What is the visibility of this mailing list archive?
    # We use the Archive path to infer it
    $tmp = readlink "$list_dir/Archive";
    if (defined $tmp) {
	if ($tmp =~ /\/Team\//i) {
	    $archive_type = "Team";
	} elsif ($tmp =~ /\/Member\//i) {
	    $archive_type = "Member";
	} else {
	    $archive_type = "Public";
	}
    }

    # set up the CSS URLs
    if (defined $archive_type) {
	$css_type = $archive_type;
	$css_type =~ tr/A-Z/a-z/;
    } else {
	$css_type = "public";
	$archive_type ="Public";
    }
    $messagelist_css = "$css_base/$css_type-messagelist";
    $message_css = "$css_base/$css_type-message";
    
} ## Configure


sub createNavBar {
    my $long_period = shift;
    my $period;

    
# contrary to update archive wai, we get here a verbose period and we have to extract
# the items we need from it. We suppose we have archive_name@w3.org from period

    $_ = $long_period;
    s/.*\ from\ //;
    $period = $_;

    open (OUT, ">$list_dir/$archive_status/$hm_navbarup") or
	die "Could not open $list_dir/$archive_status/$hm_navbarup: $!";

    print OUT <<END;
<p>
   <a href="http://www.w3.org/">W3C home</a> &gt;
   <a href="http://lists.w3.org/" title="Mailing lists archives">Mailing
    lists</a> &gt;
   <a href="http://lists.w3.org/Archives/$archive_type/" title="$archive_type mailing
lists">$archive_type</a> &gt;
   <a href="../" title="Index of $archive_name" rel="start">$archive_name</a> &gt;
   <a href="./" rel="contents" title="Messages received in $period">$period</a>
</p>
END
    close (OUT);
    # make the file be og+rw
    chmod ($ar_modes, "$list_dir/$archive_status/$hm_navbarup");
} #createNavBar

sub callHypermail {

# find the archive type
    my $prefix = "$list_dir/$archive_status";

    createNavBar ($verbose_name);

    #$options = "-r -m $catmail -l \"$verbose_name\" -d $period_dir -a $other_archives -c /home/lists/w3t-test/.hmrc-wai";
    $options = "-c $hm_conf_file -o readone=0 -o ietf_mbox=1 -M -p -m $catmail -l \"$verbose_name\" -d $period_dir -o icss_url=$messagelist_css -o mcss_url=$message_css -o hmail=$archive_name";

    # the WAI options
    $options = $options . " -o ihtmlheadfile=$prefix/$hm_head";
    $options = $options . " -o ihtmlnavbar2upfile=$prefix/$hm_navbarup";
    $options = $options . " -o ihtmlhelpupfile=$prefix/$hm_helpup";
    $options = $options . " -o ihtmlhelp2low=$prefix/$hm_helpdown";

    if ($overwrite) {
	$options = "-x " . $options;
    }

    if ($progress) {
	$options = "-p " . $options;
    }

    if ($iso2022jp) {
	$options = "-o iso2022jp=on " . $options;
    }

    $command ="$hypermail $options"; 
    system($command);

## comment the following  line if we're debugging
    unlink($catmail);
} ## callHypermail


sub concatenateMails {
# First, concatenate all mails into a single file 
    
    open (OUT, ">$catmail") or die "Can't open $catmail: $!";
    
    if ($progress) {
	print "Concatenating files ...  $w3path/......";
    }
    for ($i=$first_msg; $i < $last_msg; $i++) {
	if ($progress) {
	    printf "\b\b\b\b\b\b%-6d", $fileNames[$i];
	}
	
#    sleep(1);

	if (!-s "$mail_dir/$fileNames[$i]") {
	    if ($progress) {
		print "skipped non existant file: $mail_dir/$fileNames[$i]\n";
		next;
	    }
	}

	open (IN, "$mail_dir/$fileNames[$i]")  
	    or die "Can't open $mail_dir/$fileNames[$i]: $!";
 	# send the From line as is
	$_ = <IN>;
	print OUT $_;
	# write the rest of the messages, IETF-escaped	
	while (<IN>) {
	    print OUT ">", $_;
	}
	close (IN);
    }
	
    close (OUT);
	
    if ($progress) {
	print "\r", " " x 70;
	print "\r\uConcatenated ", $last_msg - $first_msg , " files\n";
    }
} ## concatenateMails
    


sub by_number {
        $a <=> $b;
} ## by_number
 

#('s:f:l:t:n:j:');
sub getOpts {
#    local($argumentative) = @_;
    local($_);

    while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
        $curr = shift(@ARGV);
	if ($curr eq "-list") {
	    $list_name = shift(@ARGV);
	} elsif ($curr eq "-pdir") {
	    $period = shift(@ARGV);	    
	} elsif ($curr eq "-first") {
	    $first_msg = shift(@ARGV);	    
	} elsif ($curr eq "-last") {
	    $last_msg = shift(@ARGV);	    
	} elsif ($curr eq "-overwrite") {
	    $overwrite = 1;
	} elsif ($curr eq "-quiet") {
	    $progress = 0;
	} elsif ($curr eq "-desc") {
	    $verbose_name = shift(@ARGV);
	    while (defined ($curr = shift(@ARGV))) {
		$verbose_name = $verbose_name . " " . $curr;
	    }
	} elsif ($curr eq "-archive_name") {
	    $archive_name = shift(@ARGV);
	} elsif ($curr eq "-iso2022jp") {
	    $iso2022jp = 1;
	}
	
    }
} ## getOps
