#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# perf72L src/perf/cmdperft/usr/bin/topas/listtrec.pl 1.15.2.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2017 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)29       1.15.2.5  src/perf/cmdperft/usr/bin/topas/listtrec.pl, cmdperft, perf72L, l2017_43A8 9/15/17 11:29:07
use Getopt::Std;
use warnings;
my $cmdargs;
my $header_printed=0;

my $print_flag=1;

my $global_pid=-1; # global_pid will contain the PID of the  persistent recording when persistent recording is being listed.
my $global_pgname="";
my $show_persistent_only=0;
my $show_wle=0;
my $kill_arg="";

sub is_recording_process
{
	my ($process_id)=@_;
	my $pattern1='^/usr/bin/xmwlm  *.*' .  " $process_id" . ' *$' ;
	my $pattern2='^/usr/bin/topasrec  *.*' .  " $process_id" . ' *$' ;
	my $pattern3='^/usr/bin/topas_nmon  *.*' .  " $process_id" . ' *$' ;

	my $pattern="($pattern1|$pattern2|$pattern3)";

	my $cmd='/usr/bin/ps -e -o args,pid';

	my @list=`$cmd | /usr/bin/grep -E \"($pattern1|$pattern2|$pattern3)\""`;

	my $len=scalar(@list);

	if ( $len > 0 )
	{
		my @list2=`$cmd | /usr/bin/grep -E \"($pattern3)\""`;
		
		my $len2 =scalar(@list);
		
		if( $len2 > 0 )
		{		
			$kill_arg="-USR1"; # For nmon use kill with -USR1	
		}
		
		return 1;
	}
	else
	{
		return 0;	
	}
}

sub is_wle_running( )
{
	my @list=`/usr/bin/crontab  -l | /usr/bin/grep "/usr/bin/topasout -R weekly"`;

	my $list_len = scalar(@list);

	if ( $list_len <= 0 )
	{
		return 0;
	}

	if( index($list[0],"topasout -R weekly") >= 0 and index($list[0],"grep") <=  0)
	{
		return 1;
	}

	return 0;
}

sub parse_topasrec_cmd_line
{
	my ($cmdline,$cmd,$output_path_opt, $start_time_opt) = @_;
	my %map=  ( ) ;
	my @tokens = split " ",$cmdline;
	my $arg_len = scalar(@tokens);

	# sample cmdline will be 
	# "topasrec     root 483452 ./topasrec -C -o test -S 00:11:22,Jan18,2008"
	my $command = $tokens[0];

	# Add a check here to check for substring
	if( $command ne "topasrec")
	{
		return %map
	}

	my $user = $tokens[1];
	my $pid = $tokens[2];
	my $argv_0 = $tokens[3];  # from the args list
	my $argv_1 = " ";

	if( $arg_len >= 4 )
	{
		$argv_1 = $tokens[4];
	}

	if (( $argv_0 ne $cmd ) or (( $argv_1 ne "-C") and ( $argv_1 ne "-G")) )  # we only want cmd lines like "/usr/bin/topasrec -L ..."
	{
		return %map;
	}

	# shift command,user,pid,argv_0
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);

	my $output_path;
	my $start_time;
	my $counter=0;
	my $persistent="no";

	foreach my $word (@tokens)
	{
		#print " $word \n" ;

		if ( $word eq  $output_path_opt)
		{
			$output_path=$tokens[$counter + 1];
		}
		elsif (  index($word,"-ystart_time=") >= 0  ) 
		{
			my $full_len = length("-ystart_time=");
			my $substr_len = length($word) - length("-ystart_time=");
			$start_time=substr($word, $full_len,$substr_len);
		} 
		elsif ( $word eq  "-ypersistent=1")
		{
			$persistent="yes";
		}

		$counter++;
	}

	$map{'path'}=$output_path;
	$map{'pid'} =$pid;
	$map{'user'}=$user;
	$map{'start_time'}=$start_time;
	$map{'persistent'}=$persistent;
	return %map;
}

