#dan-response
( $input, $ouput ) = @ARGV;

print "\n--- dan-response.pl\n";
print "--- Takes a Fray second-order output file as its first parameter.\n";
print "    Writes a headed, comma-delimited output file containing response times.\n\n";

die "Must specify an input file.\n" unless $input;

# Binary "." concatenates two strings. Documented in perlop.html

$output = $input . "-responses.txt" unless $output;

print "Input file is $input\n";
print "Output file is $output\n";

# I'm new to Perl. Key functions:
# open, close, print, substr, split - documented in perlfunc.html
# pattern matching - in "RegExp Quotelike Operators" in perlop.html
#                  - and perlre.html
# Binary "=~" binds a scalar to a pattern match. "!~" inverts the truth of the result.
# Parentheses within a pattern returns a list of matches. The assignment will be tru
# if the pattern matched.
#
# Program flow:
#   open input/output files

open(Input, "<" . $input) || die "Can't read input file.\n";

# Must read "I/O Operators" section in perlop.html to follow this properly.
# And perlsyn.html is basic stuff.

#   get rat name and date/time, looks like this: 
#         Box 6: 45 - ; Sun,09 May 1999.08:16:11, 192 min session
#                ^^
#                rat
#

HEADER: while(<Input>) {
  
  if (/Box (\d+): (\d+)\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)\W+(\w+)/) {
    # Bizarrely, I can't get this to work with "^Box", even though it's the start of a line.
    # Something to do with "Embedded newlines will not be matched by ^ or $"? (from perlre.html)
    $box = $1;
    $rat = $2;
    $dayname = $3;
    $day = $4;
    $month = $5;
    $year = $6;
    $hour = $7;
    $min = $8;
    # print "*** FOUND. Box $box, rat $rat, dayname $dayname, day $day, mo $month, y $year, h $hour, m $min\n";
    last HEADER;
  }
  die "Couldn't find header.\n" if eof(Input);
}

$date_time = $day . substr($month,0,3) . substr($year,2,2) . "_" . $hour . $min;

print "Rat:       $rat\n";
print "Date/time: $date_time\n";

#   go to this point:
#
#         Response times - active lever (dsec):
#
# The "^" in the pattern ties it to the first column.
# / and / delimit a pattern.

RESPONSEPOINT: while(<Input>) {
  last RESPONSEPOINT if /Response times - active lever .*$/;
  #\(dsec\)
  die "Couldn't find response times.\n" if eof(Input);
}

#   then (ignoring superfluous carriage returns), the next line is a very long single line
#   containing all the response time in ds. Read it/split it into an array.
#   Since no pattern given to "split", splits on whitespace (which is what we want).

$_ = <Input>;   # we were looking at the "Response times - active lever" line; want to move on one.
chop;     # get rid of trailing newline
@responsetimes = split;
# print "response times: @responsetimes";


#   Output a header.

open(Output, ">" . $output) || die "Can't write to output file.\n";
print Output "RAT,DATE_TIME,RESPONSE_TIME\n";

#   Then walk through the array and output something neat.
#   Response times are in ds.
#   $#responsetimes is the subscript of the last member. (see perlsyn.html)
#   First element is at 0.
#   $responsetimes[25] is the 25th element.
#   @responsetimes is the whole array.

$first_hour = 0;
$second_hour = 0;
$third_hour = 0;
$total = $#responsetimes + 1;

for ($i = 0; $i <= $#responsetimes; $i++) {
  print Output $rat . "," . $date_time . "," . $responsetimes[$i] . "\n";
	if ($responsetimes[$i] < 36000) { $first_hour ++ };
	if ($responsetimes[$i] >= 36000 && $responsetimes[$i] < 72000) { $second_hour ++ };
	if ($responsetimes[$i] >= 72000 && $responsetimes[$i] < 108000) { $third_hour ++ };
}

print "Active responses - first hour $first_hour, second hour $second_hour, third hour $third_hour, total $total.\n";

#   Close and terminate.

close(Input);
close(Output);
