#!/bin/bash

ARCH=$arch
LIBPATH=""
DEVICE=ueyed
STDOUT=/dev/stdout
STDERR=/dev/stderr
## CREATE A REPORT FOR DEBUGGING PURPOSES

separator()
{
  echo
  echo "--------------------------------------------------------------------------------------------------"
  echo
  echo "   -->  < " $1 " >"
  echo
}

message()
{
  echo
  echo "*** < " $1 " > ***"
  echo
}

usage()
{
    echo "Usage: ${1} [ -o file ]"
    echo "Options:"
    echo "    -o file           print output to 'file'. If omitted, stdout is used."
    exit ${2}
}

APPEND=0

while getopts 'ao:' OPTION
do
    case $OPTION in
        a)  APPEND=1;;
        o)  STDOUT=$OPTARG
            STDERR=$OPTARG
            ;;
        \?) echo "Unknown option \"-$OPTARG\"." >&2
            usage ${0} $EXIT_ERROR;;
        :)  echo "Option \"-$OPTARG\" requires an argument." >&2
            usage ${0} $EXIT_ERROR;;
        *)  echo "Fatal option error ...\"$OPTION\"... " >&2
            usage ${0} $EXIT_BUG;;
    esac
done

# get libpath
if [ ARCH = "x86_64" ]
then
    LIBPATH="/lib64"
else
    LIBPATH="/lib"
fi


if test $APPEND -eq 0
then
    rm -Rf $STDOUT
fi

# dump header
separator "report created on $(date)" >> $STDOUT 2>> $STDERR
echo "Send to your local distributors support or: support@ids-imaging.de" >> $STDOUT 2>> $STDERR
echo "Homepage: https://www.ids-imaging.com for driver updates" >> $STDOUT 2>> $STDERR

# dump current user
separator "user information" >> $STDOUT 2>> $STDERR
message "current user: $(whoami)" >> $STDOUT 2>> $STDERR
message "groups: $(groups $(whoami))" >> $STDOUT 2>> $STDERR

# dump system limitations
separator "system limits" >> $STDOUT 2>> $STDERR
ulimit -a >> $STDOUT 2>> $STDERR

# dump setup package version
if [ -e ./VERSION ]
then
    separator "ueye SDK version" >> $STDOUT 2>> $STDERR
    cat ./VERSION >> $STDOUT 2>> $STDERR
fi

# dump linux version
separator "Linux version" >> $STDOUT 2>> $STDERR
cat /proc/version >> $STDOUT 2>> $STDERR

#dump distribution version
separator "Linux distribution version" >> $STDOUT 2>> $STDERR
cat /etc/issue >> $STDOUT 2>> $STDERR

# dump gcc version
separator "GCC version information" >> $STDOUT 2>> $STDERR
echo "GNU compiler collection: " `gcc -v 2>&1` >> $STDOUT 2>> $STDERR
echo "Glibc:                   " `for LIBCSO in $(find $LIBPATH -name libc.so.6); do echo -n "Found '$LIBCSO': "; $LIBCSO | head -n 1; done` >> $STDOUT 2>> $STDERR
echo "X Windows:               " `X -version 2>&1 | grep "Version"` >> $STDOUT 2>> $STDERR

# gather network information
separator "network information" >> $STDOUT 2>> $STDERR
ifconfig  >> $STDOUT 2>> $STDERR

for interface in `ls /sys/class/net`; do
    speed=`cat /sys/class/net/$interface/speed     2>/dev/null`
    state=`cat /sys/class/net/$interface/operstate 2>/dev/null`
    printf "%s\n  state: %s\n  speed: %s\n\n" $interface $state $speed >> $STDOUT 2>> $STDERR
done

# dump some cpu infos
separator "CPU information" >> $STDOUT 2>> $STDERR
cat /proc/cpuinfo >> $STDOUT 2>> $STDERR

# dump some memory information
separator "Memory information" >> $STDOUT 2>> $STDERR
cat /proc/meminfo >> $STDOUT 2>> $STDERR

# dump Linux command line
separator "Linux run with command line" >> $STDOUT 2>> $STDERR
cat /proc/cmdline >> $STDOUT 2>> $STDERR

# show PCI devices
separator "PCI devices" >> $STDOUT 2>> $STDERR
lspci >> $STDOUT 2>> $STDERR