sub do_CEC_Cluster_listing 
{
	
	my ($format)=@_;
	#my $val=" ";
	my $cmd2="/usr/bin/topasrec";
	my $output_path_opt2="-o";
	my $start_time_opt2="-ystart_time";

	my $counter=1;
	my %map = ();
	my $path="";
	my $pid="";
	my $user="";
	my $start_time="";
	my $fmt="";
	my $retval=-1;

	my $p_sno;
	my $p_user;
	my $p_start_time;
	my $p_path;
	my $p_status;
	my $p_pid;
	my $p_per;
	my $p_fmt;
	my $p_persistent;
	my $str=" ";

	# Add a check here to check for substring. Both xmwlm and topasrec
	# are taken inorder to maintain the compatibility to find the list 
	# of older recording also.
	if($format eq "CEC")
	{
		$str = "-C";	
	}
	elsif ($format eq "Cluster")
	{
		$str = "-G";
	}	
	my @list =`/usr/bin/ps -e -o command,user,pid,args  | /usr/bin/grep "topasrec $str"`;
	# sample ps listing of CEC recording is shown in the next line:
	# topasrec     root 323820 /usr/bin/topasrec -C -c 6 -S 01:52:39,May20,2008 -o /
	foreach my $proc (@list)
	{
		%map = parse_topasrec_cmd_line($proc,$cmd2,$output_path_opt2,$start_time_opt2);

		if( %map ) 
		{
			$path = $map{'path'};
			$pid = $map {'pid'};
			$user = $map {'user'};
			$start_time = $map{'start_time'};
			$persistent = $map{'persistent'};	

			if ($print_flag eq 1 )
			{	
				$p_sno = sprintf ("-%3d" , $counter);
				$p_user = sprintf("%-10s", $user);
				$p_start_time=sprintf("%-19s", $start_time);
				$p_status = sprintf(" %-15s" , "Running  ");
				$p_pid = sprintf("%-10d" , $pid);
				$p_fmt = $format;

				if ( $persistent eq "yes") 
				{

					if ( $header_printed eq 0  )	
					{
						$header_printed = 1;		
						#	10	  20	    30	      40	50	  60	    70	      80	90	 100		
						#1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
						printf "Pid         User       Fmt Start_time           Path                            Status \n";

					}	

					$p_path = sprintf(" *%-30s",$path);
					printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";
					$retval=0;


					# Only one persistent recording exist always. if show_persistent_only is true return
					if ( $show_persistent_only == 1 )
					{
						return $retval;
					}
				}
				else
				{
					$p_path = sprintf(" %-30s",$path);

					if ( $show_persistent_only == 0 )
					{

						if ( $header_printed eq 0  )	
						{
							$header_printed = 1;		
							#	10	  20	    30	      40	50	  60	    70	      80	90	 100		
							#1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
							printf "Pid         User       Fmt Start_time           Path                            Status \n";
						}	

						printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";

						$retval=0;
					}
				}

			}
			elsif ( $persistent eq "yes")  #  when print_flag is zero we want to get the Process ID of persistent recording
			{
				$global_pid=$pid + 0; # Dummy addition is required here to convert string to number .  Dont' remove this + 0
				$retval=0;
			}	
		}
	}

	return $retval;
}

