#!/usr/bin/perl

#
$version = "fixcounter 0.0.1";

# variables
$debug=0;
# absolute path to the counter file on the website.
$logpath = "/Library/WebServer/Documents/count.shtml";
$logpath1 = "/Library/WebServer/Documents/count1.shtml";

$line = "";
# maximum number of dots printed to the screen in one line
$maxdots = 64;
# global storage for where the dot is
$dotposition = 0;
# indent each row of dots
$indentation = "     ";
# what will the dot look like, will it be an at sign, peroid, comma, dash ...
$step = ".";
$marker = "*";
$dot = ".";

@counterdata = ();
$pads = join ("", "%0", $pad, "d");

# refresh how often?
$refreshhours = 24;
$refreshminutes = 0;
$refreshseconds = 0;
$refreshtime = ((($refreshhours*60)+$refreshminutes)*60)+$refreshseconds;

# open up the counter file
open ($countfile, "$logpath");
@counterdata = <$countfile>;
close($countfile);

@sortarray = ();
blippr("getting the data to sort", $indentation);
$dot = $step;
foreach $linevalue(@counterdata) {
  blip;
  $checkline1 = join("", $linevalue);
  if ($checkline1 =~ m/^<tr.*/) {
    $sortarray[++$#sortarray] = $checkline1;
    };
  };
  

#<tr><td><a href="/images/spinningapple.gif">/images/spinningapple.gif</a>:</td><td>18516</td></tr>

blippr("sorting the file according to the most accessed", $indentation);
for ($i=0; $i<$#sortarray; $i++) {
  $dot = $marker;
  &blip;
  $dot = $step;
  $indentation = join("",$i,"/",$#sortarray,"  ");
  $checkline1 = $sortarray[$i];
  $url1 = join( "", $checkline1);
  $url1 =~ s/^<tr><td><a href=\"//;
  $count1 = join( "", $url1);
  $url1 =~ s/\">.*$//;
  $url1 =~ s/<.*>//;
  $count1 =~ s/^.*<td>//;
  $count1 =~ s/<.*$//;
  $count1 =~ s/\r//;
  $count1 =~ s/\n//;
  $url1 =~ s/\r//;
  $url1 =~ s/\n//;
  $url1 =~ s/lower/upper/;
  for ($j=$i; $j<=$#sortarray; $j++) {
    &blip;
    $checkline2 = $sortarray[$j];
    $url2 = join( "", $checkline2);
    $url2 =~ s/^<tr><td><a href=\"//;
    $count2 = join( "", $url2);
    $url2 =~ s/\">.*$//;
    $url2 =~ s/<.*//;
    $count2 =~ s/^.*<td>//;
    $count2 =~ s/<.*$//;
    $count2 =~ s/\r//;
    $count2 =~ s/\n//;
    $url2 =~ s/\r//;
    $url2 =~ s/\n//;
    $url2 =~ s/lower/upper/;
    if ($count2 > $count1) {
      $sortarray[$j] = $checkline1;
      $sortarray[$i] = $checkline2;
      $url1 = $url2;
      $count1 = $count2;
      $checkline1 = $checkline2;
      }
    elsif ($count2 == $count1) {
#      print "$count2/$count1,$url2 / $url1\n";
#      print "$i/$#sortarray  count2=count1 $url2 $url1\n";
      if (lc($url2) lt lc($url1)) {
#        print "url2<url1\n";
        $sortarray[$j] = $checkline1;
        $sortarray[$i] = $checkline2;
        $url1 = $url2;
        $count1 = $count2;
        $checkline1 = $checkline2;
        }
#      print "$count2/$count1,$url2 / $url1\n\n";
      };
    }
  };

for ($i=0; $i<$#sortarray; $i++) {
  print $sortarray[$i];
  };

$indentation = "     ";
blippr("storing the sorted data", $indentation);
$i=0;
foreach $linevalue(@counterdata) {
  &blip;
  if ($linevalue =~ m/^<tr.*/) {
    $linevalue = $sortarray[$i];
    $i++;
    };
  };

$counterdata[4] = "  <title> Sorted Counter </title>\r\n";
&savecounters;

############ all done ############
print "\n\n";
exit 0 ;




# save the counter html file
sub savecounters {
  open ($countfile, ">$logpath1");
  foreach $myarray_line(@counterdata) {
    &blip;
    print $countfile "$myarray_line";
    };
  close($countfile);
  };

  

# this is an old message routine from the old adventure game
sub blip {
  print $dot;
  $dotposition++;
  # for some reason perl doesn't handle global variables correctly in an if statement
  # so we localize them for that purpose.
  my $position = $dotposition;
  my $max = $maxdots;
  if ($position >= $max) {
    print "\n",$indentation;
    $dotposition = 0;
    };
  };
  
sub blippr {
  ($msg, $msg2) = @_;
  print "\n\n<",$msg,">\n",$msg2;
  $dotposition = 0;
  };

