#!/usr/bin/perl
# =========================================================================
# FILE: countLines.pl                                           10-29-2018
#
# SERVICES: D&B Model Railroad Utility Program 
#
# DESCRIPTION:
#   This program counts the number of program and comment lines.
#
#   This program is written for perl on Raspberry Pi 3.
#
# PERL VERSION: 5.24.1
#
#      (c) Copyright (c) 2018  Don Buczynski.  All Rights Reserved.
# =========================================================================
BEGIN {
   use Cwd;
   ($ExecutableName) = ($0 =~ /([^\/\\]*)$/);
   if (length($ExecutableName) == length($0)) {
      $WorkingDir = cwd();
   }
   else {
      $WorkingDir = substr($0, 0, rindex($0, "/"));
   }
   unshift (@INC, $WorkingDir);
   $libDir = join("/../", $WorkingDir, "lib");
   unshift (@INC, $libDir) if (-e $libDir);

   srand;      # Initialize random number seed.
}

use DnB_Message;

@fileList = ('DnB.pl','DnB_GradeCrossing.pm','DnB_Mainline.pm','DnB_Message.pm',
             'DnB_Sensor.pm','DnB_Signal.pm','DnB_Turnout.pm','DnB_Yard.pm');

$codeTotal = 0;
$testTotal = 0;
$commentTotal = 0;
$testFlag = 0;
print "\nDnB model railroad perl code summary.\n";

foreach $file (@fileList) {
   if (&ReadFile($file, \@array, 'NoTrim')) {
      $result = &DisplayError("Error reading file: $file");
   }
   else {
      print "Processing $file ...\n";
      $code = 0;
      $test = 0;
      $comment = 0;
      foreach $line (@array) {
         chomp($line);
         next if ($line =~ m/^\s*$/);
         if ($line =~ m/^\s*#/) {
            $comment++;
            next;
         }
         if ($line =~ m/^\}/ and $testFlag == 1) {
            $test++;
            $testFlag = 0;
            next;
         }
         if ($line =~ m/^sub Test.+ \{/) {
            $test++;
            $testFlag = 1;
            next;
         }
         if ($testFlag == 1) {
            $test++;
         }
         else {
            $code++;
         }
      }
      print "   $comment comment lines.\n";
      $commentTotal += $comment;
      print "   $code code lines.\n";
      $codeTotal += $code;
      print "   $test test lines.\n";
      $testTotal += $test;
   }
}
$total = $codeTotal + $commentTotal + $testTotal;
print "\nTotal comment lines: $commentTotal  " . sprintf("%2d.1%%", ($commentTotal/$total)*100) . "\n";
print "Total code lines: $codeTotal  " . sprintf("%2d.1%%", ($codeTotal/$total)*100) . "\n";
print "Total test lines: $testTotal  " . sprintf("%2d.1%%", ($testTotal/$total)*100) . "\n\n";

# Compare to old Basic Stamp code.
$dir = "/home/pi/perl/BasicStampCode";
if (-d $dir) {
   @bspFileList = ('BlockSignal.bsp','GradeCrossingV2.bs2','ReverseLoop.bs2',
                   'TurntableV2.bs2','YardRoute.bs2');

   $codeTotal = 0;
   $testTotal = 0;
   $commentTotal = 0;
   $testFlag = 0;
   print "DnB Basic Stamp code summary compare.\n";

   foreach $file (@bspFileList) {
      if (&ReadFile("$dir/$file", \@array, 'NoTrim')) {
         $result = &DisplayError("Error reading file: $file");
      }
      else {
         print "Processing $file ...\n";
         $code = 0;
         $test = 0;
         $comment = 0;
         foreach $line (@array) {
            next if ($line =~ m/^\s*$/);
            if ($line =~ m/^\s*'/) {
               $comment++;
               $testFlag = 0;
               next;
            }
            if ($line =~ m/^TestLoop:/) {
               $test++;
               $testFlag = 1;
               next;
            }
            if ($testFlag == 1) {
               $test++;
            }
            else {
               $code++;
            }
         }
         print "   $comment comment lines.\n";
         $commentTotal += $comment;
         print "   $code code lines.\n";
         $codeTotal += $code;
         print "   $test test lines.\n";
         $testTotal += $test;
      }
   }
   $total = $codeTotal + $commentTotal + $testTotal;
   print "\nTotal comment lines: $commentTotal  " . sprintf("%2d.1%%", ($commentTotal/$total)*100) . "\n";
   print "Total code lines: $codeTotal  " . sprintf("%2d.1%%", ($codeTotal/$total)*100) . "\n";
   print "Total test lines: $testTotal  " . sprintf("%2d.1%%", ($testTotal/$total)*100) . "\n\n";
}
exit(0);