#!/usr/bin/perl
# 
#  	iManager Plugin Installer for Linux
#  		Program to quickly install iManager plugins on Linux platform.
#
#       Version  1.0
#       Date     6th Jan 2007
#    
#       Author   Nagareshwar Y Talekar (tnagareshwar@gmail.com)
#
#
##################################################################################################



use Switch;


use strict;

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




# Global variables
our $pluginFile;
our $tomcatPath = "/var/opt/novell/tomcat4";
our $extractPath = "/tmp/iManPlugin";
our $modName;
 
#
# Usage: perl iManPluginInstallerLinux [-t <tomcat path>] <plugin file path>
#

#check if any command line parameters specified
if( @ARGV < 1 ||  @ARGV > 3)
{
	printUsage();
 	exit;
}

my $pluginFile = $ARGV[0];


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

# Step 1 Extract the plugin file to tmp directory
print "\n\n Extracting the plugin file $pluginFile\n";
if( ExtractPluginFile($pluginFile) != 0 )
{
	print "\n\n Error : Failed to extract the plugin file $pluginFile.\n Make sure you have specified complete path of plugin file and the file exists\n\n";	
	exit(0);
}

if( FindModuleName() == 0 )
{
    goto Cleanup;        
}



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



# Step 3 Copy Files 
print "\n\n Installing the plugin file into iManager...\n"; 

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


# 3.2 Move the module to plugin folder
#print "\n\n";
system("cp -r $extractPath/currentwebapp/portal/modules  $tomcatPath/webapps/nps/portal 2> /tmp/imanerr.txt");


# 3.3 Move the web inf files to web-inf folder
#print "\n\n";
system("cp -r $extractPath/currentwebapp/WEB-INF $tomcatPath/webapps/nps 2> /tmp/imanerr.txt");


# 3.4 Move the meta files to right location
#print "\n\n";
my $destPath = "$tomcatPath/webapps/nps/WEB-INF/modules/$modName";
system("mkdir $destPath 2> /tmp/imanerr.txt");
system("cp -r $extractPath/META-INF $destPath 2> /tmp/imanerr.txt");

# 3.5 Remove the temporary files from work directory
#print "\n\n";
$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);


Cleanup:
# Step 5 Remove temporary files
# TODO suppress any prompting during removing file..
print "\n\n Cleaning up temporary files...\n";
system("rm -r /tmp/iManPlugin 2> /tmp/imanerr.txt");


print "\n\n Successfully installed the specifiled plugin to iManager\n\n";



exit;
 



#****************************************************************************
sub FindModuleName
{
my $tempPath = $extractPath . "/currentwebapp/portal/modules";
my $line;
my $pline;
my @mods;

print "\n Finding the module name...\n";
system("cd $tempPath;dir > /tmp/imanModName.txt;");

if( !open(TMPH, "/tmp/imanModName.txt") )
{
	print "\n Error : Failed to find the plugin module name";
 	return 0;
}

while ($line = <TMPH>)
{
	$pline = $line;
}

@mods = split(/ /, $pline);

$modName = $mods[@mods-1];
chomp $modName;

#for(my $i=0; $i < @mods; $i++)
#{
#	print "\n part $i = $mods[$i]";
#}

print "\n Module name is [$modName]";

close(HANDLE);
return 1;

}


 
 
   
#****************************************************************************
  
 # 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;
}




 #****************************************************************************

# Function to extract npm file to tmp directory
sub ExtractPluginFile
{
  my $path = $_[0];
  my $result = system("cd /tmp;mkdir iManPlugin;cd iManPlugin;jar -xf $path");
	
  print "\n Result of the extract command is : $result";
  
  return $result;
}
 
 
 
 #****************************************************************************
 
 # print the usage  
 sub printUsage
 {
    print "\n\n iManagerPluginInstaller for Linux v1.0 \n\t\tby Nagareshwar Y Talekar (tnagareshwar\@novell.com)";
    print "\n\n Usage: \n\tperl iManPluginInstallerLinux.pl <plugin file path> \n\n";
    print "\n\n Example: \n\tiManPluginInstallerLinux.pl /plugin/sso.npm";
    print "\n\n";
 }
 
 
 #****************************************************************************

