#!/perl/bin/perl



system("del .\\archive_vz\\*.* /Q");



#------------0 ,JA,FE,MÄ,AP,MA,JU,JU,AU,SE,OK,NO,DE
@monDaten31 = (00,31,28,31,30,31,30,31,31,30,31,30,31);
sub date2min($thedata)
{
 $thedata = shift;
 @datetimesplit = split(/ /,$thedata);
 @thedatesplitxx = split(/-/,$datetimesplit[0]);
 @thetimesplit = split(/:/,$datetimesplit[1]);

 #$dasjahr = substr($thedatesplitxx[0],2)
 $dasmonat = $thedatesplitxx[1];
 $added_days = 0;
 for(my $ih = 0; $ih <= $dasmonat-1; $ih++)
 {
  $added_days += $monDaten31[$ih];
 }
#print $thedata."\n";
return (($added_days+$thedatesplitxx[2])*24*60)+(($thetimesplit[0]*60)+$thetimesplit[1]);
}




  #------------------ directions holen
  %dirDaten = ();
  open(DATEIdd2, "<camdaten.txt");
  while(<DATEIdd2>)
  { 
    @rsplit = split(/	/,$_);
    $dirDaten{$rsplit[0]} = $rsplit[2];
  }
  close(DATEIdd2); 


#-------------------------------- datum-liste einlesen
open(DATEIdd2, "<datasave.dat");
@datesplit = <DATEIdd2>;
close(DATEIdd2); 



@thecams = ("","0000","0001","0002","0003","0004","0005","0006","0007","0008","0009","000A","000B","000C","000D","000E","000F","0010","0011");
@the_formated_mins = ("<15m.","<30m.","<45m.","<1h","<2h","<4h","<6h","<8h","<10h","<12h","<24h",">24h");

$actual_datum_pos = 0;
foreach $singleline (@datesplit)
{
 @thedatesplit = split(/	/,$singleline);

  #--------------------------- datum-datei öffnen
  %verweilDaten_15_30_45 = ();
  $sammeldatei = "";
  $actual_pos_infile = 0;
  $filename = "$thedatesplit[0].txt";
  open(DATEI, "<archive/$filename");
  while(<DATEI>)
  {
   @mydatasplit = split(/	/,$_);
   $kommteventnr = $mydatasplit[0];
   $kommtcam = $mydatasplit[4];
   $kommtdatetime = $mydatasplit[3];
   $nummernschild = $mydatasplit[1];
   $direction = $dirDaten{$mydatasplit[4]};

   if ($direction eq "kommt")
   {
print "\n$actual_pos_infile.>>>>>>>>>>>>>>>>>>>>>>suchen $thedatesplit[0] $mydatasplit[0] mydatasplit[1] $direction\n";


    ##################### x tage, aber maximal $#arch...
    $zielpos = $actual_datum_pos + 7;
    $zielpos = $#datesplit if ($zielpos > $#datesplit);
    #------------------------------------------------------- SUCHVORGANG GEHT
    #$fehlerfall = 0;
    for(my $i = $actual_datum_pos; $i <= $zielpos; $i++)
    {
     @thedatasplit2 = split(/	/,$datesplit[$i]);

     open(DATEI2, "<archive/$thedatasplit2[0].txt");
     @archive_filedata = <DATEI2>;
     close(DATEI2);

     ##################### erste Datei ab Momentanposition
     $startpos = $thedatasplit2[0] eq $thedatesplit[0] ? $actual_pos_infile+1 : 0;
     for(my $i2 = $startpos; $i2 <= $#archive_filedata; $i2++)
     {
      @thedatasplit3 = split(/	/,$archive_filedata[$i2]);
      $nummernschild2 = $thedatasplit3[1];
      $direction2 = $dirDaten{$thedatasplit3[4]};
      if ($nummernschild2 eq $nummernschild && $direction2 eq "kommt")
      {
       #$fehlerfall = 1;
       goto nexthint;
      }
      if ($nummernschild2 eq $nummernschild && $direction2 eq "geht")
      {
       #if ($fehlerfall eq 0)
       #{
        $startminute = &date2min($mydatasplit[3]);
        $stopminute = &date2min($thedatasplit3[3]);
        $minutes = $stopminute - $startminute;
        $stunden = int($minutes / 60);
        $gehteventnr = $thedatasplit3[0];
        $gehtcam = $thedatasplit3[4];
        $gehtdatetime = $thedatasplit3[3];

        print "$actual_pos_infile.$mydatasplit[3]\n$i2.$thedatasplit3[3]\n";
        print "$i2...nummernschild2..$thedatasplit2[0]..min:$minutes..std:$stunden\n";

        $sammeldatei .= "$kommteventnr/$gehteventnr\t$nummernschild\t$kommtdatetime\t$gehtdatetime\t$kommtcam\t$gehtcam\t$minutes\t\t\n";

        #------- minuten formater
        $formated_min = ">24h" if ($minutes >= 1440);
        $formated_min = "<24h" if ($minutes < 1440);
        $formated_min = "<12h" if ($minutes < 720);
        $formated_min = "<10h" if ($minutes < 600);
        $formated_min = "<8h" if ($minutes < 480);
        $formated_min = "<6h" if ($minutes < 360);
        $formated_min = "<4h" if ($minutes < 240);
        $formated_min = "<2h" if ($minutes < 120);
        $formated_min = "<1h" if ($minutes < 60);
        $formated_min = "<45m." if ($minutes < 45);
        $formated_min = "<30m." if ($minutes < 30);
        $formated_min = "<15m." if ($minutes < 15);

        #------- formated datasammel
        $verweilDaten_15_30_45{"$formated_min-"}++;
        $verweilDaten_15_30_45{"$formated_min-$kommtcam"}++;

        goto nexthint;
       #}
      }
     }
    }
   }

  nexthint:
  $actual_pos_infile++;
  }
  close(DATEI); 

  open(DATEIb, ">archive_vz/$filename");
  print DATEIb $sammeldatei;
  close(DATEIb); 

    $sammeldatasave = "";
    foreach $theformated_mins (@the_formated_mins)
    {
     $sammelvarious = "";
     foreach $thecams (@thecams)
     {
      $idx = "$theformated_mins-$thecams";

      $sammelvarious .= "$verweilDaten_15_30_45{$idx}	";
     }
     $sammeldatasave .= "$theformated_mins	$sammelvarious	\n";
    }
    chop($sammeldatasave);

  open(DATEIb, ">archive_15_30_45/$filename");
  print DATEIb $sammeldatasave;
  close(DATEIb); 

$actual_datum_pos++;
}






