# NWSetFileMAttrInfo.pl
#
# by Dean Giles 
# Version 1.0 
# January 4, 2007
#
# This Perl script sets or clears the metadata archive bit on a file.
# This script has been designed to be run on Netware. 
# The API set can be downloaded from  http://developer.novell.com/wiki/index.php/Virtual_File_Services_for_NetWare. 
# 
# It requires that NSS be installed and an NSS pool be present.
#

{
	# User help function
	if (@ARGV < 2 || $ARGV[0] eq "/?" || $ARGV[0] eq "-?") 
	{
		print "USAGE: perl NWSetMetaDataArchivBit.pl FILE_NAME  yes/no \n";
		print "Where FILE_NAME is the name of the file, ".
			"it must include the complete path including the leading slash.\n".
			"And with yes to set the bit or No to clear the bit.\n\n";

		print "Example: perl NWSetMetaDataArchivBit.pl VOL1:file.txt yes \n";
		exit;
	}

	# Global Variables
	$fileName = $ARGV[0];	 # Name of the file to set the attribute on.
	$enable = $ARGV[1];    # Yes or No -- yes to set the Metadata archive bit, or no to reset it.
	$file = "+<_ADMIN:Manage_NSS/files.cmd"; # 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;  # Variable used to manage errors.
	
	# 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 \n";
	$command = "<virtualIO><datastream name=\"command\"/></virtualIO>".
		"<fileRequest><fileInfo><setFileInfo>".
		"<fileName>$fileName</fileName>".
		"<attributes><attrArchive enabled=\"$enable\"/></attributes>".
		"</setFileInfo></fileInfo></fileRequest>";
	# This is the command for initializing the virtual file for sending a command.
	print "File Name: $file \n";
	print "Request Sent: $command \n"; 

	
	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);
  	}
}		


