#!/bin/bash
#
#  niSystemReport
#     This script checks different things about the system and is meant as a
#     means of reporting vitals of the system to National Instruments when
#     running into problems and attempting to diagnose them.
#
#  version 15.0.1f0
#
#   Copyright 2005,
#  National Instruments Corporation.
#  All Rights reserved.
#
#  originated:  21 June 2005
#

niSystemReportVersion=15.0.1f0
nikalDir=`cat /etc/natinst/nikal/nikal.dir`
statusFail=1
statusSuccess=0

reportFile()
{
   if [ -e $1 ]; then
      echo " "
      echo "********************************************************************************"
      echo "$1:"
      cat $1
      echo "********************************************************************************"
   else
      echo "$1: File not found!!!"
   fi
}

reportCommand()
{
   if [ -e $1 ]; then
      echo " "
      echo "********************************************************************************"
      echo "$*:"
      $* 2>&1
      echo "********************************************************************************"
   else
      echo "$1: Command not found!!!"
   fi
}

reportLibrary()
{
   if [ -e $1 ]; then
      echo " "
      echo "********************************************************************************"
      echo "$1:"
      strings $1 | grep nNIVersion
      echo "********************************************************************************"
   else
      echo "$1: Library not found!!!"
   fi
}

# must be root
if [ `id -ur` != 0 ]; then
   echo "Please run $0 as root"
   exit $statusFail
fi

echo "$0 version $niSystemReportVersion"
if which uname >/dev/null 2>&1; then
   reportCommand `which uname` -a
fi

for releaseFile in `ls /etc/*-release`
do
   reportFile $releaseFile
done

cd $nikalDir/src/nikal
if [ ! -e $nikalDir/src/nikal/Makefile.in ]; then
   ./configure
fi

make clean >/dev/null 2>&1
echo " "
echo "********************************************************************************"
echo "$nikalDir/src/nikal make:"
make 2>&1
echo "********************************************************************************"
cd -

if which tail >/dev/null 2>&1; then
   reportCommand `which tail` --lines=25 /var/log/messages
fi

reportFile $nikalDir/src/nikal/Makefile.in
. $nikalDir/src/nikal/Makefile.in

reportCommand $CC -v
reportFile /proc/version

# NI Kernel Modules
for kernelModule in `find $MODPATH -type f`
do
   reportLibrary $kernelModule
done

# NI User Libraries
natinstDir=${nikalDir%\/nikal}
for libDir in `find $natinstDir -name lib`
do
   for userLibrary in `find $libDir -type f`
   do
      reportLibrary $userLibrary
   done
done

# NI PAL User Daemon Services
if [ -e /etc/natinst/nipal/nipal.dir ]; then
   nipalDir=`cat /etc/natinst/nipal/nipal.dir`
   for userDaemon in `find $nipalDir/etc/services -type f`
   do
      reportLibrary $userDaemon
   done
fi

reportFile /proc/modules
if which ps >/dev/null 2>&1; then
   reportCommand `which ps` awux
fi

reportFile /proc/cpuinfo
reportFile /proc/meminfo
reportFile /proc/devices
reportCommand ls -l /dev/nipalk

reportCommand /sbin/lspci -x -v
reportCommand /usr/bin/lspci -x -v
reportCommand /sbin/lspci -t -v
reportCommand /usr/bin/lspci -t -v
reportCommand /sbin/lsusb -v
reportCommand /usr/sbin/lsusb -v
reportCommand /sbin/lsusb -t
reportCommand /usr/sbin/lsusb -t

reportFile /proc/interrupts
reportFile /proc/iomem
reportFile /proc/bus/pci/devices

reportCommand /bin/dmesg

reportFile $KERNELDIRECTORY/.config
reportFile /proc/kallsyms
reportFile /proc/ksyms

