#!/usr/bin/perl -w
#****************************************************************************
#
# Copyright (c) 2007 Novell, Inc. All Rights Reserved.
# THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
# NO PART OF THIS WORK MAY BE USED, PRACTICED, PERFORMED COPIED, DISTRIBUTED,
# REVISED, MODIFIED, TRANSLATED, ABRIDGED, CONDENSED, EXPANDED, COLLECTED,
# COMPILED, LINKED, RECAST, TRANSFORMED OR ADAPTED WITHOUT THE PRIOR WRITTEN
# CONSENT OF NOVELL, INC. ANY USE OR EXPLOITATION OF THIS WORK WITHOUT
# AUTHORIZATION COULD SUBJECT THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
#
#  Author:		Novell, Inc.
#  Workfile:	imgr_clean.pl
#  Contents:	Utility to cleanup the iManager plugins for NCS/BCC, NSS,
# 				and ARK
#
#****************************************************************************

package imgr_cleanup;

#**********************************************************************
#	Use Statements
#**********************************************************************
use strict;
use warnings;
use diagnostics;

use Getopt::Long;
use Fcntl ':mode';

#**********************************************************************
#	Function Definitions
#**********************************************************************
#**********************************************************************
# Name:			GetArkFiles
# Description:	Returns the list of ARK files
# Parameters:	None
# Return:		array	- Array of files
# Notes:
#**********************************************************************
sub GetArkFiles()
{
	my @files;

	# *** Please keep in alphabetical order for ease of maintenance. ***

	# You can include individual files, multiple files via wild cards, and
	# directories in this list.  Please note that if you include a directory
	# then all files in that directory will be removed.
	# For example:
	# Individual file:	/webapps/nps/WEB-INF/ncs.tld
	# Multiple files:	/webapps/nps/WEB-INF/lib/ncs*.jar
	# Directories:		/webapps/nps/portal/modules/ncs

	push(@files, "/webapps/nps/packages/arkmgmt.npm");						# Files
	push(@files, "/webapps/nps/portal/modules/ark");						# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_arkmgmt");			# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_arkmgmtjar");		# Directory
	push(@files, "/webapps/nps/WEB-INF/ark.tld");							# Files
	push(@files, "/webapps/nps/WEB-INF/lib/ark*.jar");						# Files
	push(@files, "/webapps/nps/WEB-INF/modules/ark");						# Directory
    push(@files, "/work/Standalone/localhost/nps/portal/modules/ark");		# Directory

	return(@files);

} # GetArkFiles

#**********************************************************************
# Name:			GetNcsFiles
# Description:	Returns the list of NCS/BCC files
# Parameters:	None
# Return:		array	- Array of files
# Notes:
#**********************************************************************
sub GetNcsFiles()
{
	my @files;

	# *** Please keep in alphabetical order for ease of maintenance. ***

	# You can include individual files, multiple files via wild cards, and
	# directories in this list.  Please note that if you include a directory
	# then all files in that directory will be removed.
	# For example:
	# Individual file:	/webapps/nps/WEB-INF/ncs.tld
	# Multiple files:	/webapps/nps/WEB-INF/lib/ncs*.jar
	# Directories:		/webapps/nps/portal/modules/ncs

	push(@files, "/webapps/nps/packages/ncsmgmt.npm");						# Files
	push(@files, "/webapps/nps/portal/modules/ncs");						# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_ncsmgmt");			# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_ncsmgmtjar");		# Directory
	push(@files, "/webapps/nps/WEB-INF/lib/ncs*.jar");						# Files
	push(@files, "/webapps/nps/WEB-INF/modules/ncs");						# Directory
	push(@files, "/webapps/nps/WEB-INF/ncs.tld");							# Files
	push(@files, "/work/Standalone/localhost/nps/portal/modules/ncs");		# Directory

	return(@files);

} # GetNcsFiles

