#!/usr/bin/perl
#
# Hypermail Flat Threads 0.2
# This program is written by Georgi Chorbadzhiyski
# You can contact the author at this email (georgi AT top DOT bg)
#
# This program is distributed under GNU GPL version 2.0
#
# Sample output of this program can be found at this urls
#    http://georgi.top.bg/hm-threads/
#    http://georgi.top.bg/hm-threads/0002-thread.html
#
# Changelog
#
#  v0.2 - Added thread id to messages
#  v0.1 - Initial version
#
use strict;
use integer;

my $VERSION = "0.2";

if ($ARGV[0] eq '') {
    print <<EOT
Hypermail Flat Threads $VERSION

  Usage: $0 MAILING_LIST_ARCHIVE_DIRECTORY

EOT
;
    exit;
}

# If this is set to 'yes' then instead of creating index-threads.html
# program will overwrite the original index.html
my $INDEX_HTML_DEFAULT = 'yes';
my $DIRECTORY  = $ARGV[0]; $DIRECTORY =~ s/\/+$//;
my $CUTOFF_BEG = 2;  # Lines to remove from message start
my $CUTOFF_END = 11; # Lines to remove from message end
my $TEXT_SUBJ  = "Subject:";
my $TEXT_FROM  = "From:";
my $TEXT_DATE  = "Date:";
my $FILE_ADD   = "-thread";

my $line;
my $offset;
my $line_f;
my $offset_f;
my $ul_beg;
my $ul_end;
my $i;
my $HTML;
my $msgid;
my $title;
my $size;
my $parent_msgid;

my @parsed;
my @msgtxt;
my @index;
my %parent_msgs;

#
# Prepare threads
#
open(FILE, "<$DIRECTORY/index.html") or die "Can`t open file $DIRECTORY/index.html, $!\n";
@index = <FILE>;
close(FILE);
chomp(@index);

foreach $line (@index) {
    if ( $line eq '<UL>') { $offset++; next; }
    if ( $line eq '</UL>') { $offset--; next; }
    push @parsed, $offset.':-:'.$line;
}

