#!/usr/local/bin/perl -w
##
###########################################################################
##
##  Program:  parseCircOverrides.pl
##
##  Purpose:  Take the output of the "L > LIST circulation overrides"
##            command and massage it into a tab-delimited file
##
##  Version:  1.0   13 Jul 1998
##
##  Author:   Peter Murray, Digital Media Services
##            Case Western Reserve University
##            http://www.cwru.edu/home/pem.html
##
##  Legalities:
##            Copyright 1998 by Case Western Reserve University.
##            All rights reserved.
##
##  Revision History:
##    13 Jul 1998  pem  Initial Release
##
## DOCUMENTATION, in PERL POD format, is at the end of the program.
## Running the program `perldoc <programname>` should output the manual.
##
use strict;

my ($date, $terminalID, $initials, $patronRec);

while (<>) {                    ## For every line in the file...
  chomp;                        ## Remove the trailing newline.
  next if $_ eq '';             ## Skip the line if empty.
  next if $_ =~ /^\s*\d+\s*$/;  ## Skip the line if it is a page number.
  
 ## If the line starts with a non-space, then parse three components out of
 ## it:  the date, the terminal ID, and the Innopac initials for the person
 ## doing this circulation override.
  if ($_ =~ /^(.*)\sTerminal\s(\d+), Initials:?\s(.*?):?$/) {
    $date = $1;
    $terminalID = $2;
    $initials = $3;
    next;
  } else {
 ## If we didn't see the constant line above, then we must assume it is
 ## one of the variable text lines that lists the actual override.  We
 ## want to keep most of this intact except for extracting out the patron
 ## record number so that this can be used as a sorting point.
    $_ =~ s/^\s+//;             ## Remove leading spaces from the line
    if ($_ =~ /^Overrode ([RD]D|hold).*\.(p[x\d]+)/) {
      $patronRec = $2;                 ## The "Overrode DD" lines put the patron
    } elsif ($_ =~ /\.(p[x\d]+)\.?$/) {## record number in a place distinct from
      $patronRec = $1;                 ## every other line in the file (commonly
    } elsif ($_ =~ /for library use/) {## at the end of the line) -- and at
      $patronRec = '';                 ## times doesn't even exist!
    } elsif ($_ =~ /^Purged payments/) {
      $patronRec = '';
    } else {
      warn "Couldn't find patron rec number in $_";
      next;                            ## Skip this entry if we couldn't find it
    }      
    print "$date\t$terminalID\t$initials\t$patronRec\t$_\n";
    next;
  }
}


=head1 NAME

parseCircOverrides - Massage Circulation Overrides report into a tab-delimted file

=head1 SYNOPSIS

  parseCircOverrides.pl  [ report-file ]

=head1 DESCRIPTION

This program takes the output of the INNOPAC "L > LIST circulation overrides"
report and generates a tab-delimited file suitable for uploading into a 
spreadsheet application.  Since key data elements are in their own fields, it
is possible to sort on them to get a picture of who is doing what overrides
at what locations.

=head1 PARAMETERS

=over 4

=item [ report-file ]

The output of the "L > LIST circulation overrides" program must be saved to
a file somehow so that this program can read it.  Some ways to get this
report to a file are listed at http://www.innopacusers.org/faqs/print/print-to-file.html

The filename can either be specified on the command line or come into this
program on STDIN.

=back

The tab-delimited file is written to standard out.

=head1 EXAMPLE

  parseCircOverrides.pl report-from-innopac.txt > tab-delimited-file.txt

=head1 COPYRIGHT

Copyright 1998 by Case Western Reserve University.

=head1 AUTHOR

 Peter Murray
 Library Systems Manager
 Digital Media Services
 Case Western Reserve University
 pem@po.cwru.edu
 http://www.cwru.edu/home/pem.html

Updates available from http://www.cwru.edu/dms/homes/pem/projects/parseCircOverrides.html

=cut
