#!/usr/bin/perl

use strict;
use warnings FATAL => 'all';

my $lastin = 0.0;
my $innow = 0;
my $lastout = 0.0;
my $outnow = 0;
my $i = 0;

open (INPUT, "<", "rrd_values") || die "Can't open file rrd_values: $!\n";
  open (OUTPUT, ">", "rrd_values_max") || die "Can't open file rrd_values_max: $!\n";

  while (<INPUT>) {

    my @values = split(/ /);
    chomp($values[2]);

    if ($values[1] eq "nan") {
      $innow = $lastin;
    } else {
      $innow = sprintf("%f",$values[1]);
      $lastin = $innow;
    }

    if ($values[2] eq "nan") {
      $outnow = $lastout;
    } else {
      $outnow = sprintf("%f",$values[2]);
      $lastout = $outnow;
    }

    if ($innow >= $outnow) { 
      print OUTPUT "$innow\n";
    } else {
      print OUTPUT "$outnow\n";
    }
  }

  close OUTPUT;
close INPUT;
