#!/usr/local/bin/perl -w
use strict;
use File::Basename;
use POSIX;
use Data::Dumper;
#########
# constants
use constant true => 1;
use constant false => 0;
#########
#########
# Globals
our ($configDir, $configFile,
$ruleStart, $ruleEnd, %ruleSets,
%operators, @skippedRules, $DEBUG,
%newOperators);
$DEBUG = 0;
#
#########
my ($home, $progName, $cfgExtension);
$home = $ENV{HOME};
$progName = strip_extension( basename($0) );
$configDir = "$home/scripts/file_scrub";
$cfgExtension = ".cfg";
$configFile = $progName.$cfgExtension;
$ruleStart = "";
$ruleEnd = "";
%operators = map { $_ => '1'} qw( directory file age action );
$newOperators{'directory'} =
sub { my @dirs = map { (< $_ >) } @_;
print "No directory defined\n" if (not @dirs);
};
$newOperators{'file'} =
sub { my @files = map { (< $_ >) } @_;
print "No files de (Bfined\n" if (not @files);
};
$newOperators{'age'} =
sub { my $age = shift;
print "Invalid file age\n" if (not $age);
};
$newOperators{'action'} =
sub { my $action = shift;
print "Invalid action\n" if (not $action);
};
load_configs();
check_config();
print Dumper(\%newOperators);
$newOperators{'action'}('');
print Dumper(\%ruleSets);
######
# Subs
sub strip_extension{
$_[0] =~ s/\..*$//g;
return $_[0];
}
sub load_configs{
my ($newRule, $count1);
$newRule = false;
$count1 = 0;
open(CFG,$configFile) or die
"Cannot open config file $configFile. $!\n";
while (){
chomp;
s/\#.*$//g;
s/ //g;
next if /^\#/;
next if /^$/;
if (/$ruleStart/i){
$newRule = true;
next;
} elsif (/$ruleEnd/i){
$newRule = false;
$coun (Bt1++;
next;
}
print "loading $_\n" if ($DEBUG);
map {my ($key, $val) = split("=",$_);
$ruleSets{$count1}{$key} = $val} $_ if ($newRule);
}
close(CFG);
}
sub check_config{
my $numRules = scalar(keys(%ruleSets));
for (keys(%ruleSets)){
my $ruleNum = $_;
my $skip = 0;
###################################
# The following for loop checks
# the syntax of the operators.
# If an invalid operator is found,
# the entire rule is not executed.
for (keys(%{$ruleSets{$ruleNum}})){
#if ( ($_ !~ /\b$operators\b/) and !($skip)){
if ( (not exists($operators{$_})) and !($skip)){
print "ERROR: Invalid operator, $_\n";
print " Skipping Rule $ruleNum\n";
push(@skippedRules,$ruleNum);
$skip = 1;
}
}
} # end of syntax checking block
###################################
# The next line r (Bemoves bad rules
# from the hash of rules.
for (@skippedRules) {delete $ruleSets{$_}} ;
###################################
# Check each individual rule to
# be sure it contains valid entries
for (keys(%ruleSets)){
my $currentRule = $_;
my ($directory,$file,$age,$action);
#################
# Check each rule
print "Now checking rule $currentRule / directory\n" if ($DEBUG);
my @dirs = (< $ruleSets{$currentRule}{directory} >);
if (not @dirs){
&print_check_error(\%{$ruleSets{$currentRule}},"directory",$currentRule);
}
else {
for (@dirs){
#if ( ! -d $ruleSets{$currentRule}{directory} ){
if ( ! -d $_ ){
&print_check_error(\%{$ruleSets{$currentRule}},"directory",$currentRule);
next;
}
}
}
print "Now checking rule $currentRule / file\n" if ($DEBUG);
{
(B my $filePath = $ruleSets{$currentRule}{directory}."/".
$ruleSets{$currentRule}{file};
my @files = (< $filePath >);
if (not @files) {
&print_check_error(\%{$ruleSets{$currentRule}},"file",$currentRule);
}
else {
for (@files){
if (! -f $_ ){
&print_check_error(\%{$ruleSets{$currentRule}},"file",$currentRule);
next;
}
}
}
}
print "Now checking rule $currentRule / age\n" if ($DEBUG);
if ( $ruleSets{$currentRule}{age} =~ /\d/ ) {
if ( $ruleSets{$currentRule}{age} <= 0 ) {
&print_check_error(\%{$ruleSets{$currentRule}},"age",$currentRule);
next;
}
}
print "Now checking rule $currentRule / action\n" if ($DEBUG);
my $actions = "(compress|remove|rotate)";
if ( $ruleSets{$currentRule}{action} (B !~ /\b$actions\b/ ){
#if ( $ruleSets{$currentRule}{action} =~ /[^(compress|remove|rotate)]/ ){
&print_check_error(\%{$ruleSets{$currentRule}},"action",$currentRule);
next;
}
} # end of rule checking block
###################################
# The next line removes bad rules
# from the hash of rules.
for (@skippedRules) {delete $ruleSets{$_}} ;
print "\nProcessing ". scalar(keys(%ruleSets)) .
" of " . $numRules . " configured rules.\n";
}
sub print_check_error{
my $href = shift;
my $operator = shift;
my $ruleNum = shift;
print "\n\t1. $operator\n\t2. $ruleNum\n\n";
print "\t$$href{$operator} is not a valid $operator\n";
print "\tSkipping rule $ruleNum\n";
push (@skippedRules, $ruleNum);
}