#!/usr/bin/perl
#
#      checklog [-noprint | -nowrap] [DIRECTORY]
#
#  Check for existence of log file in DIRECTORY (default = .) with ESTIMATE
#  and TOTAL lines; and print contents (unless -noprint) in 80-character lines
#  (unless -nowrap).

use open qw/:std :utf8/;
use Text::Unidecode;

if (@ARGV > 0 && $ARGV[0] eq "-noprint") {      # Process command-line arguments
   $noprint = shift @ARGV;
} elsif (@ARGV > 0 && $ARGV[0] eq "-nowrap") {
   $nowrap = shift @ARGV;
}

$LOGS = "*log*";                                # Name(s) of log files
$LOGS = (shift @ARGV) . "/$LOGS"
   if (@ARGV == 1);
$CORRECTED = "LOG.CORRECTED";                   # Name of corrected log file

(@ARGV == 0)
   || die ("Usage: checklog [-noprint | -nowrap] [DIRECTORY]\n");

$UNCR = "/usr/bin/tr '\\r' ' '";                # Replace \r by ' '
$GREPLOGS = "/bin/cat $LOGS | $UNCR | "         # grep logs, replacing \r by ' '
	  . "/bin/grep -a";

$| = 1;                                         # Force immediate write
print "\n***** Checking log file *****\n";

###############################################################################

@exist = `/bin/ls $LOGS 2> /dev/null`;          # Get name(s) of log file(s),
if (@exist == 0) {
   print "checklog: no log file\n";
   exit unless (-e $CORRECTED);

} elsif (@exist > 1) {
   print "checklog: multiple log files\n";
   system "/bin/ls -l $LOGS 2> /dev/null";      # List names if multiple logs
}

###############################################################################

@estimate = `$GREPLOGS ESTIMATE`;               # Get line(s) with ESTIMATE
if (@estimate == 0) {
   print "checklog: no estimate of time spent\n";

} elsif (@estimate == 1) {
   $line = $estimate[0];
   unless ($line =~ m{(\d+):(\d+)}              # HH:MM
	|| $line =~ m{(\d+)\s*h.*?(\d+)\s*m}    # HH h..., MM m...
	|| $line =~ m{(\d+\.\d*|\.\d+)}         # real number
	|| $line =~ m{(^|[^.\w\d])(\d\d\d\d)}   # HHMM
	|| $line =~ m{(^|[^.\w\d])(\d\d)}       # HH
	|| $line =~ m{(^|[^.\w\d])(\d)}) {      # H
      print "checklog: no estimate of time spent\n";
      @estimate = ();
   }

} elsif (@estimate > 1) {
   print "checklog: multiple estimates of time spent\n";
   foreach $line (@estimate) {
      chomp ($line);
      print "> $line\n";                        # List lines if more than one
   }
}

if (@estimate != 1 && -e $CORRECTED) {
   @estimate = `/bin/grep ESTIMATE $CORRECTED`;
   print "checklog: estimate from corrected log file\n"
      if (@estimate > 0);
}

###############################################################################

@total = `$GREPLOGS "TOTAL"`;                   # Get line(s) with "TOTAL"
if (@total == 0) {
   print "checklog: no total of time spent\n";

} elsif (@total == 1) {
   $line = $total[0];
   unless ($line =~ m{(\d+):(\d+)}              # HH:MM
	|| $line =~ m{(\d+)\s*h.*?(\d+)\s*m}    # HH h..., MM m...
	|| $line =~ m{(\d+\.\d*|\.\d+)}         # real number
	|| $line =~ m{(^|[^.\w\d])(\d\d\d\d)}   # HHMM
	|| $line =~ m{(^|[^.\w\d])(\d\d)}       # HH
	|| $line =~ m{(^|[^.\w\d])(\d)}) {      # H
      print "checklog: no total of time spent\n";
      @total = ();
   }

} elsif (@total > 1) {
   print "checklog: multiple totals of time spent\n";
   foreach $line (@total) {
      chomp ($line);
      print "> $line\n";                        # List lines if more than one
   }
}

if (@total != 1 && -e $CORRECTED) {
   @total = `/bin/grep TOTAL $CORRECTED`;
   print "checklog: total from corrected log file\n"
      if (@total > 0);
}

###############################################################################

if (@estimate == 1) {                           # Print ESTIMATE & TOTAL
   $estimate[0] =~ s{^\s*}{};
   $estimate[0] =~ s{\s*$}{};
   print "Estimate: $estimate[0]\n";
}

if (@total == 1) {
   $total[0] =~ s{^\s*}{};
   $total[0] =~ s{\s*$}{};
   print "Total: $total[0]\n";
}

###############################################################################

exit                                            # Suppress printing log files?
   if ($noprint);

chomp (@exist);                                 # Print log file(s)
foreach $file (@exist) {
   open (LOG, "$UNCR < $file |");
   $skip = 0;
   print "\n==> $file <==\n";
   while (<LOG>) {
      $_ = unidecode ($_);
      s{\s*$}{};                                # Remove trailing whitespace
      next if ($skip && m{^$});                 # Suppress multiple blank lines
      $skip = m{^$};
      $rest = $_;
      while (!$nowrap && length($rest) > 80) {
	 $rest =~ s{^(.{1,80})\s+|^(.{80})}{};
	 ($line = $1.$2) =~ s{\s*$}{};
	 last if ($rest eq "");
	 print "$line\n";
      }
      print "$rest\n";
      $rest = "";
      $line = "";
   }
   close (LOG);
}


      
