#!/usr/bin/perl -w 
use warnings;

# Main program --------------------------------------------------------------------------------------
$verbose = 0;                           # Verbose output?
$age = 730;                             # Target age (in days)
$here = ".";                            # What directory to start with?
$rootpath = `/bin/pwd`;                 # Actual pathname of starting directory
chomp($rootpath);
$level = 0;                             # Traversal depth

traverse ($here);                       # Walk the tree...
exit 0;                                 # ...and we're done.

# Subroutines ---------------------------------------------------------------------------------------


# TRAVERSE: breadth-first traversal of directory tree
sub traverse {
   
   my $dirname = shift @_;              # Directory to traverse
   my @targets = ();                    # Web pages
   my @subdirs = ();                    # Directories within this one
   $level++;                            # Keep track of depth
   my $indent = "   " x $level;         # Indent report
   
   # Read the directory
   opendir (DIR, $dirname)              or die "PROBLEM: Unable to open directory $dirname ($!).\n";
   chdir ($dirname)                     or die "PROBLEM: Unable to change directory to $dirname.\n";
   my $pathname = `/bin/pwd`;
   chomp($pathname);
   my $shortpath = $pathname;
   $shortpath =~ s/$rootpath//o;
   if ($verbose) {
      print "$indent Level $level: Scanning directory $dirname ($pathname) ...\n";
	  }
	   
   my @entries = sort readdir(DIR);

   # Process each entry
   ENTRY: foreach $entry (@entries) {
 
      # Ignore symlinks and other special files 
      next ENTRY if (-l $entry);	  
      next ENTRY unless (-f $entry || -d $entry);

      # Capture subdirectories (ignore . and ..)
      if ((-d $entry) && ($entry !~ /^\.{1,2}$/)) {
         push @subdirs, $entry;
         next ENTRY;
         }

      # Capture web pages exceeding the specified age
      elsif (($entry =~ /^(.*)\.[sS]?[hH][tT][mM][lL]?$/) && (-M $entry <= $age)) { 
         push @targets, $entry; 
         next ENTRY;
         }
  
      # Ignore images, media, stylesheets, and so on
      else {
         next ENTRY;
         }

      }

   # Report on the files found at this level
   my $n_targets = scalar (@targets);   # Number of target pages found
   my $n_subdirs = scalar (@subdirs);   # Number of subdirectories at this level
   my @d_target = ();                   # Modification date structure
   my $d_target = "na";                 # Modification date
   if ($verbose) {
      print "$indent At this level: $n_targets *old* web pages; $n_subdirs subdirectories.\n";  
	  }

   foreach $target (@targets) { 
      @d_target = localtime ((stat $target)[9]);
	  $d_target = $d_target[3] . " " . ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$d_target[4]] . " " . ($d_target[5] + 1900);
	  if ($verbose) {
         print "   $indent $target : $d_target.\n"; 
		 }
	  else {
	     print "$shortpath/$target : $d_target.\n"; 
		 }
      }  
	  
   # Now traverse the subdirectories recursively
   my $s_targets = 0;                   # Number of targets below this level
   my $s_subdirs = 0;                   # Number of subdirectories below this level
   foreach $subdir (@subdirs) {      
      ($t, $d) = traverse ($subdir);  
	  $s_targets += $t;
	  $s_subdirs += $d;
      }
	  
   # Calculate the cumulative counts
   $n_targets += $s_targets;
   $n_subdirs += $s_subdirs;  
   if ($verbose) {   
      print "$indent Level $level: Total for $dirname and its $n_subdirs subdirectories: $n_targets *old* web pages.\n";
	  }

   # Restore original directory
   chdir ("..")                         or warn "PROBLEM: Unable to restore directory.\n";

   $level--;     # undent
   
   # Report counts to next higher level incarnation
   return ($n_targets, $n_subdirs);
   }