#**********************************************************************
# Name:			GetNssFiles
# Description:	Returns the list of NSS files
# Parameters:	None
# Return:		array	- Array of files
# Notes:
#**********************************************************************
sub GetNssFiles()
{
	my @files;

	# *** Please keep in alphabetical order for ease of maintenance. ***

	# You can include individual files, multiple files via wild cards, and
	# directories in this list.  Please note that if you include a directory
	# then all files in that directory will be removed.
	# For example:
	# Individual file:	/webapps/nps/WEB-INF/ncs.tld
	# Multiple files:	/webapps/nps/WEB-INF/lib/ncs*.jar
	# Directories:		/webapps/nps/portal/modules/ncs

	push(@files, "/webapps/nps/packages/nssmgmt.npm");						# Files
	push(@files, "/webapps/nps/portal/modules/nss");						# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_nssmgmt");			# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_nssmgmtjar");		# Directory
	push(@files, "/webapps/nps/WEB-INF/lib/nss*.jar");						# Files
	push(@files, "/webapps/nps/WEB-INF/modules/nssmgmt");					# Directory
	push(@files, "/webapps/nps/WEB-INF/nss.tld");							# Files
	push(@files, "/work/Standalone/localhost/nps/portal/modules/nss");		# Directory

	return(@files);

} # GetNssFiles

#**********************************************************************
# Name:			GetCommonFiles
# Description:	Returns the list of common (storage) files
# Parameters:	None
# Return:		array	- Array of files
# Notes:
#**********************************************************************
sub GetCommonFiles()
{
	my @files;

	# *** Please keep in alphabetical order for ease of maintenance. ***

	# You can include individual files, multiple files via wild cards, and
	# directories in this list.  Please note that if you include a directory
	# then all files in that directory will be removed.
	# For example:
	# Individual file:	/webapps/nps/WEB-INF/ncs.tld
	# Multiple files:	/webapps/nps/WEB-INF/lib/ncs*.jar
	# Directories:		/webapps/nps/portal/modules/ncs

	push(@files, "/webapps/nps/packages/storagemgmt.npm");					# Files
	push(@files, "/webapps/nps/portal/modules/storage");					# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_storagemgmt");		# Directory
	push(@files, "/webapps/nps/UninstallerData/Uninstall_storagemgmtjar");	# Directory
	push(@files, "/webapps/nps/WEB-INF/modules/storage");					# Directory
    push(@files, "/work/Standalone/localhost/nps/portal/modules/storage");	# Directory

	return(@files);

} # GetCommonFiles

#**********************************************************************
# Name:			GetPrefix
# Description:	Returns the prefix directory for the OS this utility
# 				is running on
# Parameters:	None
# Return:		scalar	- (string) Prefix directory
# Notes:
#**********************************************************************
sub GetPrefix
{
	if ($^O eq "NetWare")
	{
		return("sys:/tomcat/4");
	}
	elsif ($^O eq "linux")
	{
		return("/var/opt/novell/tomcat4");
	} # if ($^O eq "NetWare")

	die("This utility is not supported on the $^O operating system.  Please contact the author.");	

} # GetPrefix

#**********************************************************************
# Name:			ShowHelp
# Description:	Shows the syntax help for this utility
# Parameters:	None
# Return:		None
# Notes:
#**********************************************************************
sub ShowHelp
{
	print("imgr_clean.pl 0.1. Copyright (c) 2007 Novell, Inc. All Rights Reserved.\n");
	print("Usage: perl -w imgr_clean.pl [OPTION]\n");
	print("Removes the Novell iManager plugin files for NCS/BCC, NSS, and ARK\n\n");
    print("Valid options are:\n");
	print("  --ark       Removes the files for the ARK plugin\n");
	print("  --common    Removes the files for the common storage plugin\n");
	print("  --ncs       Removes the files for the NCS/BCC plugin\n");
	print("  --nss       Removes the files for the NSS plugin\n");
	print("  --verbose   Prints information as each file and directory are removed\n");
	print("  --help      Display this help and exit\n\n");
	print("At least one of --ark, --common, --ncs, or --nss must be specified\n\n");

} # ShowHelp