sub parse_local_binary_cmd_line
{
	my ($cmdline,$cmd,$output_path_opt, $start_time_opt) = @_;
	my %map=  ( ) ;
	my @tokens = split " ",$cmdline;
	my $arg_len = scalar(@tokens);
	my $persistent="no";

	my $command = $tokens[0];
	my $str=" ";

	# Add a check here to check for substring. Both xmwlm and topasrec
	# are taken inorder to maintain the compatibility to find the list 
	# of older recording also.
	if($tokens[0] eq "xmwlm")
	{
		$str = "xmwlm";	
	}
	elsif ($tokens[0] eq "topasrec")
	{
		$str = "topasrec";
	}

	if( $command ne $str)
	{
		return %map
	}

	my $user = $tokens[1];
	my $pid = $tokens[2];
	my $argv_0 = $tokens[3];  # from the args list
	my $argv_1 = " ";

	if( $arg_len >= 4 )
	{
		$argv_1 = $tokens[4];
	}

	if (( $argv_0 ne $cmd ) )  # we only want cmd lines like "/usr/bin/xmwlm -T ..."
	{
		#print "returning ...",":$argv_0:$command\n";
		return %map;
	}

	# shift command,user,pid,argv_0
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);

	my $output_path;
	my $start_time="00:00:00";
	my $counter=0;

	foreach my $word (@tokens)
	{
		if ( $word eq  $output_path_opt)
		{
			$output_path=$tokens[$counter + 1];
		}
		elsif (  index($word,"-ystart_time=") >= 0  ) 
		{
			my $full_len = length("-ystart_time=");
			my $substr_len = length($word) - length("-ystart_time=");
			$start_time=substr($word, $full_len,$substr_len);
		}
		elsif( $word eq "-ypersistent=1")
		{
			$persistent="yes";
		}

		$counter++;
	}

	$map{'path'}=$output_path;
	$map{'pid'} =$pid;
	$map{'user'}=$user;
	$map{'start_time'}=$start_time;
	$map{'persistent'}=$persistent;

	return %map;
}

sub do_local_xmwlm_listing
{
	my @list=`/usr/bin/ps -e -o command,user,pid,args  | /usr/bin/grep "/usr/bin/xmwlm -T"`;
	my $cmd2="/usr/bin/xmwlm";
	my $output_path_opt2="-o";
	my $start_time_opt2="-y";
	my $retval=-1;

	my $counter=1;
	my %map = ();
	my $path="";
	my $pid="";
	my $user="";
	my $start_time="";
	my $fmt="";

	my $p_sno;
	my $p_user;
	my $p_start_time;
	my $p_path;
	my $p_status;
	my $p_pid;
	my $p_per;
	my $p_fmt;

	# sample /usr/bin/ps listing for local bin recording
	# xmwlm        root 299066 /usr/bin/xmwlm -T -s 900 -c 96 -y 03:56:30,May18,2008 -o /smit/
	foreach my $proc (@list)
	{
		%map = parse_local_binary_cmd_line($proc,$cmd2,$output_path_opt2,$start_time_opt2);

		if( %map )
		{
			$path = $map{'path'};
			$pid = $map {'pid'};
			$user = $map {'user'};
			$start_time = $map{'start_time'};
			$persistent = $map{'persistent'};

			if ($print_flag eq 1 )
			{
				$p_sno = sprintf ("-%3d" , $counter);
				$p_user = sprintf("%-10s", $user);
				$p_start_time=sprintf("%-19s", $start_time);
				$p_path = sprintf(" %-30s",$path);
				$p_status = sprintf(" %-15s" , "Running  ");
				$p_pid = sprintf("%-10d" , $pid);
				$p_fmt = "bin";


				if ( $persistent eq "yes")
				{
					if ( is_wle_running( ) == 1 )
					{
						$p_path = sprintf(" #%-30s",$path);
						if( $show_wle == 1 or $show_wle == 2)
						{
							$retval=0;
						}
					}
					else
					{
						$p_path = sprintf(" *%-30s",$path);

						if( $show_wle == 0 or $show_wle == 2)
						{
							$retval=0;
						}
					}


					if ( $header_printed eq 0  )
					{
						$header_printed = 1;
						#       10    20        30        40    50    60        70        80
						########12345678901234567890123456789012345678901234567890123456789012345678901234567890
						printf "Pid         User       Fmt Start_time          Path                            Status \n";
					}

					printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";


					# Only one persistent recording exist always. if show_persistent_only is true return
					if ( $show_persistent_only == 1 )
					{
						return $retval;
					}
				}
				else
				{
					$p_path = sprintf(" %-30s",$path);

					if ( $show_persistent_only == 0 )
					{

						if ( $header_printed eq 0  )
						{
							$header_printed = 1;
							#       10    20        30        40    50    60        70        80
							########12345678901234567890123456789012345678901234567890123456789012345678901234567890
							printf "Pid         User       Fmt Start_time          Path                            Status \n";
						}

						printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";
						$retval=0;
					}
				}

			}
			elsif ( $persistent eq "yes")  #  when print_flag is zero we want to get the Process ID of persistent recording
			{
				$global_pid=$pid + 0; # Dummy addition is required here to convert string to number .  Dont' remove this + 0
				$retval=0;
			}
		}
	}

	return $retval;
}
			
