#!/bin/bash

# Copyright IBM Corp. 2016, 2020

qcbin="zname";
if [ $# -eq 1 ]; then
	qcbin="$1";
fi

if [ ! -x $qcbin ]; then
	echo "Error: $qcbin not found or not executable";
	echo "Note: You can specify an arbitrary executable as an argument to qc_dump";
	exit 2;
fi

echo "Executing $qcbin...";
QC_DEBUG=2 $qcbin >/tmp/ref_result.txt;

if [ $? -ne 0 ]; then
	echo "$qcbin failed - good thing we're creating a dump";
fi


echo "Adding further content...";
cd /tmp
dump="`ls -rtd qclib-??????.dump-1 2>/dev/null | tail -1`";
if [ "$dump" == "" ]; then
	echo "Error: No dump data found, sorry";
	exit 3;
fi
mv ref_result.txt $dump;
lscpu -e			> $dump/lscpu.output;
hostname			> $dump/hostname.output;
tgt=${dump%.*}.tgz;
if [ -e /dev/vmcp ]; then
	vmcp QUERY MULTITHREAD	> $dump/QUERY_MULTITHREAD.output;
fi
echo "Creating package...";
tar cvfz $tgt $dump| sed -e 's/^/  /g';

echo "Dump written to $PWD/$tgt";

exit 0;