#**********************************************************************
# Name:			DoDelete
# Description:	Deletes the files from the specified array.  This is
# 				the work horse of this script.
# Parameters:	vector	- (boolean) Verbose (T/F)?
# 				vector	- (boolean) Add prefix (T/F)?
# 				array	- Array of files to delete
# Return:		None
# Notes:
#**********************************************************************
sub DoDelete($$@)
{
	my ($bVerbose, $bAddPrefix, @fileList) = @_;
	my $file;
	my @fileData;
	my @deleteList;
	my $status;
	my $prefix = GetPrefix();

	foreach $file (@fileList)
	{
		if ($bAddPrefix)
		{
			$file = $prefix . $file;
		} # if ($bAddPrefix)

		if ($bVerbose)
		{
			print("\n***********************************************************************\n");
			print("Calling stat on $file\n");			
		} # if ($bVerbose)

		@deleteList = glob($file);		# Handle wild cards in the file list

		if (@deleteList)
		{
			@fileData = stat($deleteList[0]);
	
			if (@fileData)
			{
				if ($bVerbose)
				{
					print("File mode: $fileData[2]\n");
				} # if ($bVerbose)
	
				if (S_ISREG($fileData[2]))
				{
					if ($bVerbose)
					{
						print("Deleting file $file\n");
					} # if ($bVerbose)
	
					@deleteList = glob($file);
					chmod(0660, @deleteList);
					$status = unlink(@deleteList);
	
					if ($bVerbose)
					{
						if ($status)
						{
							print("Success: File $file has been deleted\n");	
						}
						else
						{
							print("Error: Could not delete file $file\n");	
						} # if ($status)					
					} # if ($bVerbose)
				}
				elsif (S_ISDIR($fileData[2]))
				{
					if ($bVerbose)
					{
						print("Deleting directory $file\n");
					} # if ($bVerbose)
	
					chmod(0770, $file);
					@deleteList = glob($file . "/*");

					# Call DoDelete() recursively so that any subdirectories
					# are properly deleted.  The file list already has the
					# prefix, so don't add it again
					&DoDelete($bVerbose, 0, @deleteList);

					# Finally, remove the parent directory
					$status = rmdir($file);
	
					if ($bVerbose)
					{
						if ($status)
						{
							print("Success: Directory $file has been removed\n");	
						}
						else
						{
							print("Error: Could not remove directory $file\n");	
						} # if ($status)					
					} # if ($bVerbose)
				}
				else
				{
					print("Error: Invalid file type\n");				
				} # if (S_ISREG(@fileData[2]))
			}
			else
			{
				print("Error: Could not stat $file\n");
			} # if (@fileData)
		}
		else
		{
			print("Error: Unknown file $file\n");
		} # if (@deleteList)
	} # foreach $file (@fileList)
	
} # DoDelete

#**********************************************************************
# Name:			Main program entry point
# Description:	Deletes the iManager plugin files for NCS/BCC, NSS,
# 				and or ARK
# Parameters:	None
# Return:		int		- 0 on success.
# Notes:
#**********************************************************************

my $bVerbose = 0;
my $bArk = 0;
my $bNcs = 0;
my $bNss = 0;
my $bCommon = 0;
my $bHelp = 0;
my %opts = ('verbose' => \$bVerbose,		# --verbose ==> Verbose
			'ark' => \$bArk,				# --ark ==> Delete the ARK files
			'ncs' => \$bNcs,				# --ncs ==> Delete the NCS files
			'nss' => \$bNss,				# --ncs ==> Delete the NSS files
			'common' => \$bCommon,			# --storage ==> Delete the Storage (Common) files
			'help' => \$bHelp);				# --help ==> Call ShowHelp() and exit

GetOptions(%opts);

if ($bHelp || (!$bArk && !$bNcs && !$bNss && !$bCommon))
{
	ShowHelp();
	exit(1);	
} # if ($bHelp || (!$bArk && !$bNcs && !$bNss && !$bCommon))

if ($bArk)
{
	DoDelete($bVerbose, 1, GetArkFiles());
} # if ($bArk)

if ($bNcs)
{
	DoDelete($bVerbose, 1, GetNcsFiles());
} # if ($bNcs)

if ($bNss)
{
	DoDelete($bVerbose, 1, GetNssFiles());
} # if ($bNss)

if ($bCommon)
{
	DoDelete($bVerbose, 1, GetCommonFiles());
} # if ($bNss)

print("\n");		# I like space...

exit(0);


