#! /usr/bin/perl

##############################################################
# Legger sammen de ekte katalogene og de symbolske linkene i #
# statistikken over katalog-aksessering.                     #
# Versjon for eksisterende genererte lister.                 #
# Laget av Knut A. Mork, sist oppdatert: 26.05.98            #
##############################################################


$clear = 1;
$org_file_tmp = $ARGV[0];
$org_file = "/www/almashriq/htdocs/base/statistics/" . "$org_file_tmp"; 
$parse = "/home/almash/admin/statistics/parse_html.pl";

# Part I

open (FILE, "$org_file");
open (TMP, ">tmp_file.html");
while (<FILE>)
  {
    $line = $_;
    
    # Syntaksen paa wwwstat sin ouput er litt kronglete...
    
    if ( $line =~ /^\s*([^ ]+)? +([^ ]+)? +([^ ]+)? +([^ ]+)? +(\| \/.*)/)
      {
	$line = "$1  $2  $3  $4  $5\n";
	while( $clear)
	  {
	    # Fjerne navnet i symbolske kataloger
	    if ($line =~ /(.*\/[0-9]*)\.[a-zA-Z\_]*(.*)/)
	      {
		$line = "$1$2\n";
	      }
	    else
	      {
		$clear = 0;
	      }
	  }
	print TMP $line;
      }
    $clear = 1;
  }
close (TMP);
close (FILE);
system ("sort +7 -t\" \" tmp_file.html > tmp2_file.html");
system ("mv tmp2_file.html tmp_file.html");


# Part II

open (FILE, "tmp_file.html");
open (TMP, ">tmp2_file.html");
$last_line = "| /jallajalla";
while (<FILE>)
{
    $line = $_;
    $last_line =~ /\| \/(.*)/;
    $last_line_part = $1;
    $line =~ /\| \/(.*)/;

    if ($last_line_part ne $1)
      {
	if ($last_line ne "| /jallajalla")   # Ikke den foerste gangen
	  {
	    print TMP $last_line;
	  }
      }
    else
      {
	$line =~ /^([^ ]+)?  ([^ ]+)?  ([^ ]+)?  ([^ ]+)?  (\| .*)/;
	$p_req_new = $1;
	$p_bytes_new = $2;
	$bytes_new = $3;
	$req_new = $4;
	$last_line =~ /^([^ ]+)?  ([^ ]+)?  ([^ ]+)?  ([^ ]+)?  (\| .*)/;
	$p_req = $1 + $p_req_new;
	$p_bytes = $p_bytes_new + $2;
	$bytes = $bytes_new + $3;
	$req = $req_new + $4;
	if (length $p_req == 3)
	  {
	    $p_req = $p_req . "0";
	  }
	elsif (length $p_req == 2)
	  {
	    $p_req = $p_req . "0.0";
	  }
	elsif (length $p_req == 1)
	  {
	    $p_req = $p_req . ".00";
	  }
	if (length $p_bytes == 3)
	  {
	    $p_bytes = $p_bytes . "0";
	  }
	elsif (length $p_bytes == 2)
	  {
	    $p_bytes = $p_bytes . "0.0";
	  }
	elsif (length $p_bytes == 1)
	  {
	    $p_bytes = $p_bytes . ".00";
	  }
	$line = "$p_req  $p_bytes  $bytes  $req  $5\n";
    }
    $last_line = $line;
}
close (TMP);
close (FILE);

system ("sort +5 -r -n -t\" \" tmp2_file.html > tmp_file.html");

$en_gangs_variabel = 1;

open (FILE, "$org_file");
open (TMP, "tmp_file.html");
open (TMP2, ">tmp2_file.html");
while (<FILE>)
  {
    $line = $_;
    if ( !($line =~ /\s*([^ ]+)? +([^ ]+)? +([^ ]+)? +([^ ]+)? +(\| \/.*)/))
      {
	print TMP2 $line;
      }
    elsif ($en_gangs_variabel == 1)
      {
	$en_gangs_variabel = 0;

	while (<TMP>)
	  {
	    print TMP2 $_;
	  }
      }
  }
close (TMP);
close (TMP2);
close (FILE);


system ("mv tmp2_file.html $org_file");
system ("rm tmp_file.html");

# Kaller opp et perlscript som formaterer pre-teksten til html-tabeller
system ("$parse $org_file");