# lists all the local recording based on the type of the argument that 
# is passed. types = bin, nmon, azizo.
# For all new recordings by topasrec this is called to list.
# To list the old recordings the older functions do_local_xmwlm_listing,
# do_local_nmon_listing are retained.

sub do_local_topasrec_listing 
{
	my ($format)=@_;
	my $cmd2;
	my $output_path_opt2;
	my $start_time_opt2;

	my @list=`/usr/bin/ps -e -o command,user,pid,args  | /usr/bin/grep "type=$format"`;
	if ( $format eq "bin" or $format eq "azizo")
	{
		$cmd2="/usr/bin/topasrec";
		$output_path_opt2="-o";
		$start_time_opt2="-y";

	}
	elsif ( $format eq "nmon" ){

		$cmd2="/usr/bin/topasrec";
		$output_path_opt2="-youtput_dir=";
		$start_time_opt2="-ystart_time=";
	}
	my $retval=-1;

	my $counter=1;
	my %map = ();
	my $path="";
	my $pid="";
	my $user="";
	my $start_time="";
	my $fmt="";

	my $p_sno;
	my $p_user;
	my $p_start_time;
	my $p_path;
	my $p_status;
	my $p_pid;
	my $p_per;
	my $p_fmt;

	# sample /usr/bin/ps listing for local bin recording 
	# xmwlm        root 299066 /usr/bin/topasrec -L -s 900 -c 96 -y 03:56:30,May18,2008 -o /smit/ 
	foreach my $proc (@list)
	{
		if ( $format eq "bin" or $format eq "azizo")
		{
			%map = parse_local_binary_cmd_line($proc,$cmd2,$output_path_opt2,$start_time_opt2);
		}
		elsif ($format eq "nmon")
		{

			%map = parse_nmon_cmd_line($proc,$cmd2,$output_path_opt2,$start_time_opt2);
		}

		if( %map ) 
		{
			$path = $map{'path'};
			$pid = $map {'pid'};
			$user = $map {'user'};
			$start_time = $map{'start_time'};
			$persistent = $map{'persistent'};	

			if ($print_flag eq 1 )
			{	
				$p_sno = sprintf ("-%3d" , $counter);
				$p_user = sprintf("%-10s", $user);
				$p_start_time=sprintf("%-19s", $start_time);
				$p_path = sprintf(" %-30s",$path);
				$p_status = sprintf(" %-15s" , "Running  ");
				$p_pid = sprintf("%-10d" , $pid);
				$p_fmt = $format;


				if ( $persistent eq "yes") 
				{
					if ( $format eq "bin" or $format eq "azizo"){
						if ( is_wle_running( ) == 1 )
						{
							$p_path = sprintf(" #%-30s",$path);

							if( $show_wle == 1 or $show_wle == 2)
							{
								$retval=0;
							}
						}
						else
						{
							$p_path = sprintf(" *%-30s",$path);

							if( $show_wle == 0 or $show_wle == 2)
							{
								$retval=0;
							}
						}
					}
					if ( $format eq "bin" )
					{
						my $rval=system("/usr/bin/topasout","-p",$p_pid);
						$rval = $rval >> 8;
						if ($rval == 0 || $rval == 3)
						{
							$p_status = sprintf(" %-15s" , "Running  ");
						}
						if ($rval == 2)
						{
							$p_status = sprintf(" %-15s" , "Paused  ");
						}
					}


					if ( $header_printed eq 0  )
					{
						$header_printed = 1;
						#		10	  20	    30	      40	50	  60	    70	      80
						########12345678901234567890123456789012345678901234567890123456789012345678901234567890
						printf "Pid         User       Fmt Start_time          Path                            Status \n";
					}

					printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";


					# Only one persistent recording exist always. if show_persistent_only is true return
					if ( $show_persistent_only == 1 )
					{
						return $retval;
					}
				}
				else
				{
					$p_path = sprintf(" %-30s",$path);

					if ( $show_persistent_only == 0 )
					{

						if ( $header_printed eq 0  )
						{
							$header_printed = 1;
							#		10	  20	    30	      40	50	  60	    70	      80
							########12345678901234567890123456789012345678901234567890123456789012345678901234567890
							printf "Pid         User       Fmt Start_time          Path                            Status \n";
						}

						printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";
						$retval=0;
					}
				}

			}
			elsif ( $persistent eq "yes")  #  when print_flag is zero we want to get the Process ID of persistent recording
			{	
				$global_pid=$pid + 0; # Dummy addition is required here to convert string to number .  Dont' remove this + 0
				$retval=0;
			}
		}
	}

	return $retval;
}

