#!/usr/bin/perl
#
# FCP adapter trace utility
#
# Script to collect all system information required for analysis
#
# Copyright IBM Corp. 2008, 2017
#
# s390-tools is free software; you can redistribute it and/or modify
# it under the terms of the MIT license. See LICENSE for details.
#

use strict;
use warnings;
use English;
use Cwd;
use File::Temp qw/ tempdir /;
use File::Spec::Functions;
use Getopt::Long;

sub version {
	print "$PROGRAM_NAME: version %S390_TOOLS_VERSION%\n";
	print "Copyright IBM Corp. 2008, 2017\n";
}

sub usage
{
	print <<MSG
Usage: $PROGRAM_NAME [<options>]

$PROGRAM_NAME collects all system information required for analysis.

Options:

        -h, --help
		print this help text and exit.

	-v, --version
		print version information and exit.

	-o, --output
		specify the name of the output file
MSG
}

sub dir_content
{
        my @temp_dir;
        opendir(DH, shift()) or return 0;
        @temp_dir = readdir(DH);
        closedir(DH);
        return @temp_dir;
}

sub store_mapper_devices
{
	my $dest_dir = shift();
	my $src_dir = shift();
	my @entries = grep { ! /^\./ } dir_content($src_dir);

	foreach my $map_dev (@entries) {
		my $tf = catfile($src_dir, "$map_dev");
		my $mm = `stat -L -c%t:%T $tf`;
		chomp($mm);
		$mm = join(":", map { hex($_) } split(":", $mm));
		system("echo $mm > " .  catfile($dest_dir, $src_dir, $map_dev));
	}

}

my $out_file="config";
my $temp_dir = tempdir( CLEANUP => 1);

Getopt::Long::Configure(qw/ bundling /);
GetOptions('h|help' => sub {usage(); exit 0;},
	   'v|version' => sub {version(); exit 0;},
	   'o|output=s' => \$out_file,
	  ) or do {
		print "Invalid usage !\n";
		usage();
		exit 1;
	};

system("mkdir -p $temp_dir/sys/block");
system("mkdir -p $temp_dir/sys/devices/virtual/block");
system("mkdir -p $temp_dir/sys/class");
system("mkdir -p $temp_dir/dev/mapper");
system("rsync -a --inplace /sys/block/dm* /sys/block/sd* $temp_dir/sys/block 2>/dev/null");
system("rsync -a --inplace --exclude=chpd* /sys/devices/css0 $temp_dir/sys/devices 2>/dev/null");
system("rsync -a --inplace /sys/devices/virtual/block/dm* $temp_dir/sys/devices/virtual/block 2>/dev/null");
system("rsync -a --inplace /sys/class/fc_host /sys/class/scsi_device /sys/class/scsi_tape /sys/class/scsi_generic /sys/class/fc_remote_ports $temp_dir/sys/class 2>/dev/null");
store_mapper_devices($temp_dir,"/dev/mapper");
system("tar -czf $out_file.cfg -C $temp_dir sys dev 2>/dev/null");
