#!/usr/bin/perl
# 
#  	Linux Control Tomcat
#  		Program to start/stop/restart tomcat server on Linux machine
#
#       Version  1.0
#       Date     5th Jan 2007
#    
#       Author   Nagareshwar Y Talekar (tnagareshwar@gmail.com)
#
#
##################################################################################################




use Net::Ping;
use Socket; 
use Switch;
use threads; 


use strict;

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


 
#
# Usage: perl LinuxControlTomcat.pl {-start | -stop | -restart} 
#

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

my $cmd = $ARGV[0];


if( $cmd =~ /restart/i )
{ 
     ControlTomcat(3);
}
elsif( $cmd =~ /start/i )
   {
      ControlTomcat(1);
   }
   elsif( $cmd =~ /stop/i )
   {
   	ControlTomcat(2);
   
   }
   else
   {
   	printUsage();
   	print "\n Error : Invalid command specified.\n\n";
   	exit;
   }
  
 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");
     	}
     	
     	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");
     	}
     	
     	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 Usage: \n\tperl LinuxControlTomcat.pl {start | stop | restart}  \n\n";
    print "\n Options: \n\tstart     Start the tomcat server";
    print "\n\tstop      Stop the tomcat server ";
    print "\n\trestart   Restart the tomcat server \n\n\n";
    
 }
 
 
  #****************************************************************************

