#!/usr/bin/perl -w
# $Id: clean_tmp_area,v 1.10 2006/06/16 20:29:31 braup Exp $
# Script to be run as a cron job.  Deletes older files from the MapServer
# temporary area.
#
# Cron job for user "wwwrun" is currently set up as
#
#  */5 * * * * /home/braup/work/Projects/GLIMS/MapServer/bin/clean_tmp_area >> /home/braup/work/Projects/GLIMS/MapServer/clean_tmp_area.log 2>&1

use strict;

(my $progname = $0) =~ s{^.*/}{};  # get basename of program

use Getopt::Std;
use File::Path;
use Date::Calc qw( Today_and_Now );

my $version = '$Revision: 1.10 $';
($version) = $version =~ /^\$Revision:\s*(\d+\.\d*)/;

# set defaults
my $directory = "/srv/www/htdocs/tmp";
my $min_age_minutes = 15;  # minimum age to delete a file, in minutes

# delete files with these suffixes
my @suffixes    = qw( png
                      qy
                      shp shx dbf
                      tar zip tgz
                      gml xsd
                      gmt
                        );
my $suffix_list = join(" ", @suffixes);

my $usage = "$progname version $version
Usage:
  $progname  -h            (prints this help message and exits)
OR
  $progname  [-v] [-D] [-t age_minutes]
where
  -D    specifies debug (very verbose) mode
  -t    specifies the minimum age a file must be to be deleted, in minutes
        [$min_age_minutes]
  -v    specifies verbose mode

This program cleans out the old files from the MapServer temporary directory
($directory) where MapServer stages its images files.  With no options, the
script writes a one-line summary of what it did and when it did it.

The script deletes sub-directories, as well as files with the following
extensions:
$suffix_list

";

my %opt = ();
if (! getopts('hvDt:', \%opt) ) {
  die "$usage\n";
}

die "$usage\n" if $opt{h};

$opt{v} = 1 if $opt{D};

print "Running in DEBUG (very verbose) mode\n" if $opt{D};

# modify defaults if necessary
$min_age_minutes    = $opt{t} if defined($opt{t});
my $min_age_days    = $min_age_minutes / 1440; # 1440 minutes per day

my $now = sprintf( "%d-%02d-%02d %02d:%02d:%02d", Today_and_Now(my $gmt=0) );

if ($opt{v}) {
  print "\n$now\n";
  print "$progname version $version\n";
  print "Minimum age for files to be deleted:  $min_age_minutes minutes\n";
  print "Deleting files in directory $directory with suffixes:\n";
  print "  ", join( "  ", @suffixes ), "\n\n";
}

my $df_before = `df $directory`;

# Build list of files to delete
my @files = ();
foreach my $suffix (@suffixes) {
  push @files, <$directory/*.$suffix>;
}
# Add directories to list
foreach my $file (<$directory/*>) {
  push @files, $file if -d $file;
}

if ($opt{D}) {
  use Data::Dumper;
  print "Files to be deleted, if old enough:\n\n";
  print Dumper(@files);
  print "\n\n";
}

my $age_days;
my $total_files = @files;
my $goner_count = 0;
print "*** \$min_age_days = $min_age_days\n" if $opt{D};
foreach my $file (@files) {
  $age_days = -M $file;
  print "File $file has age $age_days days\n" if $opt{D};
  if ($age_days > $min_age_days) {
    print "Deleting $file with age ", $age_days*1440, " minutes\n" if $opt{D};
    if (-d $file) {
      print "Attempting to 'rmtree' $file\n" if $opt{D};
      rmtree($file);
    }
    else {
      print "Attempting to unlink $file\n" if $opt{D};
      unlink $file;
    }
    ++$goner_count;
  }
}

print "Deleted $goner_count file(s) out of $total_files total.\n\n" if $opt{v};

my $df_after = `df $directory`;

if ($opt{v}) {
  print "Disk usage before:\n$df_before\n";
  print "Disk usage after:\n$df_after\n";
}

# Print "normal" quiet output string
my ($df0, $df1);
($df0) = ($df_before =~ m/(\d+%)/s);
($df1) = ($df_after  =~ m/(\d+%)/s);
my $normal_output_string = "$now  $progname ver. $version";
$normal_output_string   .= " deleted $goner_count of $total_files files";
$normal_output_string   .= " from $directory.  Disk: $df0 --> $df1\n";
print $normal_output_string
  unless $opt{v};
