//Code is under active development.
//Feel free to use at your own risk.
//No copyright asserted.

#!/usr/bin/php -q
<?php
//$start=microtime(); 

$year=date("Y");
$stdin fopen('php://stdin','r');

if (
$stdin) {  
  while (!
feof($stdin)) {
    
$line trim(fgetss($stdin,4096));    
    
$next_line trim(fgetss($stdin,4096));    
    
    
//handling line folding in emails
    
while ($next_line) {
      if (
substr($line,-1)=="=") {
          
$line substr($line,0,-1) . $next_line;
      }
      else {
          
$line $line ." "$next_line;
      }
      
      
$next_line trim(fgetss($stdin,4096));
    }
               
    
$record explode(" ",$line); //print_r($record);    
    
$word=current($record);
                      
    if (
substr($word,0,4) == $year) {
      
$date $word//echo "date=". $date ."<br />";
      
$keyword next($record); 
        
      if (
$keyword=="reverse") {
        
$rev_phrase $date ." reverse ";
        
$date next($record);
        
$keyword next($record);
      } 
        
      if (
$keyword=="from") {      
        
$from_icb=next($record);
        
$to_wordnext($record);
                 
        if (
$to_word=="to") {
          
$to_icb=next($record);
          
$amount=next($record);        
          
$units next($record);
          
          if (
substr($units,-1)!=".") {
            
$id next($record);            
          }
                        
          
$record_copy $rev_phrase ." "$date ." from "$from_icb ." to "$to_icb ." "number_format($amount,2,".","") ." "$units ." "$id;          
          
        }      
      }
      else if (
$keyword=="add" OR $keyword=="cut") { //this might have reporter also      
        
$amountnext($record);
        
$units next($record);
        
$other_keyword next($record); 
        
$main_icb=next($record);
        
$param next($record);
                          
        if (
substr($param,-1)!=".") { 
          
$id " "next($record);
        } 
              
        
$record_copy $date ." "$keyword ." "number_format($amount,2,".","") ." "$units ." to "$main_icb ." "$param $id;  //this record copy will be trimmed later     
      
}
      else if (
$keyword=="set") { 
        
$main_icb next($record);
        
$param next($record);
        
$to_word next($record);
        
$param_value next($record); 
        
$units next($record); //this will be the string length         
          
        
if ($param!="lastReportSHA1") {
          
$param_value number_format($param_value,2,".","");
        }            
          
        
$record_copy $date ." set "$main_icb ." "$param ." to "$param_value ." "$units;             
      }

    if (
$record_copy) break;
    }
  }
}

fclose($stdin);

file_put_contents("demo_log.txt","\n"$record_copy,FILE_APPEND);

if (
$record_copy) {
  
$_GET['prowl'] = trim($record_copy);
  
$return_by "mail"//to indicate how to return messages, timing issues    
  
include ("reporter.php");
  
//echo $message to reply to email sender;
}

//$stop = microtime(); 
//echo "<br />total time: ". ($stop-$start) ."s"; 
?>
1