# show USB devices
separator "USB devices" >> $STDOUT 2>> $STDERR
lsusb >> $STDOUT 2>> $STDERR

# dump loaded kernel modules
separator "Kernel modules" >> $STDOUT 2>>$STDERR
lsmod >> $STDOUT 2>>$STDERR

# dump network configuration
separator "network configuration" >> $STDOUT 2>> $STDERR
sysctl -q net.core.netdev_max_backlog >> $STDOUT 2>> $STDERR
sysctl -q net.core.rmem_max >> $STDOUT 2>> $STDERR
sysctl -q net.core.rmem_default >> $STDOUT 2>> $STDERR
netstat -su  >> $STDOUT 2>> $STDERR

# dump shm access rights
separator "access to /dev/shm" >> $STDOUT 2>> $STDERR
if [ -e /dev/shm ]
then
    ls -ld /dev/shm >> $STDOUT 2>> $STDERR
else
    echo "/dev/shm doesn't exist." >> $STDOUT 2>> $STDERR
fi

# dump daemon runtime dir access rights
separator "access to /var/run/ueyed" >> $STDOUT 2>>$STDERR
if [ -e /var/run/ueyed ]
then
    ls -ld /var/run/ueyed >> $STDOUT 2>>$STDERR
    ls -l /var/run/ueyed >> $STDOUT 2>>$STDERR
else
    echo "/var/run/ueyed doesn't exist." >> $STDOUT 2>>$STDERR
fi

# dump running ueye software
separator "running ueye software" >> $STDOUT 2>>$STDERR
ps aux | grep ueye >> $STDOUT 2>>$STDERR

# dump mount options
separator "mount options" >> $STDOUT 2>>$STDERR
mount >> $STDOUT 2>>$STDERR

# dump mounted filesystems
separator "mounted filesystems" >> $STDOUT 2>>$STDERR
df -ah >> $STDOUT 2>>$STDERR

# show udev rules file
separator "udev rules" >> $STDOUT 2>>$STDERR
if [ -e /etc/udev/rules.d/zz-ueyeusb.rules ]
then
    cat /etc/udev/rules.d/zz-ueyeusb.rules >> $STDOUT 2>> $STDERR
else
    echo "/etc/udev/rules.d/zz-ueyeusb.rules does not exist." >> $STDOUT 2>> $STDERR
fi

# show udev rules directory
separator "udev rules directory" >> $STDOUT 2>>$STDERR
if [ -d /etc/udev/rules.d ]
then
    ls -ld /etc/udev/rules.d >> $STDOUT 2>> $STDERR
    ls -l /etc/udev/rules.d >> $STDOUT 2>> $STDERR
    ls -lL /etc/udev/rules.d >> $STDOUT 2>> $STDERR
else
    echo "/etc/udev/rules.d does not exist." >> $STDOUT 2>> $STDERR
fi

# dump ueye access rights
separator "/dev/ueye: content and access" >> $STDOUT 2>> $STDERR
if [ -e /dev/ueye ]
then
    ls -ld /dev/ueye >> $STDOUT 2>> $STDERR
    ls -l /dev/ueye >> $STDOUT 2>> $STDERR
    ls -lL /dev/ueye >> $STDOUT 2>> $STDERR
else
    echo "/dev/ueye doesn't exist." >> $STDOUT 2>> $STDERR
fi

# dump uEye access rights
separator "/dev/uEye: content and access" >> $STDOUT 2>> $STDERR
if [ -e /dev/uEye ]
then
    ls -ld /dev/uEye >> $STDOUT 2>> $STDERR
    ls -l /dev/uEye >> $STDOUT 2>> $STDERR
    ls -lL /dev/uEye >> $STDOUT 2>> $STDERR
else
    echo "/dev/uEye doesn't exist." >> $STDOUT 2>> $STDERR
fi

# dump user/group information
separator "user and group information" >> $STDOUT 2>> $STDERR
[ -e /usr/bin/id ] && /usr/bin/id ueyed >> $STDOUT 2>> $STDERR
[ -e /usr/bin/groups ] && /usr/bin/groups ueyed >> $STDOUT 2>> $STDERR

# dump user/group information from /etc/passwd and /etc/group
separator "user and group information from files" >> $STDOUT 2>> $STDERR
cat /etc/passwd | grep ueye >> $STDOUT 2>> $STDERR
cat /etc/group | grep ueye >> $STDOUT 2>> $STDERR

