#!%PERL% -w 

use strict;
use utf8;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::Group;
use Vhffs::Functions;
use Vhffs::Services::MailingList;
use Vhffs::Constants;
use Vhffs::Robots;
use File::Path;
use HTML::Template;

my $vhffs = init Vhffs::Main;
die "Cannot init vhffs" unless defined $vhffs;

my $listdir;
my $outputlistdir;
my @years;
my $year;
my $yearpath;
my $outputyeardir;
my @months;
my $month;
my $monthpath;
my $outputmonthdir;
my $cmd;

my $listengineconfig = $vhffs->get_config->get_listengine;
die "Cannot find listengine configuration" unless $listengineconfig;

my $archivedir = $listengineconfig->{'datadir'}."/archives";
my $outputdir = $listengineconfig->{'datadir'}."/public";

my $miscdir = "/usr/lib/vhffs/bots/misc/";

my $configmhonarc = $miscdir."/mhonarc.config";
die "mhonarc configuration file unavailable" unless( -f $configmhonarc );

File::Path::mkpath( $outputdir, {
	mode => 0755
});
die "Cannot create ".$outputdir." directory\n" unless( -d $outputdir );

Vhffs::Robots::lock( $vhffs , "listenginearchives" );

my $lists = Vhffs::Services::MailingList::getall( $vhffs , Vhffs::Constants::ACTIVATED , undef );
foreach my $list ( @{$lists} )
{
#		print $list->get_domain." ".$list->get_localpart." ".Vhffs::Group::get_name_by_gid( $vhffs , $list->get_owner_gid )." ".$list->get_oid." ".Vhffs::Functions::status_string_from_status_id( $list->get_status )."\n";

	next if ( !defined $list->get_domain  ||  !defined $list->get_localpart  ||  $list->get_domain eq ""  ||  $list->get_localpart eq "" );

	# delete previous public archives (if available) if there is no public archives for this list
	if ( $list->get_open_archive == 0 )  {
		my $publicdir = $outputdir."/".$list->get_domain."/".$list->get_localpart;
		if ( -d $publicdir)  {
			File::Path::rmtree($publicdir);
		}
		next;
	}

	$listdir = $archivedir."/".$list->get_domain."/".$list->get_localpart;
	next if( !defined opendir( LISTDIR , $listdir ) );

	$outputlistdir = $outputdir."/".$list->get_domain;
	mkdir($outputlistdir, 0755);
	if (! -d $outputlistdir)  { die "Cannot create ".$outputlistdir." directory\n"; };

	$outputlistdir = $outputdir."/".$list->get_domain."/".$list->get_localpart;
	mkdir($outputlistdir, 0755);
	if (! -d $outputlistdir)  { die "Cannot create ".$outputlistdir." directory\n"; };

	# -- index : main page
	my $maintemplate = new HTML::Template( filename => $miscdir."/mhonarc.indexmain.tmpl" );
	$maintemplate->param( NAME => $list->get_domain."/".$list->get_localpart );
	my $indexbody = "";
	my $oddoreven = "odd";

	@years = readdir( LISTDIR );
	foreach $year ( reverse sort @years )
	{
		next if( $year =~ /^\..*$/ );

		$yearpath = $listdir."/".$year;

		next if( !defined opendir( YEARDIR , $yearpath ) );

		$outputyeardir = $outputlistdir."/".$year;
		mkdir($outputyeardir, 0755);
		if (! -d $outputyeardir)  { die "Cannot create ".$outputyeardir." directory\n"; };

		@months = readdir( YEARDIR );
		foreach $month ( reverse sort @months )
		{
			next if( $month =~ /^\..*$/ );

			$monthpath = $yearpath."/".$month;

			$outputmonthdir = $outputyeardir."/".$month;
			mkdir($outputmonthdir, 0755);
			if (! -d $outputmonthdir)  { die "Cannot create ".$outputmonthdir." directory\n"; };

			$cmd = "mhonarc -add -quiet -rc " . $configmhonarc . " -definevar MAIN-TITLE='" . $list->get_domain . "/" . $list->get_localpart . "' -outdir " . $outputmonthdir . " " . $monthpath . "/*/*";
			system( $cmd );

			# -- index : part
			my $parttemplate = new HTML::Template( filename => $miscdir."/mhonarc.indexpart.tmpl" );
			$parttemplate->param( YEAR => $year );
			$parttemplate->param( MONTH => $month );

			if( defined opendir( MONTHDIR , $monthpath ) )  {

				my $mailnb = 0;
				my $mailsize = 0;

				my @days = readdir ( MONTHDIR );
				foreach my $day ( @days )
				{
					next if( $day =~ /^\..*$/ );

					my $daypath = $monthpath."/".$day;

					next if( !defined opendir( DAYDIR , $daypath ) );

					my @mails = readdir ( DAYDIR );
					foreach my $mail ( @mails )
					{
						next if( $mail =~ /^\..*$/ );

						$mailnb++;

						my $size;
						(undef,undef,undef,undef,undef,undef,undef,$size,undef,undef,undef,undef,undef) = stat( $daypath."/".$mail );
						$mailsize += $size;
					}
				}

				my $totalsize = sprintf("%d", $mailsize/1024 );
				$parttemplate->param( NUMBER => $mailnb );
				$parttemplate->param( SIZE => $totalsize );
				$parttemplate->param( SIZEUNIT => "KB" );
				$parttemplate->param( ODDOREVEN => $oddoreven );
				if ( $oddoreven eq "odd" ) { $oddoreven = "even"; }
				else { $oddoreven = "odd"; }
			}

			$indexbody .= $parttemplate->output;
		}

		closedir ( YEARDIR );
	}

	$maintemplate->param( BODY => $indexbody );
	open( OUTPUT , ">".$outputlistdir."/index.html" );
	print OUTPUT $maintemplate->output;
	close( OUTPUT );

	closedir( LISTDIR );
}

Vhffs::Robots::unlock( $vhffs , "listenginearchives" );
