
#!/usr/bin/perl
# receive imi results
# save lines which have more than 5 fields and start with imi_result;
#
# ------------------------- Change this !
my $RESULT_DIR='imi2esb/results';        # received data directory
my $RESULT_FN=$RESULT_DIR.'/imi.data';   # data is saved in  ~/data/imi.data
my $RESULT_SCRIPT='bin/parseresults.sh'; # after receiving data, start this script
# ---------------------------------------
#
my $nook=0;
my $now=time;
# --- create directory if it does not exists
mkdir($RESULT_DIR)  if (! -d $RESULT_DIR);


# append received data to file if data is correct

if (open my $io, '>>'.$RESULT_FN) {
   # -- parse input data
   while (<STDIN>) {
      my $line="$_";
      $line=~ s/[\r\n]*//g;
      my @arr=split(/;/,$line);
      if ($#arr > 5 || $arr[0] eq 'imi_result') {
         print $io "$line\n";
         $nook++;
      }
   }
   close($io);
   chmod(0660,$RESULT_FN);
}
if ($nook > 0 ) {
   # -- report back ok and trigger the processing job
   print "OKdone.\n";
   qx{$RESULT_SCRIPT $RESULT_FN}  if (-f $RESULT_SCRIPT);
} else {
   print "ERRdone.\n";
}
exit 0;
#EOF