# show installed uEye libraries
separator "installed libueye_api versions" >> $STDOUT 2>> $STDERR
find /usr/lib -name libueye_api* -exec ls -l {} \; >> $STDOUT 2>> $STDERR

# dump dependencies of libueye_api
separator "libueye_api* dependencies" >> $STDOUT 2>> $STDERR
find /usr/lib -name libueye_api* -exec echo "++ {} dependencies ++" \; -exec     ldd -v -d {} \; >> $STDOUT 2>> $STDERR

# dump ueyed versions, dependencies, configuration and status
if  [ -e /usr/bin/ueyeusbd ]
then
    separator "ueyeusbd version" >> $STDOUT 2>> $STDERR
    /usr/bin/ueyeusbd -v >> $STDOUT 2>> $STDERR
    separator "ueyeusbd dependencies" >> $STDOUT 2>> $STDERR
    ldd -v -d /usr/bin/ueyeusbd >> $STDOUT 2>> $STDERR
    if [ -e /etc/ids/ueye/ueyeusbd.conf ]
    then
        separator "ueyeusbd configuration file" >> $STDOUT 2>> $STDERR
        cat /etc/ids/ueye/ueyeusbd.conf >> $STDOUT 2>> $STDERR
    fi
    if [ -e /etc/init.d/ueyeusbdrc ]
    then
        separator "ueyeusbd status" >> $STDOUT 2>> $STDERR
        /etc/init.d/ueyeusbdrc status >> $STDOUT 2>> $STDERR
    else
        message "/etc/init.d/ueyeusbdrc not found!" >> $STDOUT 2>> $STDERR
    fi
else
    message "No USB uEye daemon available." >> $STDOUT 2>> $STDERR
fi
if  [ -e /usr/bin/ueyeethd ]
then
    separator "ueyeethd version" >> $STDOUT 2>> $STDERR
    /usr/bin/ueyeethd -v >> $STDOUT 2>> $STDERR
    separator "ueyeethd dependencies" >> $STDOUT 2>> $STDERR
    ldd -v -d /usr/bin/ueyeethd >> $STDOUT 2>> $STDERR
    if [ -e /etc/ids/ueye/ueyeethd.conf ]
    then
        separator "ueyeethd configuration file" >> $STDOUT 2>> $STDERR
        cat /etc/ids/ueye/ueyeethd.conf >> $STDOUT 2>> $STDERR
    fi
    if [ -e /etc/init.d/ueyeethdrc ]
    then
        separator "ueyeethd status" >> $STDOUT 2>> $STDERR
        /etc/init.d/ueyeethdrc status >> $STDOUT 2>> $STDERR
    else
        message "/etc/init.d/ueyeethdrc not found!" >> $STDOUT 2>> $STDERR
    fi
else
    message "No GigE uEye daemon available." >> $STDOUT 2>> $STDERR
fi

# list connected ueye devices
if [ -e /usr/bin/ueyesetid ]
then
    separator "connected cameras" >> $STDOUT 2>> $STDERR
    /usr/bin/ueyesetid -d >> $STDOUT 2>> $STDERR
fi

# extract ueyed errors from logs...
separator "Latest uEye messages /var/log/messages (limit: 100)" >> $STDOUT 2>> $STDERR
[ -e /var/log/messages ] && cat /var/log/messages | grep ueye | tail -n 100 >> $STDOUT 2>> $STDERR
separator "Latest uEye messages of /var/log/syslog (limit: 100)" >> $STDOUT 2>> $STDERR
[ -e /var/log/syslog ] && cat /var/log/syslog | grep ueye | tail -n 100 >> $STDOUT 2>> $STDERR

separator "Latest USB messages /var/log/messages (limit: 100)" >> $STDOUT 2>> $STDERR
[ -e /var/log/messages ] && cat /var/log/messages | grep usb | tail -n 100 >> $STDOUT 2>> $STDERR
separator "Latest USB messages of /var/log/syslog (limit: 100)" >> $STDOUT 2>> $STDERR
[ -e /var/log/syslog ] && cat /var/log/syslog | grep usb | tail -n 100 >> $STDOUT 2>> $STDERR

# show usb related kernel logs
separator "USB kernel log entries" >> $STDOUT 2>> $STDERR
dmesg | grep usb >> $STDOUT 2>> $STDERR
