#! /usr/bin/perl
#
#  Purpose:
#  FlowViewer_CleanFiles is a utility for cleaning out temporary files
#  that have been left over from debugging (e.g. $debug_files = 'Y'). 
#  All files older than $remove_files_time will be removed from the 
#  $working_directory. This can be run from crontab.
#
#  Description:
#
#  Input arguments (received from the form):
#  Name                 Description
#  -----------------------------------------------------------------------
#  none 
#
#  Modification history:
#  Author       Date            Vers.   Description
#  -----------------------------------------------------------------------
#  J. Loiacono  04/01/2007      3.2     Original released version
#  J. Loiacono  12/07/2007      3.3     Minor fixes
#
#$Author$
#$Date$
#$Header$
#
###########################################################################
#
#               BEGIN EXECUTABLE STATEMENTS
#
 
use FlowViewer_Configuration;
use File::stat;

$current_time = time;

# Clean out the Work directory according to 'remove_workfiles_time'

while ($next_file = <$work_directory/*>) { 
       
	$last_access = stat($next_file)->mtime;
        ($directory,$filename) = $next_file =~ m/(.*\/)(.*)$/; 
	$file_age = $current_time - $last_access;

	if ($file_age > $remove_workfiles_time) {
		print "From $work_directory, deleting $next_file\n";
		unlink ($next_file);
	}
}

# Clean out the Report directory according to 'remove_reportfiles_time'

while ($next_file = <$reports_directory/*>) { 
       
        ($directory,$filename) = $next_file =~ m/(.*\/)(.*)$/;

	if ($filename eq "FlowViewer_Save.png") { print "skipping $next_file\n"; next; }
	if ($filename eq "FlowViewer.png")      { print "skipping $next_file\n"; next; }
	if ($filename eq "$user_logo")          { print "skipping $user_logo\n"; next; }

	$last_access = stat($next_file)->mtime;
        ($directory,$filename) = $next_file =~ m/(.*\/)(.*)$/; 
	$file_age = $current_time - $last_access;

	if ($file_age > $remove_reportfiles_time) {
		print "From $reports_directory, deleting $next_file\n";
		unlink ($next_file);
	}
}

# Clean out the Graph directory according to 'remove_workfiles_time'

while ($next_file = <$graphs_directory/*>) { 
       
        ($directory,$filename) = $next_file =~ m/(.*\/)(.*)$/;

	if ($filename eq "FlowGrapher_Save.png") { print "skipping $next_file\n"; next; }
	if ($filename eq "FlowGrapher.png")      { print "skipping $next_file\n"; next; }

	$last_access = stat($next_file)->mtime;
        ($directory,$filename) = $next_file =~ m/(.*\/)(.*)$/; 
	$file_age = $current_time - $last_access;

	if ($file_age > $remove_graphfiles_time) {
		print "From $graphs_directory, deleting $next_file\n";
		unlink ($next_file);
	}
}
