#!/usr/bin/perl

require "getopts.pl";
require "flush.pl";
require "ctime.pl";
require "timelocal.pl";

use LWP::Simple;
use File::Temp qw(tempfile);

$typegraph = $ARGV[0];

$url1 = "http://ssds-prod.mbari.org/ssds/data/raw-data?deviceID=1780&lastNumberOfPackets=150&isi=0&noHTMLHeader=1";

loop:;
$data = get($url1);

print "$data\n";

# remove the file when the reference goes away with UNLINK option (1 remove 0 keep)
$tmp_fh = new File::Temp( UNLINK => 0 );
#print "temp filename: $tmp_fh\n";

my @lines = split /\n/, $data;

foreach my $line (@lines) {

@fields = split /, /, $line;
$GF375 = $fields[1];
$GF375com = $fields[2];
$GF48 = $fields[3];
$GF48com = $fields[4];
$datetime = $fields[6];

print $tmp_fh "$datetime $GF375 $GF375com $GF48 $GF48com\n";
}

open (PLOT,"|/usr/bin/gnuplot");
print PLOT "set terminal png x000000 xdddddd x004400\n";
print PLOT "set size .75,.5\n";
print PLOT "set xdata time\n";
print PLOT "set timefmt '%Y-%m-%d %H:%M:%S'\n";
print PLOT "set xrange [*:*]\n";
print PLOT "set title '375 volt GF'\n";
print PLOT "plot '$tmp_fh' using 1:3\n";
close (PLOT);

#sleep 60;

#goto loop;

