#!/bin/bash

TMPDIR="/tmp/hwcheck"
CENTOSPACKAGELIST="tar numactl ethtool util-linux lshw dmidecode pciutils usbutils coreutils"
UBUNTUPACKAGELIST="tar numactl ethtool fdisk util-linux lshw dmidecode pciutils usbutils coreutils"


for i in $(ls /sys/class/net | sort); do if [[ "$i" != "lo" ]]; then mac="$(ethtool -P $i | awk '{print $3}')" ; break; fi ; done
mac=$(tr -d ':' <<<$mac)

filename="hwcheck-${mac}.tar.gz"


if [ -f "$filename" ]; then
	echo "File already exists: $filename"
	exit 0
fi


echo -e "\nChecking required packages\n"

if grep -i ubuntu /etc/*-release > /dev/null 2>&1; then
	sudo apt-get -qq update
	sudo DEBIAN_FRONTEND=noninteractive apt-get install -qy $UBUNTUPACKAGELIST
elif grep -i centos /etc/*-release > /dev/null 2>&1; then
	sudo yum install -y -q $CENTOSPACKAGELIST
fi


echo -e "\nGenerating report\n"

mkdir -p "$TMPDIR"

sudo dmesg > "$TMPDIR"/dmesg.txt
sudo lshw > "$TMPDIR"/lshw.txt
sudo dmidecode > "$TMPDIR"/dmidecode.txt
sudo lspci -nnvvvv > "$TMPDIR"/lspci.txt
sudo lsusb -vvv > "$TMPDIR"/lsusb.txt
sudo cat /proc/cpuinfo > "$TMPDIR"/cpuinfo.txt
sudo cat /proc/meminfo > "$TMPDIR"/meminfo.txt
sudo numactl --hardware > "$TMPDIR"/numactl.txt
sudo lsblk > "$TMPDIR"/lsblk.txt
sudo fdisk -l > "$TMPDIR"/fdisk.txt

for i in $(ls /sys/class/net); do if [[ "$i" != "lo" ]]; then sudo ethtool $i; fi ; done > "$TMPDIR"/ethtool.txt


echo -e "\nCompressing results\n"

tar -czpf "$filename" -C "$TMPDIR" .

echo -e "\nDone!  Results in: $filename\n"
