# linuxPingEpoch.pl
#
# by Dean Giles 
# Version 1.0 
# December 18, 2006
#
# This perl script pings an inactive Epoch to prevent it from being removed.
# If an epoch is inactive for more than the default interval (determinable by linuxGetInactiveEpochInterval)
#   then the epoch will be removed.  Using this script to ping the epoch will reset the Inactive clock and
#   begging the inactive interval again.
# This script has been designed to be run on Linux. 
# The API set can be downloaded from  http://developer.novell.com/wiki/index.php/Virtual_File_Services_for_NetWare. 
#
# The Perl script requires the name of an NSS volume as input.  It requires that NSS be installed and an NSS volume be present.
#

{
	# User help function
	if (@ARGV < 1 || $ARGV[0] eq "/?" || $ARGV[0] eq "-?") 
	{
		print "USAGE: perl linuxPingEpoch.pl VOLUME_NAME \n";
		print "Example: perl linuxPingEpoch.pl VOL1 \n";
		exit;
	}


	# Global Variables
	$volume = $ARGV[0];
	$file = "+</_admin/Manage_NSS/Volume/$volume/FileEvents.xml"; # This is the MSS management file we are accessing
	$reply;	   # Variable used to read from file.
	$command; # This is the NSS xml command that will be the NSS request to the file.
	$error;	  # This variable manages error codes and messages.
	$epoch;  # This variable keeps track of a user supplied epoch.
	$input;  # This variable is used to get user input.
	
	# Open the file and check that it exists.
	open(NSSFILE, $file) 
  		or die "Error opening NSS management file ($!) on server";
	print "file is now open";
	
	
	# ------------------------------------------------------------------------------
	# List the Event Epochs and choose one to use.
	$command = "<nssRequest><fileEventList><listEpochs/></fileEventList></nssRequest>"; 
	# Initializing the virtual file for sending a command.
	print "$file \n";
	print "$command \n"; 
		

	#Open the file and check that it exists.
	if (!syswrite(NSSFILE, $command, length($command))) #Initialize the nss management file and write a command.
		{
		
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
		sysread (NSSFILE, $error, 10000);  #Read the error message.
		
		print "Error writing initialization to management file. \n";
		print "$error  \n\n";	 #Print the error message to the screen.
		close (NSSFILE);
		}
	else
		{
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
	
		sysread (NSSFILE, $reply, 10000);  #Read the reply.
	
		print "$reply  \n\n";	 #Print the reply to the screen.

		print "Choose an epoch and type in the complete 37 letter code including dashes. \n";

		chomp ($epoch=<STDIN>);
		
  		}
	
# ------------------------------------------------------------------
# Use the epoch number to get the file event list.

	$command = "<nssRequest><fileEventList><pingEpoch epochNumber=\"$epoch\"/></fileEventList></nssRequest>"; 
	# Initializing the virtual file for sending a command.
	print "$file \n";
	print "$command \n"; 

	seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.

	#Open the file and check that it exists.
	if (!syswrite(NSSFILE, $command, length($command))) #Initialize the nss management file and write a command.
		{
		
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
		sysread (NSSFILE, $error, 10000);  #Read the error message.
		
		print "Error writing initialization to management file. \n";
		print "$error  \n\n";	 #Print the error message to the screen.
		close (NSSFILE);
		}
	else
		{
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
	
		sysread (NSSFILE, $reply, 10000);  #Read the reply.
	
		print "$reply  \n\n";	 #Print the reply to the screen.
		
		close (NSSFILE);
  		}
}		



