#!/usr/bin/perl
#
$MODULE_NAME = "EXAMPLE_PROG";
$MODULE_DESCRIPTION = "Example perl script with testing harness";
$MODULE_VERSION = "v0.0.1";
# 
# Abstract: 
#

#
# Modification History
#

#
# The modules that we need above and beyond perl-core.
#
use Fcntl;
use Getopt::Long;
use POSIX qw(strftime);

#
# In case of command line error, print the helpful usage information and exit
#
sub usage {
  print "\n";
  print "Usage: example.pl --inputfile=file \n";
  print "optional params   --logfile=file --verbose --debug\n";
  print "\n";
  exit;
}

#
# Grab a copy of the command line, so we can print it later.  This is a VMSish
# convention, and one that can stand to be repeated, often.
#
my $prog = $0;
my @args = @ARGV;

#
# Use the long form of Getopt so that the command line makes sense to non-Unix
# folks (and everyone else).  
#
GetOptions("inputfile=s"     => \$inputfile,
           "logfile=s"       => \$logfile,
           "debug"           => \$debug,
           "verbose"         => \$verbose );

#
# did we get a filespec for the input file?
#
if(!$inputfile) {
  print "No input file Supplied\n";
  print "An input file is required, quitting\n";
  usage();
}

#
# attempt to open the input file.  we are opening in read_only, full shared mode.  
#
open (INPUTFH, $inputfile) or die "can't open input file [$inputfile]";

#
# attempt to open the logfile as sole owner, noshare mode.  This will either 
# open a "real" text file, or sys$output.  either way is fine.
#
# did we get a logfile?  this is not required, and will default to sys$Output
#
if(!$logfile) {
  open (LOGFH,">&STDOUT") or die "Can't open STDOUT as the logfile, quitting\n";
} else {
  open (LOGFH,"> $logfile") or die "Can't open $logfile as the logfile, quitting\n";
}

#
# Print a banner line and the time
#
$now_string = strftime "%e-%b-%Y:%H:%M:%S", localtime;
print LOGFH "\n";
print LOGFH $MODULE_NAME,", ",$MODULE_DESCRIPTION,", ",$MODULE_VERSION,"\n";
print LOGFH "Example perl program began at $now_string\n";

#
# Set up the record and event counters, initialize any global variables
#
$tot_input_recs = 0;         # total records read from input file

#
# The main read loop.  Virtually every comparison or calculation from this point
# forward depends on the record variables set up in the configuration file.
#
$max_rec_length = 0;

while($line = <INPUTFH>) {

  # trim off the crlf or \n from the end of the record
  chomp($line);
  $tot_input_recs += 1;

  $buffer_size = length($line);
  if($buffer_size > $max_rec_length) {
    print LOGFH "max buffer size grew from $max_rec_length to $buffer_size at record number $tot_input_recs\n" if $verbose;
    $max_rec_length = $buffer_size;
  }
  
  print "read $buffer_size bytes from line number $tot_input_recs\n" if $debug;

# the real work would happen in here
  if(!(defined($debug))) {

  }
}

close(INPUTFH);

# 
# print the banner with the end time of the run.
#
$now_string = strftime "%e-%b-%Y:%H:%M:%S", localtime;
print LOGFH "Example perl program ended at $now_string\n";


#
# If the verbose command line flag was set, print out the statistics that were
# gathered during this run.
#
if( $verbose) {
  print LOGFH "\n";
  print LOGFH "$MODULE_NAME Run Statistics\n";
  print LOGFH "$tot_input_recs : records read from $inputfile\n";
  print LOGFH "$max_rec_length : maximum record length read\n";
  print LOGFH "\n";
}

# Most quality VMS programs will print the complete command line that invoked
# the program along with all the parameters and values.
#
print LOGFH "\nCommand line: $prog ";
foreach $i (0 .. $#args) {
  print LOGFH "$args[$i] ";
}
print LOGFH "\n";

close(LOGFH);

