#!/usr/bin/perl
#

$debug = 0;

$temp = "sum_pa.tmp";

$infile = "allbos_pa.dat";

# this will count all PAs only in period 1996 - 2008
#$sortfile = "allbos_pa.sort";
# this will count all PAs over 1976 - 2008
$sortfile = "allbos_pa_since76.dat";



open(INFILE, "$infile");
while (<INFILE>) {

  $line = $_;
  chomp($line);
  @words = split(/\s+/, $line);
  if ($#words != 5) {
    if ($debug > 0) {
      printf "skipping line ..%s.. \n", $line;
    }
    next;
  }

  $player = $words[3];
  if ($debug > 0) {
    printf " next player %s \n", $player;
  }

  $cmd = sprintf "grep %s %s | awk '{ print \$3 }' > $temp ", $player, $sortfile;
  $ret = exec_cmd($cmd);
  $sum = 0;
  open(TEMP, $temp);
  while (<TEMP>) {
    $templine = $_;
    chomp($templine);
    @tempwords = split(/\s+/, $templine);
    $this_pa = $tempwords[0];
    $sum += $this_pa;
    if ($debug > 0) {
      printf " line ..%s.. added %5d so now sum %5d \n", $templine, $this_pa, $sum;
    }
  }
  $total_pa = $sum;

  @words = split(/_/, $player);
  $good_name = sprintf "%s %s", $words[1], $words[0]; 
  
  printf " %20s ,  %20s , %5d \n", $player, $good_name, $total_pa;


}

exit 0;



##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


