#!/bin/bash
#
# GNU/Linux O.S. report generation tool
#
# Copyright (c) 2008-2025 by Silvan Calarco <silvan.calarco@mambasoft.it>
#

MAKEREPORT_VERSION=20250803
TEMPFILE=`mktemp`

. ${ROOT}/etc/sysconfig/machine
. ${ROOT}/etc/os-release

if [ "${SUDO_USER}" == "" ]; then
   # mambareport uses pkexec instead of sudo
   SUDO_USER="\#${PKEXEC_UID}"
fi

[ ! "$SYSTEM_MANUFACTURER" -o "$SYSTEM_MANUFACTURER" = "System manufacturer" ]  && {
   SYSTEM_MANUFACTURER=unknown
}

[ ! "$SYSTEM_PRODUCT_NAME" -o "$SYSTEM_PRODUCT_NAME" = "System Product Name" ]  && {
   SYSTEM_PRODUCT_NAME=unknown
}

[ ! "$SYSTEM_SERIAL_NUMBER" -o "$SYSTEM_SERIAL_NUMBER" = "System Serial Number" ]  && {
   SYSTEM_SERIAL_NUMBER=unknown
}

SYSTEM_MANUFACTURER=`echo $SYSTEM_MANUFACTURER | tr ' ' _ | tr '-' _ | tr '/' _`
SYSTEM_PRODUCT_NAME=`echo $SYSTEM_PRODUCT_NAME | tr ' ' _ | tr '-' _ | tr '/' _`
SYSTEM_SERIAL_NUMBER=`echo $SYSTEM_SERIAL_NUMBER | tr ' ' _ | tr '-' _ | tr '/' _`

REPORTNAME="${NAME}-report-$SYSTEM_MANUFACTURER-$SYSTEM_PRODUCT_NAME-$SYSTEM_SERIAL_NUMBER.txt"

if [ "$1" ]; then
   NOTE="$1"
fi

if [ "$2" ]; then
   REPORTNAME="$2"
fi

function catfile() {

   FILENAME=$1
   FILEDESC=$2

   for c in $FILENAME; do 
      cat >> $TEMPFILE << _EOF

****************************************************************
* File '$c': $FILEDESC
****************************************************************
_EOF

      cat ${ROOT}/$c >> $TEMPFILE
   done

}

function tailfile() {

   FILENAME=$1
   FILEDESC=$2

   for c in $1; do
      cat >> $TEMPFILE << _EOF

****************************************************************
* File '$c' (last 200 lines): $FILEDESC
****************************************************************
_EOF

      tail -n 200 ${ROOT}/$c >> $TEMPFILE
   done

}

function catinitramfs() {

   FILENAME=$1
   FILEDESC=$2

   for c in $1; do
      TMPDIR=`mktemp -d`

      cat >> $TEMPFILE << _EOF

****************************************************************
* Initramfs image '$c': $FILEDESC
****************************************************************
_EOF
      
      pushd $TMPDIR >/dev/null
      gunzip -c ${ROOT}/$c | cpio -i &>/dev/null
      ls -lR . >> $TEMPFILE
      echo "\

************************\
init script in initramfs\
************************" >> $TEMPFILE
      cat init >> $TEMPFILE
      popd >/dev/null
      [ "$TMPDIR" -a "$TMPDIR" != "/" ] && rm -rf $TMPDIR
   done

}

function catcommand() {

   CMDNAME="$1"
   FILEDESC="$2"

   cat >> $TEMPFILE << _EOF

****************************************************************
* Command '$CMDNAME': $FILEDESC
****************************************************************
_EOF

   eval $CMDNAME 2>>$TEMPFILE >> $TEMPFILE

}

echo "${NAME} report generation tool ${MAKEREPORT_VERSION}"
echo "Copyright (c) 2008-2024 by Silvan Calarco <silvan@openmamba.org>"
echo "Released under the terms of the GNU GPL v3 license"
echo

