#!/bin/bash


#==============================================#
#=========== Basic Bugreport Script ===========#
#==============================================#
# Collects data about hardware, installed      #
# packages and power usage, and puts it in an  #
# archive				       #
# 					       #
#==============================================#


if [ "$(id -u)" != "0" ]; then
    echo "Not executed by root, re-executing as root...."
    echo "Please enter the root password...."
    exec su -c "$0"
    exit 0
fi

echo "Preparing bug report archive...."

#makes sure there is no temp directory
if [ -e bugtemp ] ; then
  rm -rf bugtemp
fi
 
#make temp directoy 
mkdir bugtemp
cd bugtemp

#Install needed stuff
yum -y install hwinfo powertop >/dev/null

# get dmesg info first since hwinfo screws up output
echo -e "Linux kernel params used on boot: \n" > dmesg.txt
dmesg | grep "Command line:" | sed 's/vg_[A-Za-z0-9_-]*[/-]/XXXXXXXX\//g' >> dmesg.txt
echo -e "\nLast 100 lines of dmesg\n" >> dmesg.txt
dmesg | tail -n100 >> dmesg.txt

#Get yum installed info
echo -e "All yum installed packages: \n\n" > yum_installed.txt
yum list installed >> yum_installed.txt


#Get yum repo info
yum repolist > yum_repolist.txt




# Run hwinfo + edit possible identifying info
hwinfo --all | sed -e 's/Unique ID:.*//' -e 's/Parent ID:.*//' \
		   -e 's/HW Address.*//' -e 's/Serial ID:.*//' \
		   -e 's/Serial:.*//' -e 's/Device Files:.*//' \
		   > hwinfo.txt


#run powertop
powertop --html=powertop-dump.html >/dev/null 2>&1


#Make unique name
BUG="bugreport-$(date +%F-%H:%M:%S)-$(echo "${USER}-$(hostname)" | shasum | awk '{printf $1}')"

#create tar archive
tar czvf ../$BUG.tar.gz hwinfo.txt powertop-dump.html yum_installed.txt yum_repolist.txt dmesg.txt

#Make proper permissions
chown $(logname): ../$BUG.tar.gz

#cleanup
cd ..
rm -rf bugtemp

echo "Prepare your bugreport by writing an as detailed description of the issue as you can, and posting it to the appropriate forum section."
echo "Please attach $BUG.tar.gz to your bugreport on the forums"