sub parse_nmon_cmd_line
{
	my ($cmdline,$cmd,$output_path_opt, $start_time_opt) = @_;
	my %map=  ( ) ;
	my @tokens = split " ",$cmdline;
	my $arg_len = scalar(@tokens);
	my $persistent="no";
	my $command = $tokens[0];
	my $str = " ";
	# Add a check here to check for substring
	# Both topas_nmon and topasrec
	# are taken inorder to maintain the compatibility to find the list 
	# of older recording also.
	if($tokens[0] eq "topas_nmon")
	{
		$str = "topas_nmon";	
	}
	elsif ($tokens[0] eq "topasrec")
	{
		$str = "topasrec";	
	}

	if( $command ne $str)
	{
		return %map
	}

	my $user = $tokens[1];
	my $pid = $tokens[2];
	my $argv_0 = $tokens[3];  # from the args list
	my $argv_1 = " ";

	if( $arg_len >= 4 )
	{
		$argv_1 = $tokens[4];
	}

	my $flag = (( $argv_0 eq $cmd ) 
			and ( index($cmdline,"-youtput_dir="))
			and ( index($cmdline,"-ystart_time=")));

	if ( not $flag ) 
	{
		return %map;
	}

	# shift command,user,pid,argv_0
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);
	shift(@tokens);

	my $output_path="";
	my $start_time="";
	my $counter=0;

	foreach my $word (@tokens)
	{
		# word should be one of "-youtput_dir=","-ystart_time="
		if ( substr($word,0,2) ne "-y" )
		{
			next;
		}

		# remove "-y" prefix and take the rest of the string.
		$word=substr $word,2;

		my $out_loc=  index($word,"output_dir=");  
		my $start_loc=  index($word,"start_time=");
		my $pers_loc= index($word,"persistent=1");

		if($out_loc >= 0 )
		{
			$output_path=substr $word,$out_loc + length("output_dir=") ;
		}
		elsif($start_loc >= 0)
		{
			$start_time= substr $word,$start_loc + length("start_time=") ;
		}
		elsif($pers_loc >= 0 )
		{
			$persistent="yes";
		}
		else
		{
			# ignore this word
			next;
		}
	}

	$map{'path'}=$output_path;
	$map{'pid'} =$pid;
	$map{'user'}=$user;
	$map{'start_time'}=$start_time;
	$map{'persistent'}=$persistent;

	return %map;
}

