#! /usr/bin/perl

###############################################################
# Name: format_weekly                                         #
# Created: 27.2.98         By: Knut A. Mork                   #
# Do: Part of the weekly statistics process                   #
###############################################################

$systempath = "/home/almash/admin/statistics/";
$org_filename = "tmp_week_stat.html";
$filename = "week_stat.html";
$head = "head";
$tail = "tail";
$unsorted_domain = "unsorted_domain";
$sorted_domain = "sorted_domain";
$unsorted_archive = "unsorted_archive";
$sorted_archive = "sorted_archive";
$started_copying = 0;
$uns_domain = 0;
$uns_archive = 0;

open(TMP_FILE, ">$systempath$filename");
open(HEAD, "$systempath$head");
while(<HEAD>)
{
    print( TMP_FILE $_);
}
close(HEAD);

open(ORG_FILE, "$systempath$org_filename");
open(UNS_DOMAIN, ">$systempath$unsorted_domain");

while($uns_domain != 2)
{
	$line = <ORG_FILE>;
	if( $uns_domain == 1)
	{
		if( $line =~ "\<\/PRE\>")
		{
			$uns_domain = 2;
		}	
		else
		{
			print(UNS_DOMAIN $line);
		}
	}
	if( $line =~ "\<HR\>" && $uns_domain == 0)
	{
		print(TMP_FILE "<P><BR>\n");
	}
	elsif( $line =~ /\<H2\>(.*)\<\/H2\>/ && $uns_domain == 0)
	{
		$started_copying = 1;
		print(TMP_FILE "<H3>$1<\/H3>\n");
	}
	elsif( $started_copying == 1 && $uns_domain == 0)
	{
		print(TMP_FILE $line);
	}	
	if( $line =~ /\|------------------------------------$/ && $uns_domain == 0)
	{
		$uns_domain = 1;
	}
}

close(UNS_DOMAIN);

system("sort -r +3 $systempath$unsorted_domain > $systempath$sorted_domain");

open(SOR_DOMAIN, "$systempath$sorted_domain");
while(<SOR_DOMAIN>)
{
	$line = $_;
	print( TMP_FILE "$line"); 
}

close(SOR_DOMAIN);

print( TMP_FILE "</PRE>");
print( TMP_FILE "<P><BR>\n");
$line = <ORG_FILE>;
$line = <ORG_FILE>;
$line =~ /\<H2\>(.*)\<\/H2\>/;
print( TMP_FILE "<H3>$1</H3>\n");
$line = <ORG_FILE>;
print( TMP_FILE $line);
$line = <ORG_FILE>;
print( TMP_FILE $line);
$line = <ORG_FILE>;
print( TMP_FILE $line);

open(UNS_ARCHIVE, ">$systempath$unsorted_archive");
while( $uns_archive != 2)
{
    $line = <ORG_FILE>;
    if( $line =~ "\<\/PRE\>")
    {
	$uns_archive = 2;
    }
    else
    {
	if( ! ($line =~ /Code / || $line =~ /cgi-bin/ || $line =~ /\/sys\// || $line =~ /\/icons\//))
	{
	    print(UNS_ARCHIVE $line);
	}
    }
}
close(UNS_ARCHIVE);
system("sort -r +3 $systempath$unsorted_archive > $systempath$sorted_archive");

open(SOR_ARCHIVE, "$systempath$sorted_archive");
while(<SOR_ARCHIVE>)
{
    print( TMP_FILE $_);
}

close(SOR_ARCHIVE);

print( TMP_FILE "</PRE>\n");
print( TMP_FILE "<P><BR>\n");
$line = <ORG_FILE>;
$line = <ORG_FILE>;
print( TMP_FILE $line);
$line = <ORG_FILE>;
print( TMP_FILE $line);
$line = <ORG_FILE>;
print( TMP_FILE $line);
close(ORG_FILE);

open(TAIL, "$systempath$tail");
while(<TAIL>)
{
    print( TMP_FILE $_);
}
close(TMP_FILE);

system("rm /home/almash/admin/statistics/sorted_*");
system("rm /home/almash/admin/statistics/unsorted_*");