#
# Generate thread files
#
for ($i=0;$i<$#parsed;$i++) {
    ($offset,$line) = split(":-:",$parsed[$i]);
    ($offset_f,$line_f) = split(":-:",$parsed[$i+1]);
    if ($offset_f > 1) {
	$HTML = '';
	($msgid) = ($line =~ /\"([0-9]{4})\.html\"/)[0];
	($title) = ($line =~ /\<STRONG\>(.*?)\<\/STRONG\>/)[0];
	$parent_msgid=$msgid;
	$parent_msgs{$parent_msgid}=1;
	$HTML = "
<HTML>
<HEAD>
<TITLE>$title</TITLE>
</HEAD>
<BODY BGCOLOR=white TEXT=black>
<TABLE WIDTH=100% CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR><TD>
<H1>$title</H1>
";
	if ( -e "$DIRECTORY/$msgid.html" ) {
	    open(FILE, "<$DIRECTORY/$msgid.html") || die "Can`t open file $DIRECTORY/$msgid.html, $!\n";
	    @msgtxt = <FILE>;
	    close(FILE);
	    put_msg();
	};
	while ($offset_f > 1) {
	    $i++;
	    ($offset,$line) = split(":-:",$parsed[$i]);
	    ($offset_f,$line_f) = split(":-:",$parsed[$i+1]);
	    ($msgid) = ($line =~ /\"([0-9]{4})\.html\"/)[0];
	    if ( -e "$DIRECTORY/$msgid.html" ) {
		open(FILE, "<$DIRECTORY/$msgid.html") || die "Can`t open file $DIRECTORY/$msgid.html, $!\n";
		@msgtxt = <FILE>;
		close(FILE);
		put_msg();
	    };
	};
	$HTML .= "
</TD></TR>
</TABLE>
</BODY>
</HTML>
";
	open(FILE, ">$DIRECTORY/$parent_msgid$FILE_ADD-tmp.html") || die "Can`t create file $DIRECTORY/$parent_msgid$FILE_ADD-tmp.html, $!\n";
	print FILE $HTML;
	close(FILE);
	# If Exist
	if ( -e "$DIRECTORY/$parent_msgid$FILE_ADD.html" ) {
	    my $size1 = -s "$DIRECTORY/$parent_msgid$FILE_ADD.html";
	    my $size2 = -s "$DIRECTORY/$parent_msgid$FILE_ADD-tmp.html";
	    # If new file size is different then we assume that there new msgs in the thread
	    if ( $size1 != $size2 ) {
		unlink("$DIRECTORY/$parent_msgid$FILE_ADD.html") || warn "Can`t delete $DIRECTORY/$parent_msgid$FILE_ADD.html, $!\n";
		rename("$DIRECTORY/$parent_msgid$FILE_ADD-tmp.html", "$DIRECTORY/$parent_msgid$FILE_ADD.html") || warn "Can`t rename $DIRECTORY/$parent_msgid$FILE_ADD-tmp.html, $!\n";
	    } else {
		unlink("$DIRECTORY/$parent_msgid$FILE_ADD-tmp.html") || warn "Can`t delete $DIRECTORY/$parent_msgid$FILE_ADD-tmp.html, $!\n";
	    };
	# Just rename it...
	} else {
	    rename("$DIRECTORY/$parent_msgid$FILE_ADD-tmp.html", "$DIRECTORY/$parent_msgid$FILE_ADD.html") || warn "Can`t rename $DIRECTORY/$parent_msgid$FILE_ADD-tmp.html, $!\n";
	}
	$parent_msgs{$parent_msgid.'_size'}= -s "$DIRECTORY/$parent_msgid$FILE_ADD.html";
    }
}

#
# Generate index-threads
#
open(FILE, ">$DIRECTORY/index$FILE_ADD-tmp.html") || die "Can`t create $DIRECTORY/index$FILE_ADD-tmp.html, $!\n";
foreach $line (@index) {
    if ( $line =~ /^\<li\>\<A HREF=/ ) {
		($msgid) = ($line =~ /\"([0-9]{4})\.html\"/)[0];
		if ($parent_msgs{$msgid} == 1) {
		    $size = $parent_msgs{$msgid.'_size'};
		    $line =~ s/^<li>/<li><SMALL><[Aa] HRE[Ff]="$msgid$FILE_ADD.html?$size">thread<\/A><\/SMALL> /;
		}
    };
    print FILE $line."\n";
}
close(FILE);
if ($INDEX_HTML_DEFAULT eq 'yes') {
    unlink("$DIRECTORY/index.html") if ( -e "$DIRECTORY/index.html" );
    rename("$DIRECTORY/index$FILE_ADD-tmp.html", "$DIRECTORY/index.html");
} else {
    unlink("$DIRECTORY/index$FILE_ADD.html") if ( -e "$DIRECTORY/index$FILE_ADD.html" );
    rename("$DIRECTORY/index$FILE_ADD-tmp.html", "$DIRECTORY/index$FILE_ADD.html");
}

#
# Format message
#
sub put_msg() {
    my $color = "#FFFFFF";
    my $blank1 = "";
    my $blank2 = "";
    if ($offset == 2) { $color = "#f0f0f0"; $blank1 = ' WIDTH="7%"' ; $blank2 = ' WIDTH="93%"'; };
    if ($offset == 3) { $color = "#e7e7e7"; $blank1 = ' WIDTH="15%"'; $blank2 = ' WIDTH="85%"'; };
    if ($offset == 4) { $color = "#e0e0e0"; $blank1 = ' WIDTH="20%"'; $blank2 = ' WIDTH="80%"'; };
    if ($offset == 5) { $color = "#d7d7d7"; $blank1 = ' WIDTH="25%"'; $blank2 = ' WIDTH="75%"'; };
    if ($offset == 6) { $color = "#d0d0d0"; $blank1 = ' WIDTH="30%"'; $blank2 = ' WIDTH="70%"'; };
    if ($offset == 7) { $color = "#c7c7c7"; $blank1 = ' WIDTH="35%"'; $blank2 = ' WIDTH="65%"'; };
    if ($offset == 8) { $color = "#c0c0c0"; $blank1 = ' WIDTH="40%"'; $blank2 = ' WIDTH="60%"'; };
    if ($offset == 9) { $color = "#b7b7b7"; $blank1 = ' WIDTH="45%"'; $blank2 = ' WIDTH="55%"'; };

    my $msgbeg = 0;
    my $msgend = 0;
    my $i;
    my $line;

    my $m_author;
    my $m_subject;
    my $m_date;

    foreach $line (@msgtxt) {
	if ($line =~ /<META NAME=\"Author\"/) { ($m_author) = ($line =~ /CONTENT=\"(.*?)\"/)[0]; };
	if ($line =~ /<META NAME=\"Subject\"/) { ($m_subject) = ($line =~ /CONTENT=\"(.*?)\"/)[0]; };
	if ($line =~ /\<!-- received=\".*?\" --\>/) { ($m_date) = ($line =~ /\"(.*?)\"/)[0]; };
	if ($line =~ /\<!-- body=\"start\" --\>/) { $msgbeg = $i; };
	if ($line =~ /\<!-- body=\"end\" --\>/) { $msgend = $i; };
	$i++;
    }    
    @msgtxt = @msgtxt[$msgbeg+$CUTOFF_BEG..$msgend-$CUTOFF_END];
	my $msgid_clean = $msgid;
	$msgid_clean =~ s/^0+//;
    $HTML .= "
<HR NOSHADE WIDTH=100%>
<TABLE BGCOLOR=$color WIDTH=100% CELLPADDING=1 CELLSPACING=0 BORDER=0>
<TR>
    <TD$blank1>&nbsp;</TD>
    <TD$blank2>
$TEXT_FROM <STRONG>$m_author</STRONG><BR>
$TEXT_SUBJ <STRONG>$m_subject</STRONG> <A HREF=$msgid.html>(Msg)</A> <A HREF=index.html#$msgid_clean>(Thread)</A><BR>
$TEXT_DATE <STRONG>$m_date</STRONG><BR>
<BR>
@msgtxt
    </TD>
</TR>
</TABLE>
"
};
