#!/perl/bin/perl

use Time::Local;


print "Content-type: text/html\n\n";


  #------------------ regionen.txt holen
  %rDaten = ();
  open(DATEIdd2, "<regionen.txt");
  while(<DATEIdd2>)
  { 
    @rsplit = split(/	/,$_);
    $rDaten{$rsplit[0]} = $rsplit[1];
  }
  close(DATEIdd2); 

  #------------------ directions holen
  %dirDaten = ();
  open(DATEIdd2, "<camdaten.txt");
  while(<DATEIdd2>)
  { 
    @rsplit = split(/	/,$_);
    $dirDaten{$rsplit[0]} = $rsplit[2];
  }
  close(DATEIdd2); 




  ################################ splitted 24h-daten speicherung
  @thetime = ("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
  #@thecams = ("","CAM_1","CAM_2","CAM_3","CAM_4","CAM_5","CAM_6","CAM_7","CAM_8","CAM_9");
  @thecams = ("","0000","0001","0002","0003","0004","0005","0006","0007","0008","0009","000A","000B","000C","000D","000E","000F","0010","0011");
  @thedirs = ("","kommt","geht");
  @w_days = ("So","Mo","Di","Mi","Do","Fr","Sa");
  @codetab = ("à","á","è","é","ì","í","ò","ó","ù","ú");

%datesammel_mon = ();
%datesammel_mon_MIXED = ();
%monatsDaten = ();
%regionsDaten = ();
%camDaten = ();
$datasave_sammel = "";
$eintragid = 0;
$direction="";
foreach my $day (<c:/Programme/AutoPlate/log/*/*/*>)
{
  #---------------------------- Wochentag ermitteln
  $day=~m/(\d{4})\/(\d\d)\/(\d\d)$/;
  $tagmonatjahr = "$3.$2.$1";
  #--------------------------------- calc
  ($d,$m,$y) = split /\./, $tagmonatjahr;
  $m         = $m - 1;
  $y         = $y - 1900;
  $sec       = timelocal(00,00,00, $d, $m, $y);
  ($tmp, $tmp, $tmp, $tmp, $tmp, $tmp, $wday) = localtime($sec);

  #---------------------------------- main foreach
  %regionsDaten_dayli = ();
  %datesammel_bt_MIXED_dayli = ();
  %stundensammel = ();
  my $tosave = "";
  my $perday = 0;
  my $schildkiller_lastschild = "";
  my $schildkiller_lastcam = "";
  foreach my $image (<$day/*/*p_*.jpg>)
  {
    $image=~m/(\d{4})\/(\d\d)\/(\d\d)\/(\d\d)\/(\d\d)(\d\d)\d\d_([a-zA-Z0-9]{4})p_(.+)\./;
    #print "$eintragid	$8		$1-$2-$3 $4:$5:$6	$7\n";
    $direction = $dirDaten{$7};
    $jahrmonat = "$1-$2";
    $jahrmonattag = "$1-$2-$3";
    $stunde = "$4";
    $getyet = "$1-$2-$3 $4:$5:$6	$7";
    $thecam = $7;
    $camDaten{$7}++;
    $schild = $8;
    $schild =~ s/-/_/g;
    @regionsplit = split(/_/,$schild);
    #--------------------------------------- schild auf _bug testen
    my $Laenge = length($regionsplit[0]);
    if ($Laenge > 3)
    {
#print "$schild\n";
     #---- $1 löschen
     my $not = "";
     $not =~ m/()/;
     #--- now code
     my $anfang = substr($schild, 0, 3);
     my $ende = substr($schild, 3);
     $anfang =~ s/([0-9].*)//g;
     $ende  =~ s/_//g;
     $schild = "$anfang"."_"."$1$ende";
     $schild = "?".$schild if (0 eq index($schild,"_"));
     @regionsplit = split(/_/,$schild);
#print "$schild.....$1\n";
    }
    #--------------------------------------- verschlüssle schild
    my $MagischeZahl = $regionsplit[1];
    my $Laenge = length($MagischeZahl);
    my $Quersumme = 1;
    for(my $i=0; $i<$Laenge; $i++) {
       my $Zeichen = substr($MagischeZahl, $i, 1);
       $Quersumme = $Quersumme*int(ord($Zeichen));
    }
    @summensplit = split(//,$Quersumme);
    my $Quersumme = "";
    foreach $summenteil (@summensplit)
    {
     $Quersumme .= $codetab[$summenteil];
    }
    #my $MagischeZahl = "$Quersumme";
    #my $Laenge = length($MagischeZahl);
    #my $Quersumme = "";
    #for(my $i=0; $i<$Laenge; $i++) {
    #   my $Zeichen = substr($MagischeZahl, $i, 1);
    #   $Quersumme = "$Quersumme".$codetab[$Zeichen];
    #}
    $schild = "$regionsplit[0]_$Quersumme";
    @regionsplit = split(/_/,$schild);

    #--------- doppeleinträge entfernen !
    if ("$schildkiller_lastschild $schildkiller_lastcam" ne "$schild $thecam")
    {
     if ($#regionsplit eq 1)
     {
      $regionsDaten{$regionsplit[0]}++;
      $regionsDaten_dayli{$regionsplit[0]}++;
      #print "$regionsplit[0]..$#regionsplit\n";
     }

     $tosave .= "$eintragid	$schild	$w_days[$wday]	$getyet	$direction		\n";
     #print $tosave;

     #------------------------------------ monatsdatensammler
     $monatsDaten{$jahrmonat}++;

     $datesammel_mon{"$jahrmonat"}++;
     $datesammel_mon_MIXED{"$jahrmonat-"}++;
     $datesammel_mon_MIXED{"$jahrmonat-$direction"}++;
     $datesammel_mon_MIXED{"$jahrmonat-$thecam"}++;
     $datesammel_mon_MIXED{"$jahrmonat-$thecam$direction"}++;

     $stundensammel{"$jahrmonattag-$stunde-"}++;
     $stundensammel{"$jahrmonattag-$stunde-$direction"}++;
     $stundensammel{"$jahrmonattag-$stunde-$thecam"}++;
     $stundensammel{"$jahrmonattag-$stunde-$thecam$direction"}++;

     $datesammel_bt_MIXED_dayli{"hash-"}++;
     $datesammel_bt_MIXED_dayli{"hash-$direction"}++;
     $datesammel_bt_MIXED_dayli{"hash-$thecam"}++;
     $datesammel_bt_MIXED_dayli{"hash-$thecam$direction"}++;

     $perday++;
     $eintragid++;
    }
    $schildkiller_lastcam = $thecam;
    $schildkiller_lastschild = $schild;
  }
  $day=~m/(\d{4})\/(\d\d)\/(\d\d)$/;
  $date = "$1-$2-$3";
  $filename = "$1-$2-$3.txt";
  $datasave_sammel .= "$1-$2-$3	$perday	\n";
  print "$3.$2.$1 - $perday\n";

  open(DATEId, ">archive/$filename");
  print DATEId $tosave;
  close(DATEId); 

  #--------------------------------------- regions-daten (dayli) auswerten ..
  @sammelreg = ();
  #$longname = "";
  @Schluessel = keys(%regionsDaten_dayli);
  #@sortedkeys = sort(@Schluessel);
  foreach $singlekeys (@Schluessel)#(@sortedkeys)
  {
   #$sammeldatasave_reg .= "$singlekeys	$regionsDaten_dayli{$singlekeys}	\n";
   $long = sprintf("%010d",$regionsDaten_dayli{$singlekeys});

   if (exists($rDaten{$singlekeys}))
   {
      $longname = $rDaten{$singlekeys};
      $longname =~ s/\n//g;
   } else {$longname="?";}

   push(@sammelreg,"$long	$singlekeys	$longname	");
  }

  @sammelreg = sort(@sammelreg);
  @sammelreg = reverse(@sammelreg);
  my $saveit = join("\n",@sammelreg);
  #chop($saveit);

  open(DATEIb, ">archive_reg/$filename");
  print DATEIb "$saveit";
  close(DATEIb); 



  #-------------------------------- 24h-daten speichern
  #@Schluessel = keys(%datesammel);
  #@sortedkeys = sort(@Schluessel);
  #foreach $singlekeys (@sortedkeys)
  #{
    $sammeldatasave = "";
    foreach $thetime (@thetime)
    {
     $sammelvarious = "";
     foreach $thecams (@thecams)
     {
      foreach $thedirs (@thedirs)
      {
       $idx = "$date-$thetime-$thecams$thedirs";
       $idxkommt = "$date-$thetime-$thecams"."kommt";
       $idxgeht = "$date-$thetime-$thecams"."geht";
       if (exists($stundensammel{$idx}))
       {
        $sammelvarious .= "$stundensammel{$idx}-$stundensammel{$idxkommt}-$stundensammel{$idxgeht}	";
       } else {$sammelvarious .= "	";}
      }
     }
     $sammeldatasave .= "$thetime	$sammelvarious	\n";
    }
    chop($sammeldatasave);
   
    open(DATEIb, ">archive_24h/$filename");
    print DATEIb "$sammeldatasave";
    close(DATEIb); 
  #}







 ################################ dayli splitted besucher-tab speicherung
 #print "dayli splitted besucher-tab sicherung..\n";
 #@thetime = ("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
 #@thecams = ("","CAM_1","CAM_2","CAM_3","CAM_4","CAM_5","CAM_6","CAM_7","CAM_8","CAM_9");
 #@thedirs = ("","kommt","geht");

   #-------------------------------- bt-daten speichern
   #@Schluessel = keys(%datesammel);
   #@sortedkeys = sort(@Schluessel);
   #foreach $singlekeys (@sortedkeys)
   #{
     #$sammeldatasave = "";
     #foreach $thetime (@thetime)
     #{
      $sammelvarious = "";
      foreach $thecams (@thecams)
      {
       foreach $thedirs (@thedirs)
       {
        $idx = "hash-$thecams$thedirs";
        $idxkommt = "hash-$thecams"."kommt";
        $idxgeht = "hash-$thecams"."geht";
        if (exists($datesammel_bt_MIXED_dayli{$idx})) {$sammelvarious .= "$datesammel_bt_MIXED_dayli{$idx}-$datesammel_bt_MIXED_dayli{$idxkommt}-$datesammel_bt_MIXED_dayli{$idxgeht}	";} else {$sammelvarious .= "	";}
       }
      }
     # $sammeldatasave .= "$thetime	$sammelvarious	\n";
     #}
     #chop($sammeldatasave);
   
     open(DATEIb, ">archive_bt/$filename");
     print DATEIb "$sammelvarious";
     close(DATEIb); 
   #}









}
open(DATEId, ">datasave.dat");
print DATEId $datasave_sammel;
close(DATEId); 


  #-------------------------------- Monatsdaten speichern
  $sammeldatasave_cam = "";
  @Schluessel = keys(%camDaten);
  @sortedkeys = sort(@Schluessel);
  foreach $singlekeys (@sortedkeys)
  {
   $sammeldatasave_cam .= "$singlekeys	$camDaten{$singlekeys}	\n";
  }
  chop($sammeldatasave_cam);

  #open(DATEIb, ">datasave_monat.dat");
  open(DATEIb, ">datasave_cam.dat");
  print DATEIb "$sammeldatasave_cam";
  close(DATEIb); 


  #---------------------------- Cam-daten auswerten ..
  $sammeldatasave_mon = "";
  @Schluessel = keys(%monatsDaten);
  @sortedkeys = sort(@Schluessel);
  foreach $singlekeys (@sortedkeys)
  {
   $sammeldatasave_mon .= "$singlekeys	$monatsDaten{$singlekeys}	\n";
  }
  chop($sammeldatasave_mon);

  open(DATEIb, ">datasave_mon.dat");
  print DATEIb "$sammeldatasave_mon";
  close(DATEIb); 






 ################################ splitted mon-daten speicherung
 #@thecams = ("","CAM_1","CAM_2","CAM_3","CAM_4","CAM_5","CAM_6","CAM_7","CAM_8","CAM_9");
 @thecams = ("","0000","0001","0002","0003","0004","0005","0006","0007","0008","0009","000A","000B","000C","000D","000E","000F","0010","0011");
 @thedirs = ("","kommt","geht");
 foreach $thecams (@thecams)
 {
  foreach $thedirs (@thedirs)
  {
   #-------------------------------- mon-daten speichern
   $sammeldatasave = "";
   @Schluessel = keys(%datesammel_mon);
   @sortedkeys = sort(@Schluessel);
   foreach $singlekeys (@sortedkeys)
   {
    $idx = "$singlekeys-$thecams$thedirs";
    $idxkommt = "$singlekeys-$thecams"."kommt";
    $idxgeht = "$singlekeys-$thecams"."geht";
    if (exists $datesammel_mon_MIXED{$idx})
    {
     $sammeldatasave .= "$singlekeys	$datesammel_mon_MIXED{$idx}	$datesammel_mon_MIXED{$idxkommt}	$datesammel_mon_MIXED{$idxgeht}	\n";
     print $datesammel_mon_MIXED{"$singlekeys-$thecams$thedirs"};
    }
   }
   chop($sammeldatasave);
   
   open(DATEIb, ">archive_mon/datasave_mon_$thecams$thedirs.txt");
   print DATEIb "$sammeldatasave";
   close(DATEIb); 
  }
 }






  #---------------------------- regions-daten (gesamt) auswerten ..
  #$sammeldatasave_reg = "";
  @sammelreg = ();
  #$longname = "";
  @Schluessel = keys(%regionsDaten);
  #@sortedkeys = sort(@Schluessel);
  foreach $singlekeys (@Schluessel)#(@sortedkeys)
  {
   #$sammeldatasave_reg .= "$singlekeys	$regionsDaten{$singlekeys}	\n";
   $long = sprintf("%010d",$regionsDaten{$singlekeys});

   if (exists($rDaten{$singlekeys}))
   {
      $longname = $rDaten{$singlekeys};
      $longname =~ s/\n//g;
   } else {$longname="?";}

   push(@sammelreg,"$long	$singlekeys	$longname	");
  }
  #chop($sammeldatasave_reg);

  @sammelreg = sort(@sammelreg);
  @sammelreg = reverse(@sammelreg);
  my $saveit = join("\n",@sammelreg);
  #chop($saveit);

  open(DATEIb, ">datasave_reg.dat");
  print DATEIb "$saveit";
  close(DATEIb); 





#-------------------------------- datasave_real_????.dat anlegen zum markieren des tatsächlichen tagesausfalls
#---------------------JA,FE,MÄ,AP,MA,JU,JU,AU,SE,OK,NO,DE
@the_dayDaten = (31,28,31,30,31,30,31,31,30,31,30,31);
@the_monDaten = ("01","02","03","04","05","06","07","08","09","10","11","12");
@the_yearDaten = ("2007","2008","2009","2010","2011","2012","2013");
%schalter = ( "2007" => 28,
              "2008" => 29,
              "2009" => 28,
              "2010" => 28,
              "2011" => 28,
              "2012" => 29,
              "2013" => 28 );


foreach $my_years (@the_yearDaten)
{
 $theschalter = $schalter{$my_years};
 $the_dayDaten[1] = $theschalter;
 $sammeldata = "";
 foreach $my_mons (@the_monDaten)
 {
  $the_for_days = $the_dayDaten[$my_mons-1];
  for(my $i = 1; $i <= $the_for_days; $i++)
  {
   $formated_day = $i < 10 ? "0".$i : $i;
   $sammeldata .= "$my_years-$my_mons-$formated_day		\n";
  }
 }
 open(DATEIb, ">archive_real/datasave_real_$my_years.dat");
 print DATEIb $sammeldata;
 close(DATEIb); 
}




