#!/usr/bin/env perl 
#
# Counts symbols in the CDF Production Executable
# and tries to assign them to code packages that we can change,
# for example in the CDF code itself or in the ZOOM code or in the ROOT code
#
# Run this script via commands similar to:
#
#   > source ~cdfsoft/cdf2.cshrc
#   > setup cdfsoft2
#   > count_symbols.csh
#
# Written by Eric Wicklund
# August 24th, 2001
# Updated September 10th, 2002 
#
# Based on a python script from Bill Ashmanskas
#
#use diagnostics;
#
#  Read from standard input 
#  A typical input line must look like this:
#  /cdf/code/cdfsoft/dist/packages/CalibBanks/3.18.0/something.cc:221
#  i.e. with a fully qualified path name
#  
#
$total = 0;
while (<>) {
  chomp;
  $package = "unknown";
# print "$_\n";
#
# Ignore empty lines
#
  if ($_ ne "") {
#
# Parse the path name.  Ignore short paths.
#
     @dirs = split('/');
     $n = @dirs + 0;
#    printf "n elements $n\n";
     if ( $n >= 6) {
#
# There are many calibration packages, combine them for summary
#
       if ((index($_,"Calib") != -1)  or 
            ( (index($_,"DB") != -1 ) and (index($_,"DataFileDB") == -1) )) {
          $package = "*Calib*" }
       else {
#
# Examples in this section are
# /tmp/build-gtools-v2_3/share/bison.simple:204
# /usr/products/root/v2_26_01aKCC_4_0/Linux+2.2/include/TMath.h:345
#      
         if ( $dirs[1] eq "tmp" ) { $package = $dirs[2] }
         else {
          if ( $dirs[2] eq "products" ) { $package = $dirs[3] }
          else {
#
# Get ourselves located in cdf path name
# Examples in this section are
# /home/cdfsoft/dist/packages/TrackingKal/V00-00-23/src/KalTrack.cc:389
# /cdf/code/cdfsoft/dist/packages/TrackingObjects/development/src/COTQ.cc:467
#
#         print "$dirs[1]\n";
#         printf "n elements $n\n";
          if ( $dirs[1] eq "home" ) { $c = 0 };
          if ( $dirs[1] eq "cdf" )  { $c = 1 };
#
          if ( $dirs[$c+3] eq "products" ) { $package = $dirs[$c+4] }
          else { 
           if ( $dirs[$c+4] eq "packages" ) { $package = $dirs[$c+5] }
           else { 
            if ( $dirs[$c+6] eq "include" ) { $package = $dirs[$c+7] }
            else {
	     if ( $dirs[$c+6] eq "tmp" ) { $package = $dirs[$c+8] }
             else {
	      if ( $dirs[$c+6] eq "SpecialFunctions" ) { $package = $dirs[$c+7] }
	      }
	     }
            }
           }
          }
         }
      }
     };
#
#    print "Package $package\n";     
#    if ( $package eq "unknown") { print "did not parse $_\n" };
#
# Accumulate for summary
#
     if ( ($package ne "unknown") and 
          ($package ne "kai") and 
          ($package ne "gcc") ) { 
       $total += 1;
       $counts{$package} += 1 }
     }   
  }
#
# Print out summary
#
foreach $p (sort(keys(%counts))) {
   printf "%7d %-18s\n",$counts{$p},$p
}
printf "%7d Total",$total