sub do_local_nmon_listing( )
{
	my @list=`/usr/bin/ps -e -o command,user,pid,args  | /usr/bin/grep "/usr/bin/topas_nmon"`;
	my $cmd2="/usr/bin/topas_nmon";
	my $output_path_opt2="-youtput_dir=";
	my $start_time_opt2="-ystart_time=";
	my $retval=-1;

	my $counter=1;
	my %map = ();
	my $path="";
	my $pid="";
	my $user="";
	my $start_time="";
	my $fmt="";

	my $p_sno;
	my $p_user;
	my $p_start_time;
	my $p_path;
	my $p_status;
	my $p_pid;
	my $p_per;
	my $p_fmt;

	# Sample /usr/bin/ps listing for nmon command line:
	# "topas_nmon root 344252  /usr/bin/topas_nmon -fT -s 30 -c 240 -youtput_dir=/home/radhat/clock10 -ystart_time=22:51:32,May14,2008" ;
	foreach my $proc (@list)
	{
		%map = parse_nmon_cmd_line($proc,$cmd2,$output_path_opt2,$start_time_opt2);

		if( %map ) 
		{
			$path = $map{'path'};
			$pid = $map {'pid'};
			$user = $map {'user'};
			$start_time = $map{'start_time'};
			$persistent = $map{'persistent'};	

			if ($print_flag eq 1 )
			{	

				$p_sno = sprintf ("-%3d" , $counter);
				$p_user = sprintf("%-10s", $user);
				$p_start_time=sprintf("%-19s", $start_time);
				$p_path = sprintf(" %-30s",$path);
				$p_status = sprintf(" %-15s" , "Running  ");
				$p_pid = sprintf("%-10d" , $pid);
				$p_fmt = "nmon";



				if ( $persistent eq "yes") 
				{
					if ( $header_printed eq 0  )
					{
						$header_printed = 1;
						#		10	  20	    30	      40	50	  60	    70	      80
						########12345678901234567890123456789012345678901234567890123456789012345678901234567890
						printf "Pid         User       Fmt Start_time           Path                            Status \n";
					}

					$p_path = sprintf(" *%-30s",$path);
					printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";
					$retval=0;

					# Only one persistent recording exist always. if show_persistent_only is true return
					if ( $show_persistent_only == 1 )
					{
						return $retval;
					}
				}
				else
				{
					$p_path = sprintf(" %-30s",$path);

					if ( $show_persistent_only == 0  && length($path) && length($start_time))
					{
						if ( $header_printed eq 0  )
						{
							$header_printed = 1;
							#		10	  20	    30	      40	50	  60	    70	      80
							########12345678901234567890123456789012345678901234567890123456789012345678901234567890
							printf "Pid         User       Fmt Start_time           Path                            Status \n";
						}

						printf "$p_pid $p_user $p_fmt $p_start_time $p_path $p_status\n";
						$retval=0;
					}
				}
			}
			elsif ( $persistent eq "yes")  #  when print_flag is zero we want to get the Process ID of persistent recording
			{
				$global_pid=$pid + 0; # Dummy addition is required here to convert string to number .  Dont' remove this + 0
				$retval=0;
			}
		}
	}

	return $retval;
}

sub do_kill_proc
{
	my ($pid) =  @_;
	if( is_recording_process($pid) == 1 )
	{
		`/usr/bin/kill $kill_arg $pid`;
		 return $?;	
	}
		
	return 1;	
}

sub Usage
{
	print "Supported usage: listtrec { -l [ -P] <type> | -p <pid } | -k  { cec_bin | cluster_bin | local_bin | local_nmon } \n";
}

sub kill_persistent
{
	my ($persistent_kill)=@_;
	my @format = ('bin', 'nmon', 'azizo','CEC','Cluster');

	$print_flag=0;
	if ( $persistent_kill eq "cec_bin" )
	{
		do_CEC_Cluster_listing($format[3]);
	}
	elsif ( $persistent_kill eq "cluster_bin" )
	{
		do_CEC_Cluster_listing($format[4]);
	}
	elsif ( $persistent_kill eq "local_bin" )
	{
		do_local_xmwlm_listing();
		do_local_topasrec_listing($format[0]);
	}
	elsif ( $persistent_kill eq "local_nmon" )
	{
		do_local_nmon_listing();
		do_local_topasrec_listing($format[1]);
	}
	elsif ( $persistent_kill eq "local_azizo" )
	{
		do_local_topasrec_listing($format[2]);
	}
	else 
	{
		Usage();
		return 1;
	}

	if ( $global_pid  > 0 )
	{
		return do_kill_proc($global_pid);
	}

	return 0;
}

