#!/bin/bash

#  Gnome15 - Suite of tools for the Logitech G series keyboards and headsets
#  Copyright (C) 2011 Brett Smith <tanktarta@blueyonder.co.uk>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

#
# Simple script to gather as much information about the environmen
# Gnome15 is running in as possible.
#
# Sorry it's a bit untidy, it will improve :)

# Check running as root
if [ $(id -u) != 0 ]
then echo "$0: you should run that as root using either 'sudo $0' or 'su -c $0'" >&2
     exit 1
fi

separator() {
    echo "------------------------------------------------------------"
}

# System details
echo -e "System Details\n"
uname -a
if [ -f /etc/lsb-release ]
then cat /etc/lsb-release
fi
echo
cat /proc/cpuinfo
echo
cat /proc/meminfo

# Gnome15 packages
echo -e "Gnome15 Packages\n"
if which dpkg >/dev/null 2>&1
then echo -e "Debian based packaging found\n"
     dpkg -l 'gnome15*' 'pylibg19*' 'lg4l*' 'python-uinput*' 'python-inputevent*' 'libsuinput*' 'libg15*' 'g15*' 2>/dev/null
fi
if which rpm >/dev/null 2>&1
then echo -e "RPM based packaging found\n"
     rpm -qa 'gnome15*' 'pylibg19*' 'lg4l*' 'python-uinput*' 'python-inputevent*' 'libsuinput*' 'libg15*' 'g15*' 2>/dev/null
fi
separator

# lsusb
echo -e "USB Device Summary (lsusb)\n"
lsusb 
echo -e "\nUSB Device Details (lsusb -v)\n"
lsusb -v
separator

# kernel modules
echo -n "Kernel modules :"
mods=$(lsmod|awk '{ print $1 }'|grep "hid_"|sort -u)
if [ -z "${mods}" ]
then echo "No kernel modules used"
else echo "${mods}"
     echo -e "\nFrame buffers: "
     ls -l /dev/fb*
     echo -e "\nInput Devices: "
     for i in /dev/input/by-id/*
     do
         linked_to=$(ls -l $i|awk '{ print $10 }')
         linked_to_name=$(basename $linked_to)
         linked_to_file=/dev/input/$linked_to_name
         linked_to_details=$(ls -l $linked_to_file|awk '{ print $1, $3, $4 }')
         echo $(basename $i)" -> ${linked_to_name} ( ${linked_to_details} )"
     done
fi
separator
if [ -f /etc/default/lg4l-linux ]
then echo "/etc/default/lg4l-linux contents :-"
     cat /etc/default/lg4l-linux
     separator
fi

# USB device permissions (for g15direct/g19direct)
echo -n "USB device permissions"
ls -lR /dev/bus/usb
separator

if [ -d /sys/class/leds ]
then echo -n "LED files (/sys/class/leds)"
     ls -l /sys/class/leds
     separator
fi
     

if [ -d /sys/class/graphics ]
then echo "Frame buffer information"
     for i in /sys/class/graphics/*
     do
        echo "$i ->"
        pushd $i >/dev/null
        if [ -f name ]; then
            echo "    Name : "$(cat name)
        fi
        if [ -f mode ]; then
            echo "    Mode : "$(cat mode)
        fi
        if [ -f modes ]; then
            echo "    Modes: "$(cat modes)
        fi
        if [ -f bits_per_pixel ]; then
            echo "    BPP  : "$(cat bits_per_pixel)
        fi
        ls -l|awk '{ print "\t" $0 }' 
        popd >/dev/null
     done
     separator
fi

if [ -d /sys/bus/usb/drivers ]
then echo "Drivers bound to USB devices"
     find /sys/bus/usb/drivers
     separator
 fi
