use strict;
use warnings;
# this script generates a gnuplot script to plot all txt files from 'indir'
# and generate png's in 'outdir'
my $indir=shift;
my $outdir=shift;
printf("system \"mkdir -p %s/%s\"\n", $outdir, $_) for qw(hi lo loh ulo uloh);
for my $fn (glob "$indir/log*") {
	(my $date=$fn ) =~ s/.*log-(\d.*)/$1/;
	next unless (-e $fn && -s $fn);
	printf("set term png\n");
	printf("set output \"$outdir/hi/%s.png\"\n", $date);
	printf("plot [0:3600] \"%s\" using (int(\$1)%%3600):(\$2)\n", $fn);

	printf("set output \"$outdir/lo/%s.png\"\n", $date);
	printf("plot [0:3600] \"%s\" using (int(\$1)%%3600):(\$3)\n", $fn);
	printf("set output \"$outdir/loh/%s.png\"\n", $date);
	printf("plot [0:3600] \"%s\" using (int(\$1)%%3600):(\$3-0.396*(int(\$1)%%3600))\n", $fn);

	printf("set output \"$outdir/ulo/%s.png\"\n", $date);
	printf("plot [0:3600] \"%s\" using (int(\$1)%%3600):(\$3) smooth unique\n", $fn);
	printf("set output \"$outdir/uloh/%s.png\"\n", $date);
	printf("plot [0:3600] \"%s\" using (int(\$1)%%3600):(\$3-0.396*(int(\$1)%%3600)) smooth unique\n", $fn);

}

sub getstart {
	my ($fn)=@_;
	open FH, "<$fn" or die "$fn: $!\n";
	my $line= <FH>;
	close FH;
	if ($line =~ /\d+/) {
		return $&;
	}
	return 0;
}
