@tars=split /\n/,`ls -1 wiki*.bz2`;


$i=-1;
open F,'stat.dat'; 
while (<F>) {
  $date=$_;
  chomp $date;
  $date =~ s/^([^;]*);.*/$1/;
  $done{$date}=1;
  print "$date\n";
  $i++;
}
close F;
$last="wiki$date.tar.bz2";

open F,'change.dat';
while (<F>) {
  $line=$_;
  chomp $line;
  @line = split /;/,$line;
  $changed{$line[0]}=$line[1];
}
close F;


open F,'>>stat.dat'; 
unless (defined %done) {
  print F "date;num;size;bew;change\n2003-10-14;0;0;0;0\n";
}
foreach $tar (@tars) {
  $size=0;
  $date=$tar;
  $date =~ s/^wiki(.*).tar.bz2/$1/;
  next if $done{$date};
  print "Neu: $date\n";
  @files=split /\n/,`tar jtvf $tar`;
  $num=scalar @files;
  foreach $line (@files) {
    $fsize=(split /\s+/,$line)[2];
    $size += $fsize;
  }
  $bew=`tar jxOf $tar LarpWikiBewohner.txt |grep \\* |wc -l`;
  chomp $bew;
  $change=0;
  if (defined $last) {
    `cd test1;tar jxvf ../$tar`;
    `cd test2;tar jxvf ../$last`;
     @change= split /\n/,`cd test1;for a in *;do diff -qN "\$a" "../test2/\$a";done`;
     $change=scalar(@change);
    foreach $line (@change) {
      chomp($line);
      $line =~ s/Dateien (.*) und .*/$1/;
      if (defined $changed{$line}) {
	$changed{$line}++;
      } else {
	$changed{$line}=1;
      }
    }
    `rm -f test1/*`;
    `rm -f test2/*`;
  }
  $last=$tar;
  print "$date;$num;$size;$bew;$change\n";
  print F "$date;$num;$size;$bew;$change\n";
  $i++;
}
close F;
 
open F,'>stat.r'; 
  print F "wiki <- read.table(\"stat.dat\",header=TRUE,sep=\";\",as.is=TRUE,na.strings=\".\")\n";
  print F "attach(wiki)\n";
  print F "z <- strptime(date, \"%Y-%m-%d\")\n";

  print F "png (\"wikinum.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,num, type=\"h\", xaxt=\"n\", ylab=\"Seiten\")\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(num),100),col=\"red\")\n";

  print F "png (\"wikisize.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,size, type=\"h\", xaxt=\"n\", ylab=\"Byte\" )\n"; 
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(size),500000),col=\"red\")\n";

  print F "png (\"wikibew.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,bew, type=\"h\", xaxt=\"n\", ylab=\"Bewohner\" )\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(bew),10),col=\"red\")\n";

  print F "ppb <- num/bew\n";
  print F "ppb[1]=0\n";
  print F "png (\"wikippb.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,ppb, type=\"h\", xaxt=\"n\", ylab=\"Seiten/Bewohner\" )\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(ppb),5),col=\"red\")\n";

  print F "spb <- size/bew\n";
  print F "spb[1]=0\n";
  print F "png (\"wikispb.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,spb, type=\"h\", xaxt=\"n\", ylab=\"Byte/Bewohner\" )\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(spb),10000),col=\"red\")\n";

  print F "spp <- size/num\n";
  print F "spp[1]=0\n";
  print F "png (\"wikispp.png\", width=640, height=480, pointsize=10) \n";
  print F "plot(z,spp, type=\"h\", xaxt=\"n\", ylab=\"Byte/Seite\" )\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,max(spp),500),col=\"red\")\n";
  print F "png (\"wikichg.png\", width=640, height=480, pointsize=10) \n";
  print F "ch <- change-change+50\n";
  print F "ch <- pmin(ch,change)\n";
  print F "plot(z,ch, type=\"h\", xaxt=\"n\", ylab=\"Mod. Seiten\" )\n";
  print F "axis.POSIXct(1, at=seq(z[1], z[$i], by=\"month\"), format=\"%d.%m.%Y\")\n";
  print F "abline(h=seq(0,50,5),col=\"red\")\n";
close F;

open F,">change.dat";
foreach (sort by_count keys %changed) {
  print F "$_;$changed{$_}\n";
}
close F;

 
#################################
sub by_count {
  unless (defined $changed{$a}) {
    return 1;
  }
  unless (defined $changed{$b}) {
    return -1;
  }
  ($changed{$b} <=> $changed{$a});
}
