#!/bin/bash

# Copyright (C) 2004-2007 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This script discover evdev mice and keyboards and also discovers video cards
# on the machine.
# For input devices, it uses /proc/bus/input/devices
# For video cardes, it uses the "discover" package.

# TODO: find a decent way to do all this.

# This function prints the physical addresses of the mice found
function discover_input () {

    KEYBOARDS=($(hal-find-by-capability --capability input.keyboard))
    MICE=($(hal-find-by-capability --capability input.mouse))

    for i in ${KEYBOARDS[@]}; do
	EVDEV_NODE=$(hal-device $i | grep linux.device_file | cut -d"'" -f2)
        echo -e "kbd\t${i}"
	echo -e "kevdev\t${EVDEV_NODE}"
    done
    for i in ${MICE[@]}; do
	EVDEV_NODE=$(hal-device $i | grep linux.device_file | cut -d"'" -f2)
        echo -e "mouse\t${i}"
	echo -e "mevdev\t${EVDEV_NODE}"
    done
}

# Prints bus address and drivers of the video cards.
function video_cards () {

    IDS_BY_DRIVER=/usr/share/xserver-xorg/pci

    # 'discover' prints in the reverse order of lspci, I think
    BUS_IDS=($(lspci | grep VGA | cut -d' ' -f1 | tac))

    for (( i=0; i < ${#BUS_IDS[@]}; i++ )); do
        PCI_DEVICE=$(lspci -n -s ${BUS_IDS[i]} | cut -d' ' -f3)
        # We might find multiple drivers. Use the first.
        DRIVERS[i]=$(grep -i ${PCI_DEVICE/:/} $IDS_BY_DRIVER/* | \
                     cut -d'/' -f6 | cut -d'.' -f1 | head -n 1)
    done

    for (( i=0 ; i < ${#BUS_IDS[@]}; i++ )) ; do
        # busid from lspci is in format 00:00.00
        # below we split in 00 and 00.00
        NUMS=(`echo ${BUS_IDS[$i]} |  \
              awk 'BEGIN {FS=":"}{print toupper($1), toupper($2)}'`)
        # now, we split 00.00 in 00 and 00
        SEC_NUMS=(`echo ${NUMS[1]} |  \
                  awk 'BEGIN {FS="."}{print toupper($1), toupper($2)}'`)
        # now, we convert the numbers from hexa to decimal base
        echo -e "bus\t`echo "obase=10;ibase=16;${NUMS[0]};${SEC_NUMS[0]};${SEC_NUMS[1]};" | bc | paste -s -d":"`"

    done

    for i in ${DRIVERS[@]}; do
        echo -e "driver\t$i"
    done


} # video_cards

# ******************** MAIN *************************

if [[ "$#" = 0 ]]
then
    ARG=all  
else
    ARG=$1
fi


case $ARG in
    all)
        CARDS=`video_cards`
        INPUT=`discover_input`
        echo "$INPUT" | grep mouse
        echo "$INPUT" | grep mevdev
        echo "$INPUT" | grep kbd
        echo "$INPUT" | grep kevdev
        echo "$CARDS"
        ;;
    all2)
	CARDS=`video_cards`
	INPUT=`discover_input`
        echo "$INPUT"
	echo "$CARDS"
	;;

    mouse|mevdev|kbd|kevdev)
	INPUT=`discover_input`
        echo "$INPUT" | grep $ARG
        ;;
    bus|driver)
	CARDS=`video_cards`
        echo "$CARDS" | grep $ARG
        ;;
    cards)
	CARDS=`video_cards`
        echo "$CARDS"
        ;;

    #debug)
    #    echo "$INPUT" | egrep "(PHYS|HANDLER|NAME|--)"
    #	;;
    *)
        echo "$1: unrecognized argument"
	echo "Valid parameters: "
	echo "mouse:  prints the hal ids of the mice"
	echo "mevdev: prints the evdev mouse events"
	echo "kbd:    prints the hal ids of the keyboards"
	echo "kevdev: prints the evdev keyboard events"
	echo "driver: prints the video card drivers"
	echo "cards:  prints the video card bus IDs"
	echo "all:    prints everything"
        ;;
esac
