--- PrintAnalyzer	2004-10-12 20:45:49.000000000 -0400
+++ bin/PrintAnalyzer	2004-10-12 20:43:59.000000000 -0400
@@ -4,6 +4,11 @@
 #    Generate some stats from cups page_log file
 #    copyright            : (C) 1999 - 2003 by Thies Moeller                         
 #    email                : moeller@tu-harburg.de 
+#
+#    ChangeLog
+#       Max Baker <max@warped.org> 10/12/04
+#           + Added name lookup from /etc/passwd and option w
+#           + Fixed sort by date 
 #***************************************************************************
 
 #***************************************************************************
@@ -28,13 +33,12 @@
 my %opt;
 my $PAGE_LOG_FILE;
 
-getopt("f:q",\%opt);
+getopt("f:qw",\%opt);
 
 if (exists $opt{"f"}) { $PAGE_LOG_FILE = $opt{f};}
 else {$PAGE_LOG_FILE = "/var/log/cups/page_log";}
 
 ##############
-# edit start and end of normal work time
 # activity outside this interval will be marked with an "!" 
 # to disable set WorkStart to 0 and WorkEnd to 24
 ##############
@@ -60,18 +64,38 @@
 my %queueUserStats  = ();
 my $totalReq      = 0;
 my $totalPages    = 0;
+my %userLookup    = ();
 
-sub DateCompare
+sub UserLookup
 {
-	    my $date1 = substr($a, 6, 2) * 1024;  # Years
-            my $date2 = substr($b, 6, 2) * 1024;
-
-	    $date1 += substr($a,3,2) * 64;       # Months
-	    $date2 += substr($b,3,2) * 64;
+    return '' if exists $opt{w};
+    my $user = shift;
+    return $userLookup{$user} if exists $userLookup{$user};
+
+    my $name = '';
+    open (PASSWD,"< /etc/passwd") or return undef;
+    while(<PASSWD>)
+    {
+        next unless $_ =~ /^$user:/;
+        $name = (split(/:/,$_))[4];
+    } 
+    close(PASSWD);
+    $userLookup{$user} = $name;
+    return $name;
+}
 
-	    $date1 += substr($a, 0, 2);          # Days
-	    $date2 += substr($b, 0, 2);
-            return ($date1 <=> $date2);
+sub DateCompare
+{
+	    my $year1  = substr($a, 6, 2);
+        my $year2  = substr($b, 6, 2);
+	    my $month1 = substr($a, 3, 2);
+	    my $month2 = substr($b, 3, 2);
+	    my $day1   = substr($a, 0, 2);
+	    my $day2   = substr($b, 0, 2);
+    
+        return $year2  <=> $year1  if $year1  != $year2;
+        return $month2 <=> $month1 if $month1 != $month2;
+        return $day2   <=> $day1;
 }
 
 
@@ -150,17 +174,18 @@
 {
 	my $userReq;
 	my $pageperjob;
+    my $userName;
 
 ############# Output Form ################
 format USERLOG_TOP =
 PrinterAccounting
-Username            Requests      Pages   Pages/Request
---------------------------------------------------------
+Username                                        Requests      Pages   Pages/Request
+-----------------------------------------------------------------------------------
 .
 
 format USERLOG =
-@<<<<<<<<<<<<<<<<<< @>>>>>>>   @>>>>>>>   @>>>>>>>>
-$userReq,           $userRequests{$userReq}, $userPages{$userReq}, $pageperjob
+@>>>>>>>>>>>>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>   @>>>>>>>   @>>>>>>>>
+$userReq, $userName, $userRequests{$userReq}, $userPages{$userReq}, $pageperjob
 .
 ############# Output Form ################
 
@@ -169,6 +194,7 @@
         $^="USERLOG_TOP";
         foreach $userReq (sort { $userPages{$b} <=> $userPages{$a}} keys %userRequests)
             {
+            $userName = UserLookup($userReq);
             $pageperjob = sprintf("%5d", POSIX::ceil($userPages{$userReq} / $userRequests{$userReq}));
             write ;
             }
@@ -359,22 +385,23 @@
 	my $billing;
 	my $pageperbilling ;
 	my $billinguser;
+    my $billingusername;
 
 ############# Output Form ################
 format BILLINGLOG_TOP =
 Billing Heuristic
-Billing                           Pages
---------------------------------------------------------------
+Billing                                                       Pages
+-------------------------------------------------------------------------
 .
 
 format BILLINGLOG =
-@<<<<<<<<<<<<<<<<<<<<<<<<        @>>>>>>>>
+@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<        @>>>>>>>>
 $billing,$pageperbilling
 .
 
 format BILLINGUSERLOG =
-|- @>>>>>>>>>>>>>>>>>>>>>        @>>>>>>>>
-$billinguser,$pageperbilling
+|- @>>>>>>>>>>>>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<        @>>>>>>>>
+$billinguser, $billingusername,  $pageperbilling
 .
 ############# Output Form ################
 
@@ -390,6 +417,7 @@
         $~="BILLINGUSERLOG";
         foreach $billinguser ( sort {$billingStats{$billing}{"user"}{$b} <=> $billingStats{$billing}{"user"}{$a}} keys %{$billingStats{$billing}{"user"}})
 	{
+      $billingusername = UserLookup($billinguser);
 	  $pageperbilling = $billingStats{$billing}{"user"}{$billinguser};
           write;		
 	}
@@ -402,12 +430,13 @@
 	my $queue;
 	my $pageperqueue;
 	my $queueuser;
+    my $queueusername;
 
 ############# Output Form ################
 format QUEUEUSER_TOP =
 Queue-User
-Queue                            Pages
---------------------------------------------------------------
+Queue                                                    Pages
+-----------------------------------------------------------------
 .
 
 format QUEUENAME =
@@ -416,8 +445,8 @@
 .
 
 format QUEUEUSERLOG =
-|- @>>>>>>>>>>>>>>>>>>>>>        @>>>>>>>>
-$queueuser,$pageperqueue
+|- @>>>>>>>>>>>>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<    @>>>>>>>>
+$queueuser,$queueusername,$pageperqueue
 .
 ############# Output Form ################
 
@@ -432,6 +461,7 @@
         $~="QUEUEUSERLOG";
         foreach $queueuser ( sort {$queueUserStats{$queue}{$b} <=> $queueUserStats{$queue}{$a}} keys %{$queueUserStats{$queue}})
 	{
+      $queueusername = UserLookup($queueuser);
 	  $pageperqueue = $queueUserStats{$queue}{$queueuser};
           write;		
 	}
@@ -594,6 +624,10 @@
 
 Quiet operation
 
+=item B<-w>
+
+Don't Resolve login names to real names in /etc/passwd
+
 =back
 
 =head1 SEE ALSO
@@ -605,9 +639,3 @@
  Thies Moeller
 
 =cut
-
-
-
-
-
-
