#! /usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2008 
# 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 

# @(#)78   1.8   src/rsct/trace/showtr.perl, trace, rsct_rfos, rfoss001a 9/25/08 14:50:38
# Search on "sub help" and/or "sub usage" for script description.

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

my $spool_dir, $from, $to, $previous, $help, $ls;

if (!GetOptions("spool_dir=s" => \$spool_dir, "from=s" => \$from, "to=s" => \$to, "help" => \$help, "h" =>\$help, "ls" => \$ls))
{
	usage(1);
}

if (undef ne $help)
{
	help();
}

$previous = $ARGV[0];

my $time_specification;

if ((undef ne $from) || (undef ne $to))
{
	if (undef ne $previous)
	{
		usage(1);
	}

	if (undef ne $from)
	{
		if (! ($from =~ /^\d{2}-\d{2}-\d{4}$/))
		{
			usage(1);
		}
		else
		{
			$time_specification .= "--from $from ";
		}
	}
	
	if (undef ne $to)
	{
		if (! ($to =~ /^\d{2}-\d{2}-\d{4}$/))
		{
			usage(1);
		}
		else
		{
			$time_specification .= "--to $to ";
		}
	}
}
else
{
	if (undef eq $previous)
	{
		$previous = 0;
	}

	$time_specification = "--previous $previous";
}

if (undef eq $spool_dir)
{
	$spool_dir = $ENV{CT_TR_SPOOL_DIR};

	if (undef eq $spool_dir)
	{
		printf STDERR "%s:%d: couldn't determine spool_dir\n", $0, __LINE__;
		usage(1);
	}
}

my $pwd = `pwd`;
chomp $pwd;

my $cluster_id;

if ($pwd =~ /^\/var\/ct\/(.+)\/log\//)
{
	$cluster_id = "$1";
}
else
{
	printf STDERR "%s:%d: couldn't determine cluster_id\n(need to be in a subdirectory of /var/ct/*/log)\n", $0, __LINE__;
	usage(1);
}

my $daemon_name;
my $daemon_name_rc = 0;
my $daemon_name_found = 0;

if (! opendir DIR, "$pwd")
{
	printf(STDERR "Error: cannot open current working directory(%s).\n", $pwd);
	exit 2;
}

while (my $entry = readdir(DIR))
{
	if (("." ne $entry) && (".." ne $entry))
	{
		my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat("$entry");

		if (S_ISREG($mode))
		{
			$daemon_name = `rpttr -B $entry 2>/dev/null`;
			$daemon_name_rc = $?;

			if (0 == $daemon_name_rc)
			{
				$daemon_name_found = 1;
				last;
			}
		}
	}
}

close DIR;

if (0 == $daemon_name_found)
{
	print "Cannot determine daemon name because the current directory has no trace files containing in...\n";
	print "Enter daemon name (e.g. IBM.ConfigRMd): ";
	$daemon_name = <STDIN>;
}

chomp $daemon_name;

if ($daemon_name_rc || (! $daemon_name))
{
	printf STDERR "%s:%d: couldn't determine daemon_name\n", $0, __LINE__;
    usage(1);
}

my $cluster_name;

open LS, "ls -l /var/ct |" or die sprintf "%s:%d: trouble with ls -l /var/ct\n", $0, __LINE__;

while (<LS>)
{
	if (/^l.+ (.+?) -> \/var\/ct\/$cluster_id$/)
	{
		$cluster_name = "$1";
		last;
	}
}

close LS;

if (undef eq $cluster_name)
{
	printf STDERR "%s:%d: couldn't determine cluster_name\n", $0, __LINE__;
	usage(1);
}

$ENV{'PATH'} = "/usr/bin:/bin:/usr/sbin";
my $node_name = `hostname`;
chomp $node_name;

$node_name =~ s/\..*$//;

my $node_id = `/usr/sbin/rsct/bin/lsnodeid`;
chomp $node_id;

my $lstrsp_command = sprintf("lstrsp --spool_dir %s --cluster_name %s --cluster_id %s --node_name %s --node_id %s --daemon_name %s %s", $spool_dir, $cluster_name, $cluster_id, $node_name, $node_id, $daemon_name, $time_specification);

if (undef eq $ls)
{
	my $list = `$lstrsp_command`;
	my $rc = $?;

	if ($rc)
	{
		exit $rc;
	}
	else
	{
		if ("" eq $list)
		{
			exit 0;
		}
		else
		{
			# convert newlines to spaces
			$list =~ s/\n/ /g;
			exit system("rpttr $list");
		}
	}
}
else
{
	exit system($lstrsp_command);
}

# functions

sub help
{
	usage();

	print STDERR <<HELP;

The showtr utility passes selected trace spool files corresponding
to the current working directory to rpttr. It thus must be executed
from a directory from which trace files are spooled.

Examples:

# show ConfigRM's trace files from today
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr

# show ConfigRM's trace files from today and yesterday
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr 1

# show ConfigRM's trace files from the last week
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr 7

# show ConfigRM's trace files since a past date
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr --from 03/31/2008

# show ConfigRM's trace files from the beginning of the spool
# until a past date
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr --to 03/31/2008

# show ConfigRM's trace files between two dates (inclusive)
> cd /var/ct/IW/log/mc/IBM.ConfigRM
> showtr --from 03/31/2008 --to 04/15/2008
HELP

	exit 0;
}

sub usage
{
	print STDERR <<USAGE;

Usage:
showtr [--spool_dir <path> | first export CT_TR_SPOOL_DIR=<path>] 
       [--ls]
       [--help]
       [<number of days before today>] | [--from <from date (mm-dd-yyyy)> [--to <to date (mm-dd-yyyy)>]]

Notes:
- <number of days before today> defaults to 0
- the --ls option lists the spool files instead of passing them to rpttr
- showtr must be executed in a subdirectory of /var/ct/*/log
- showtr attempts to determine daemon_name from "rpttr -B" against all trace
  files the current directory until found. If it can't, it prompts for daemon_name.
USAGE

	if (undef ne $_[0])
	{
		exit $_[0];
	}
}