sub main
{
	my $type = "ALL";

	my $l_flag = 0;
	my @format = ('bin', 'nmon', 'azizo','CEC','Cluster');
	my $pid=9999999;
	my $p_flag = 0;
	my $retval=-1;
	my $retval1=-1;
        my $retval2=-1;
	my $retval3=-1;

	getopts('l:p:k:P',\%opts);

	my $l_type = $opts{'l'};
	my $l_kill = $opts{'p'};
	my $persistent_kill = $opts{'k'};

	if( $opts{'P'} ) # use topasout to list the recordings if persistent 
	{
		if( $l_type eq "ALL" || $l_type eq "local_bin" || $l_type eq "local_nmon" )
		{
			system("topasout","-z",$l_type);
			$retval1=$?;
			if( $retval1 != 0 )
			{
				$retval1=255;
			}
			$header_printed=1;
		}
		if( $l_type eq "CEC" || $l_type eq "ALL" || $l_type eq "cec_bin" )
                {
                        $show_persistent_only=1;
                        $retval2=do_CEC_Cluster_listing($format[3]);
                }
		if( $l_type eq "Cluster" || $l_type eq "ALL" || $l_type eq "cluster_bin" )
		{
			$show_persistent_only=1;
			$retval3=do_CEC_Cluster_listing($format[4]);
		}
		if($retval1 != 0 && $retval2 !=0 && $retval3 !=0)
                {
                        $retval=255;
                }
                else
                {
                        $retval=0;
                }	
	}
	elsif ( $l_type and !$l_kill and !$persistent_kill )
	{
		$print_flag=1;

		if ( $l_type eq "CEC" || $l_type eq "cec_bin" )
		{
			$retval=do_CEC_Cluster_listing($format[3]);
		}
		elsif ( $l_type eq "Cluster" || $l_type eq "cluster_bin")
		{
			$retval=do_CEC_Cluster_listing($format[4]);
		}
		elsif ( $l_type eq "local_bin")
		{
			$show_wle=2;
			my $rv1=do_local_xmwlm_listing();
			my $rv2=do_local_topasrec_listing($format[0]);
			if( $rv1 == 0 || $rv2 == 0 )
			{
				$retval=0;
			}
		}
		elsif ( $l_type eq "local_nmon")
		{	
			my $rv1=do_local_nmon_listing();
			my $rv2=do_local_topasrec_listing($format[1]);	
			if( $rv1 == 0 || $rv2 == 0 )
			{
				$retval=0;
			}
		}
		elsif ( $l_type eq "local_azizo")
		{
			$show_wle=2;
			$retval = do_local_topasrec_listing($format[2]);		
		}
		elsif ( $l_type eq "local_wle")
		{
			$show_persistent_only=1;
			$show_wle=1;
			my $rv1=do_local_xmwlm_listing();	
			my $rv2=do_local_topasrec_listing($format[0]);
			if( $rv1 == 0 || $rv2 == 0 )
			{
				$retval=0;
			}
		}
		elsif ( $l_type eq "ALL" )
		{
			$show_wle=2;
			my $retval1=do_CEC_Cluster_listing($format[3]);
			my $retval2=do_local_xmwlm_listing();	
			my $retval3=do_local_nmon_listing();	
			my $retval5=do_CEC_Cluster_listing($format[4]);
			my $retval4=-1;
			my $retval6;
			for ( $i=0;$i<3;$i++)
			{
				$retval6 = do_local_topasrec_listing($format[$i]);	
				if ( $retval6 == 0 )
				{
					$retval4=0;
				}
			}

			if ( $retval1 == 0 or $retval2 == 0 or $retval3 == 0  or $retval4 == 0 or $retval5 == 0 )
			{
				$retval = 0 ;
			}
			else
			{
				$retval = -1;
			}

		}
	}
	elsif( $l_kill and !$l_type and !$persistent_kill)
	{
		$retval=do_kill_proc( $l_kill ) ;
	}
	elsif( !$l_kill and !$l_type and $persistent_kill)
	{
		$retval=kill_persistent($persistent_kill);
	}
	else
	{
		Usage() ;
	}

	return $retval;
}
my $retval=main ( );

# exit codes are used by SMIT.Hence return the exit code
exit $retval;