#!/usr/bin/perl
# 
#  	iManager Plugin Uninstaller for Linux
#  		Program to quickly uninstall iManager plugins on Linux platform.
#
#       Version  1.0
#       Date     18th Jan 2007
#    
#       Author   Nagareshwar Y Talekar (tnagareshwar@gmail.com)
#
#
#       Description  This program uninstalls the plugin from iManager. Currently it supports only 
#                    plugins ( pcprox , sso, secure workstation) associated with Secure Login product. 
#                    
#
##################################################################################################



use Switch;


use strict;

$|++;                       # force auto flush of output buffer


# Global variables
our $pluginFile;
our $pluginName;
our $tomcatPath = "/var/opt/novell/tomcat4";
our $modName;

 
#
# Usage: perl iManUninstallPlugin  <plugin name>
#

#check if any command line parameters specified
if( @ARGV != 1)
{
	printUsage();
 	exit;
}

my $pluginName = $ARGV[0];

if( $pluginName =~ /pcprox/i )
{
	$modName = "prx";
	$pluginFile = "pcprox.npm";
}
elsif( $pluginName =~ /sso/i )
{
	$modName = "sso";
	$pluginFile = "sso.npm";

}
elsif( $pluginName =~ /sw/i )
{
	$modName = "sw";
	$pluginFile = "sw.npm";
}
else
{
	printUsage();
	print "Error : Specify the proper plugin name. \n\n";
 	exit;
}

#print "\n name = $modName and file = $pluginFile \n";
#exit;

print "\n\n iManagerPluginUninstaller for Linux v1.0 \n\t\tby Nagareshwar Y Talekar (tnagareshwar\@novell.com)";

# Step 1 Stop tomcat server
#print "\n\n Stopping tomcat serer...\n"; 
ControlTomcat(2);


# Step 3 Remove all plugin specific files
print "\n\n Uninstalling the $pluginName plugin from iManager...\n"; 

# 3.1 Remove the plugin file to packages directory
system("rm $tomcatPath/webapps/nps/packages/$pluginFile 2> /tmp/imanerr.txt");


# 3.2 Remove the module files 
system("rm -r $tomcatPath/webapps/nps/portal/modules/$modName 2> /tmp/imanerr.txt");

# 3.3 Remove the library files
if( $modName eq "sso" )
{
	system("rm $tomcatPath/webapps/nps/WEB-INF/lib/sso.jar 2> /tmp/imanerr.txt");
	system("rm $tomcatPath/webapps/nps/WEB-INF/lib/jsso-api.jar 2> /tmp/imanerr.txt");
	system("rm $tomcatPath/webapps/nps/WEB-INF/lib/commons-codec-1.3.jar 2> /tmp/imanerr.txt");
	system("rm $tomcatPath/webapps/nps/WEB-INF/lib/commons-lang-2.0.jar 2> /tmp/imanerr.txt");
}


# 3.4 Remove the binary files
if( $modName eq "prx" )
{	
	system("rm $tomcatPath/webapps/nps/WEB-INF/bin/pcprxwrp.dll 2> /tmp/imanerr.txt");
}


# 3.5 Remove the class files
if( $modName eq "prx" )
{	
	system("rm -r $tomcatPath/webapps/nps/WEB-INF/classes/com/novell/pcprox 2> /tmp/imanerr.txt");
	system("rm -r $tomcatPath/webapps/nps/WEB-INF/classes/com/novell/admin/ndssnapins/loginMethods/pcProx 2> /tmp/imanerr.txt");
}
elsif( $modName eq "sso" )
{
	system("rm -r $tomcatPath/webapps/nps/WEB-INF/classes/com/protocom 2> /tmp/imanerr.txt");
}
elsif( $modName eq "sw" )
{
	system("rm -r $tomcatPath/webapps/nps/WEB-INF/classes/com/novell/sw 2> /tmp/imanerr.txt");
}


# 3.6 Remove the manifest file
system("rm -r $tomcatPath/webapps/nps/WEB-INF/modules/$modName 2> /tmp/imanerr.txt");

# 3.7 Remove the temporary files from work directory
my $destPath = "$tomcatPath/work/Standalone/localhost/nps/portal/modules/$modName";
system("rm -r $destPath 2> /tmp/imanerr.txt");


# Step 4 Start tomcat server
#print "\n\n Starting the tomcat server...\n";
ControlTomcat(1);


print "\n\n Successfully uninstalled the $pluginName plugin from iManager\n\n";


exit;
 


   
#****************************************************************************
  
 # Start/Stop/Restart tomcat server
 sub ControlTomcat
 {
 
     my $cmd = $_[0];
         
     switch($cmd)
     {
     	case 1
     	{
     		print "\n\n Starting the tomcat server, please wait...\n";
     		if( CheckFile("/var/opt/novell/tomcat4/bin/startup.sh") == 0)
     		{
     		    print "\n Error : Tomcat startup script not present.\n Make sure its Linux machine and tomcat is installed on this machine\n\n";
     		    exit;
     		}
     		system("/var/opt/novell/tomcat4/bin/startup.sh 1> /tmp/imanerr.txt");
     	}
     	
     	case 2
     	{
     		print "\n\n Stopping the tomcat server, please wait...\n";
     		#print " Note : If the tomcat server is not already running then it may throw some exceptions.\n";
     		if( CheckFile("/var/opt/novell/tomcat4/bin/shutdown.sh") == 0 )
     		{
     		    print "\n Error : Tomcat shutdown script not present.\n Make sure its Linux machine and tomcat is installed on this machine\n\n";
     		    exit;
     		}

     		system("/var/opt/novell/tomcat4/bin/shutdown.sh 1> /tmp/imanerr.txt");
     	}
     	
     	case 3
     	{
		print "\n\n Restarting the tomcat server...";	
     		ControlTomcat(2);
     		sleep(10);
     		ControlTomcat(1);
     	}
	
	else { print "\n Unknown command specified..."; } 
     
     }
  
     print "\n\n";
 }
 
 
 
 
#****************************************************************************

# Function to check if the specified file is present or not
sub CheckFile
{

  my $file = $_[0];
  
  if( open(HANDLE, $file) )
  {
  	close(HANDLE);
  	return 1;
  }

  return 0;
}


 
 #****************************************************************************
 
 # print the usage  
 sub printUsage
 {
    print "\n\n iManagerPluginUninstaller for Linux v1.0 \n\t\tby Nagareshwar Y Talekar (tnagareshwar\@novell.com)";
    print "\n\n Usage: \n\tperl iManUninstallPlugin.pl {pcprox | sso | sw} \n\n";
    print "\n\n Example: \n\tperl iManUninstallPlugin.pl pcprox\n";
    print "\n\n";
 }
 
 
 #****************************************************************************

