#!/usr/bin/env perl

#----------------------------------------------------------------------------
# NAME
#       NAfixperms.pl - goes through Network Appliance snapshots, and resets
#	permissions on files within specified directories
#
# SYNOPSIS
#       NAfixperms.pl
#
# FEEDBACK
#       The increasing functionality and usefullness of this script relies on
#       YOU, the user.  If this script does not behave in the intended way, or
#       if there is a lacking feature, please provide feedback to the author
#       of this script so that your feedback can be looked into and possibly
#       integrated into this script.
#
# John C. Koen 5/May/2005
# johnk@southwestern.edu
# $Id: NAfixperms.pl,v 1.8 2005/05/06 14:01:20 root Exp $
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Begin of POD
#----------------------------------------------------------------------------

=head1 NAME

NAfixperms.pl - goes through Network Appliance snapshots, and resets
permissions on files within specified directories

=head1 SCRIPT CATEGORIES

UNIX/System_administration

=head1 SYNOPSIS

NAfixperms.pl

=head1 PREREQUISITES

This script requires that it be given directories that live on a Network
Appliance.

=head1 COPYRIGHT

Copyright (c) 2004 John C. Koen <johnk@southwestern.edu>. All rights reserved.
This program is free software.  You may modify and/or distribute it
under the same terms as Perl itself.  This copyright notice
must remain attached to the file.

=head1 REVISION

$Id: NAfixperms.pl,v 1.8 2005/05/06 14:01:20 root Exp $

=head1 FEEDBACK

The increasing functionality and usefullness of this script relies on
YOU, the user.  If this script does not behave in the intended way, or
if there is a lacking feature, please provide feedback to the author
of this script so that your feedback can be looked into and possibly
integrated into this script.

=head1 README

NAfixperms.pl - goes through B<Network Appliance> snapshots, and resets
permissions on files within specified directories

=head1 AUTHOR

John C. Koen
johnk@southwestern.edu
http://www.southwestern.edu/~johnk

=cut
#----------------------------------------------------------------------------
# End of POD
#----------------------------------------------------------------------------

#
require 5;
use POSIX;
use warnings; 			# Provide helpful warnings
use strict;			# Keep script in check
use File::Find;			# Perl module which makes finding files easy
#

#----------------------------------------------------------------------------
# CONFIGURATION - adjust these variables to fit your system
#----------------------------------------------------------------------------
my $debug = 0;			# Debug flag

# Provide directories that you wish to fix perms in, inside of this txt
my $directories_to_fix = "/tmp/dirs_to_fix_fileperms.txt";

# Which snapshot should we grab permissions from
my $snapshot_time = "nightly.1";
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Begin main logic
#----------------------------------------------------------------------------

	my @directory;

# Create directory listing, after reading in directories from txt file
open (DIRECTORIES, $directories_to_fix) or die "Error using $directories_to_fix: $!\n";
while (my $directory = <DIRECTORIES>) {
	chomp($directory);	# Remove any control code from end

	# Create snapshot directory, from read-in directories
	push (@directory, "$directory/.snapshot/$snapshot_time/");
}

# Do the find, and execute &fix_perms on each find result
# This will do a file search through each directory element of @directory
find (\&fix_perms, @directory) or die "\nCAN NOT FIND .SNAPSHOT FOR DIRECTORY!";

#----------------------------------------------------------------------------
# End main logic
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
#       sub fix_perms
#----------------------------------------------------------------------------
sub fix_perms {

	my $true_filename;

	# For every filename found in directory element of @directory,
	# cut off our snapshot info (used 
	my @snapshot_filename = split "\.snapshot/$snapshot_time/", $File::Find::name;
	if (!defined($snapshot_filename[1])) { 
		$true_filename = $snapshot_filename[0];
	} else { $true_filename = $snapshot_filename[0].$snapshot_filename[1];}

	# Debug information (displayed if $debug is turned on)
	if ($debug) {
		print "SNAPSHOT FILE: $File::Find::name\n";
		print "REAL FILE: $true_filename\n";
	}

	# Grab file permissions
	my @snap_file_attributes = stat $File::Find::name;
	my $snap_file_mode = $snap_file_attributes[2];

	# Debug information (displayed if $debug is turned on)
	if ($debug) {
	print "PERMISSIONS: %o\n", $snap_file_mode;
	}

	# Fix the file permissions - chmod file to correct mode
	print "--\n";
	chmod ($snap_file_mode, $true_filename) and print "EXECUTING: chmod $snap_file_mode, $true_filename\n";

	# Fix the file permissions - chown file to correct owner
	chown $snap_file_attributes[4], $snap_file_attributes[5], $true_filename and print "EXECUTING: chown $snap_file_attributes[4], $snap_file_attributes[5] $true_filename\n";

	# Debug information (displayed if $debug is turned on)
	if ($debug) {
	system("ls -ld \"$true_filename\"");
	}
}
