#!/usr/bin/perl
use strict;
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $AMPM = "am";
if ($hour >= 12) {$AMPM = "pm"};



my %fields;
my %fieldnotes;
my %fieldsum;
my %fieldcount;

print "**Entering the FILENAME identifier for the slide you're counting and press ENTER\n";
print "**At the FIELD: prompt type any key for each cell you see in a field, then press ENTER\n";
print "**Enter any notes for that field (e.g. dividers, background brightness, etc.), then press ENTER\n";
print "**To end the program, type 'end' at the FIELD: prompt\n";


my $fieldtime = time;
$fields{$fieldtime} = 0;

print "FILENAME: ";

my $filename = <STDIN>;
chomp $filename;


print "START notes: ";
$fieldnotes{$fieldtime} = <STDIN>;
chomp $fieldnotes{$fieldtime};

$fieldsum{$fieldtime} = 0;
my $fieldtotal = 0;
my $fieldnumber = 0;

while() {

	$fieldnumber++;
	my $fieldtotal = $fieldsum{$fieldtime};
	$fieldtime = time;

###input field group numbers
	print "FIELD $fieldnumber: \a";
	my $fieldtaps = <STDIN>;
	chomp($fieldtaps);

###end loop with disabling expression "end"
	last if $fieldtaps eq 'end';

###save if not ending
	$fields{$fieldtime} = length($fieldtaps);
	$fieldcount{$fieldtime} = $fieldnumber;
	
###tally total number of fields
	$fieldsum{$fieldtime} = $fieldtotal + $fields{$fieldtime};
###input notes on field groups
	print 'notes(' . $fields{$fieldtime} . '/' . $fieldsum{$fieldtime} . " fields):";
	$fieldnotes{$fieldtime}= <STDIN>;
	chomp $fieldnotes{$fieldtime};
}

print "END notes: ";
$fieldnotes{$fieldtime} = <STDIN>;
chomp $fieldnotes{$fieldtime};
$fields{$fieldtime} = 0;
$fieldsum{$fieldtime} = $fieldtotal + $fields{$fieldtime};

open (fieldNOTES, ">$filename\_$fieldtime.csv") or die "Couldn't open file: $!";
print fieldNOTES "Date\tTime\tField\tCounts\tCumulative\tNotes\n";
foreach my $key (sort {$a <=> $b} (keys %fields)) {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($key);

  printf fieldNOTES "%4d-%02d-%02d\t%02d:%02d:%02d\t%u\t%u\t%u\t%s\n",$year+1900,$mon+1,$mday,$hour,$min,$sec,$fieldcount{$key},$fields{$key},$fieldsum{$key},$fieldnotes{$key};
}
close fieldNOTES or die "Couldn't open file: $!";


