#!/bin/bash

# Copyright IBM Corp. 2019

VERSION="1.8.5";


function usage() {
	echo;
	echo "Usage: smc_dbg [ OPTIONS ]";
	echo;
	echo "Collect debug information";
	echo;
	echo "   -h, --help           display this message";
	echo "   -t, --tgz            generate .tgz file";
	echo "   -v, --version        display version info";
	echo;
}

function redirect() {
	if [ "$tgz" == "on" ]; then
		exec &>$tmpdir/$1;
	else
		echo;
	fi
}

tgz="off";
ARCH=`uname -m | cut -c1-4`;
args=`getopt -u -o hvt -l help,version,tgz -- $*`;
[ $? -ne 0 ] && exit 1;
set -- $args;
while [ $# -gt 0 ]; do
        case $1 in
        "-h" | "--help" )
                usage;
                exit 0;;
        "-t" | "--tgz" )
                tgz="on";;
        "-v" | "--version" )
		echo "smc_dbg utility, smc-tools-$VERSION";
		exit 0;;
        * )
        esac
        shift;
done
if [ "$tgz" == "on" ]; then
	exec 3>&1 4>&2
	tmpdir=`mktemp -d /tmp/smc_dbg-XXXXXX`;
fi

redirect version.txt;
smcss -v
smc_dbg -v
smc_pnet -v
smc_rnics -v
smc_chk -v
smcd -v
smcr -v

if [ "$ARCH" == "s390" ]; then
	redirect devices.txt;
	echo "CCW Devices:"
	printf "  Device    CHPID  Port  PNET ID\n";
	echo "  -------------------------------------------";
	for device in `ls -1 /sys/bus/ccwgroup/devices`; do
		chpid=`cat /sys/bus/ccwgroup/devices/$device/chpid | tr [A-F] [a-f]`;
		osaport=`cat /sys/bus/ccwgroup/devices/$device/portno`;
		iface=`cat /sys/bus/ccwgroup/devices/$device/if_name`;
		printf "  %8s  %4s   %-4s  %s\n" $device 0x$chpid $osaport `smc_chk -i $iface`;
	done
	echo;

	echo "PCI Devices:"
	smc_rnics | sed 's/^/  /';

	redirect smcss_smcd;
	smcss --smcd;
fi

redirect smcss_all.txt;
smcss --all --debug;

redirect smcss_smcr;
smcss --smcr;

redirect pnet_table.txt;
smc_pnet --show;

redirect smcr_links.txt;
smcr -d linkgroup link-show;

redirect smcd_lgs.txt;
smcd -d linkgroup show;

redirect smcd_info.txt;
smcd info;

redirect smcd_stats.txt;
smcd -d stats;

redirect smcr_stats.txt;
smcr -d stats;


if [ "$tgz" == "on" ]; then
	exec >&3 2>&4
	cd /tmp;
	tar cvfz $tmpdir.tgz `basename $tmpdir` >/dev/null 2>&1;
	rm -rf $tmpdir;
	echo "Debug output written to $tmpdir.tgz";
fi
exit 0;