[ $UID = 0 ] || {
   echo "Error: this program must be run as root; exiting."
   exit 1
}


echo "Gathering information..."
cat > $TEMPFILE << _EOF
****************************************************************************
* ${NAME} diagnostic information ${MAKEREPORT_VERSION} for host $HOSTNAME (`date`)
****************************************************************************
_EOF

if [ "$NOTE" ]; then
echo "
REPORTED PROBLEM:
=================" >> $TEMPFILE
echo "$NOTE" >> $TEMPFILE
echo >> $TEMPFILE
fi

# system information
catfile /etc/os-release "O.S. release information"
catfile /etc/sysconfig/machine "machine information from BIOS"
catfile /proc/cpuinfo "processor(s) information"
catfile /proc/meminfo "System memory information"
catcommand "/usr/bin/lspci -nn" "PCI hardware information (short)"
catcommand /usr/bin/lsusb "USB hardware information"
catcommand /usr/sbin/lsmod "Loadel kernel modules"
catcommand "rpm -qa|grep ^kernel-" "Installed kernel packages"
catcommand "ls -l /etc/alternatives" "System alternatives"

# kernel and system logs
catcommand "journalctl --system -b | head -n 1000" "system journal from boot"
catcommand "journalctl --system -n 500" "system journal last lines"
catcommand /bin/dmesg "last kernel messages"

# user log
catcommand "sudo -u ${SUDO_USER} journalctl -b --user | head -n 500" "user journal from startup"
catcommand "sudo -u ${SUDO_USER} journalctl --user -n 500" "user journal last entries"

# process/memory information
catcommand "top -b -n1" "Top running processes"
catcommand "ps aux" "All running processes"

# system boot troubleshooting
catfile /proc/cmdline "kernel startup command line"
catcommand "ls -l /boot" "boot files"
catfile /boot/grub/grub.cfg "GRUB configuration"
catfile "/etc/modprobe.d/*" "modules configuration"
catcommand "lsinitrd /boot/initramfs-`uname -r`.img" "running kernel initial ramdisk"

# system services status
catcommand "systemctl status" "systemctl status"
catcommand "systemctl" "systemctl services status"
catcommand "systemd-analyze blame" "systemd services startup timings"

# Graphical subsystem
catfile /var/log/Xorg.0.log "Xorg startup log"
catfile /etc/X11/xorg.conf "Xorg configuration"
catfile /etc/X11/xorg.conf.d/* "Xorg configuration modules"
catcommand "DISPLAY=:0 /usr/bin/glxinfo" "GLX information"

# Audio
catfile "/proc/asound/card*/codec*" "Audio driver codecs"
catfile "/etc/asound.conf" "Alsa configuration"
catfile "/var/lib/alsa/asound.state" "Alsa mixer settings"

# Network
catcommand "/usr/sbin/ifconfig -a" "Network interfaces list"
catcommand "/usr/sbin/iwconfig" "Wireless interfaces list"
catcommand "/usr/sbin/route -n" "Routing table"
catcommand "systemd-resolve --status" "DNS configuration"

# Disks
for d in /dev/{sd[a-z],nvme[0-9]}; do
   [ -e $d ] && catcommand "smartctl -ia $d" "S.M.A.R.T. status for device $d"
done

# Installation
catcommand "rpm -qa --last | head -n 100" "Last installed packages"
catfile /var/log/Xorg.5.log "Xorg installation test log"

# Other details
catcommand "/usr/bin/lspci -vv" "PCI hardware information (verbose)"

# Systemd coredumps
catcommand "/usr/bin/coredumpctl info -q -r" "Systemd coredumps (more recent first)"

cp $TEMPFILE $REPORTNAME || exit 1
chmod +r $REPORTNAME
rm -f $TEMPFILE

echo "Report saved as $REPORTNAME"
echo "Please visit $BUG_REPORT_URL for instructions to submit this report."
