#!/usr/bin/perl -w

# Convert irtest output to tivo db format
# Output of this program needs to be further converted with
# the writeguide program to before loading it into the database.
#
# Christopher Yeoh <cyeoh@linuxcare.com>
#
# Expects input file of format:
#
# <index_name>: 
#
# x.x.x x.x.x x.x.x x.x.x
#
use strict;

sub roundint($)
{
  my($num) = shift;
  if ( ($num-int($num))>0.5)
  {
    return int($num)+1;
  }
  else
  {
    return int($num);
  }
}

if ($#ARGV!=5)
{
  print STDERR "Usage: $0 <input_file> <output_file> <record_number> <version_number> <device_name> <magic_number>\n";
  exit(1);
}

my($inputFile) = shift(@ARGV);
my($outputFile) = shift(@ARGV);
my($recordNumber) = shift(@ARGV);
my($versionNumber) = shift(@ARGV);
my($deviceName) = shift(@ARGV);
my($magicNumber) = shift(@ARGV);
local(*INFILE);
local(*OUTFILE);
my($line);
my($key) = "";
my(@array);
my(%data);
my($triplet);
my(@tripletSplit);
my($val);

die "Could not open input file" unless open(INFILE, $inputFile);
die "Could not open output file" unless open(OUTFILE, ">$outputFile");

while (defined($line=<INFILE>))
{
  chomp($line);
  next unless $line !~ /^\s*$/;
  if ($line =~ /^(.*):/)
  {
    $key = $1;
    $data{$key}{COUNT} = 0;
    $data{$key}{IND} = ();
    $data{$key}{SUM} = ();
#    print STDERR "Initialise: $key\n";
  }
  else
  {
    if ($key ne "")
    {
#      $data{$key} = ();
      @array = split(/\s+/, $line);
      if ($#array>6)
      {
#	print "Key is: $key\n";
	$data{$key}{COUNT} = $data{$key}{COUNT}+1;
	$data{$key}{IND}[$data{$key}{COUNT}-1] = ();

	my($arrayIndex) = 0;
	foreach $triplet (@array)
	{
	  $triplet =~ s/^\s*(.*)/$1/;
	  $triplet =~ s/(.*)\s*$/$1/;
	  @tripletSplit = split(/\./, $triplet);
	  if ($#tripletSplit==2)
	  {
	    $val = $tripletSplit[0]*3;
	    push(@{$data{$key}{IND}[$data{$key}{COUNT}-1]},$val);
	    if ($data{$key}{COUNT}==1)
	    {
#	      print "Got first\n";
	      push(@{$data{$key}{SUM}}, $val);
	    }
	    else
	    {
	      if ($arrayIndex>$#{@{$data{$key}{SUM}}})
	      {
#		print STDERR"Oversized data array for $key ($arrayIndex, $#{@{$data{$key}{SUM}}})\n";
	      }
	      else
	      {
		$data{$key}{SUM}[$arrayIndex] += $val;
	      }
	    }
	  }
	  $arrayIndex++;
	}
	if ($arrayIndex-1 != $#{@{$data{$key}{SUM}}})
	{
	  my($realCount) = $#{@{$data{$key}{SUM}}}+1;
	  print STDERR "Mismatch array for $key (Entry: $data{$key}{COUNT}, Have: $arrayIndex, Want: $realCount)\n";
	}
#	print STDERR "$key: $arrayIndex\n";
      }
    }
    else
    {
      print STDERR "got noise: $line\n";
    }
  }
}


print OUTFILE <<EOF;
Guide type=3

IrTivoFormat/1/$recordNumber/$versionNumber {
EOF

# Normalise
my($sumVal);
foreach $key (keys %data)
{
  $data{$key}{VAL} = ();
  foreach $sumVal (@{$data{$key}{SUM}})
  {
    push(@{$data{$key}{VAL}}, $sumVal / $data{$key}{COUNT});
  }
}

my($sum);
my($rawArray);
my($arrayIndex);
my($entryCount);
foreach $key (keys %data)
{
  print OUTFILE "  $key: 0 $magicNumber ";
  $sum =  ($#{@{$data{$key}{VAL}}}+1)/2;
  print OUTFILE $sum;
  print OUTFILE " 0 ";
#  print " ";
  $arrayIndex = 0;
  my($rounded);
  foreach $val (@{$data{$key}{VAL}})
  {
    $rounded = roundint($val);

    print OUTFILE "$rounded ";

    # Sanity checking code
    my($diff);
    my($pdiff);
    $entryCount = 0;
    foreach $rawArray (@{$data{$key}{IND}})
    {
      $entryCount++;
      $diff = $val-$rawArray->[$arrayIndex];
      $pdiff = 100*$diff/$val;
      if ( ($pdiff>0 && $pdiff>15) || ($pdiff<0 && $pdiff<-15))
      {
	print "$key Large Error ($entryCount, $arrayIndex) : ($pdiff%, $rawArray->[$arrayIndex], $val)\n";
      }
    }
    $arrayIndex++;
  }
  print OUTFILE "\n";
}
print OUTFILE <<EOF;
  DeviceName: {$deviceName}
}

EOF
