package Guardian::Base;
use strict;
use warnings;

use Exporter qw(import);

our @EXPORT_OK = qw(GenerateMonitoredFiles);

#
## Function for fileposition initialization.
#
## This function is used to get the cursor position of the end of file (EOF) of
## a specified file.
#
## In order to prevent from permanently read and keep files opened, or dealing
## with huge logfiles, at initialization time of the worker processes, the file will
## be opened once and the cursor position of the end of file (EOF) get stored.
#
sub InitFileposition ($) {
	my $file = $_[0];

	# Open the file.
	open(FILE, $file) or die "Could not open $file. $!\n";

	# Just seek to the end of the file (EOF).
	seek(FILE, 0, 2);

	# Get and store the position.
	my $position = tell(FILE),

	# Close the file again.
	close(FILE);

	# Return the position.
	return $position;
}

#
## Function to generate a hash of monitored files and their file positions.
#
## This function is responsible for creating the hash of which files should be
## monitored by guardian. In order to do this, all options from the given hash of
## main settings will be parsed and all files to monitor extracted and stored into
## a temporary hash.
#
## Next step will be to check if the the file allready is part of the existing hash
## of monitored files and if true to use the stored value of the current cursor position.
## If this check fails, the responsible function to initialize the cursor position of
## EOF (end of file) will be called and stored.
#
sub GenerateMonitoredFiles (\%\%) {
	# Dereference the given hash-refs and store
	# them into a new temporary hashes.
	my %mainsettings = %{ $_[0] };
	my %current_monitored_files = %{ $_[1] };

	# Private hash for storing the new monitored files.
	my %new_monitored_files = ();

	# Loop through the temporary hash which contains the main settings
	# and search for files which should be monitored. Compare if the file
	# already was part of the Add them to the
	# private new hash of monitored files which will be returned.
        foreach my $config_option (keys %mainsettings) {
                # Skip option if it does not look like "Monitor_XYZ".
                next unless($config_option =~ m/^Monitor_/);

                # Get the configured file for this option.
                my $file = $mainsettings{$config_option};

                # Skip the file, if it does not exist or is not read-able.
                next unless(-r "$file");

                # Check if the file not yet has been added to the hash
                # of monitored files.
                unless(exists($current_monitored_files{$file})) {
                        # Add the file, init and store the fileposition.
                        $new_monitored_files{$file} = &InitFileposition($file);
                } else {
			# Add the file and grab the fileposition from the existing hash.
			$new_monitored_files{$file} = $current_monitored_files{$file};
		}
        }

	# Return the new_monitored_files hash.
	return %new_monitored_files;
}

1;
