#!/bin/sh
########################################################################
#
# Description : show_event_log
#
# Authors     : Based on Open Suse Udev Rules
#               kay.sievers@suse.de
#
# Adapted to  : Jim Gifford
# LFS
#
# Version     : 00.00
#
# Notes       :
#
########################################################################
#
# extract udev log messages from a syslog file
# show_event_log <first> [<last> [<list of event types>] ]
#
# known events are:
#   ac97 acpi block cpu drivers edd firmware graphics i2c-adapter i8259
#   ieee1394_protocol input ioapic irqrouter lapic mem misc module namespace
#   pci_bus pci_express pcmcia pcmcia_socket platform pnp printer scsi scsi_host
#   serio sound timer timer_pit tty usb usb_device usb_host vc
#
# GPL, version 2
# Christian Zoz <zoz@suse.de>
#

EXCLUDE_SUBS="acpi cpu edd mem namespace pci_bus tty vc"
MESSAGES=${MESSAGES:-/var/log/messages}

show_event() {
	local SN PID SUBS
	declare -i SN=$1 PID
	read PID ACT SUBS < <(sed -n "s/^.*seq $SN forked, pid \[\([0-9]*\)\], '\([^']*\)' '\([^']*\)'.*$/\1 \2 \3/p;T;q" $MESSAGES)
	if [ $PID == 0 ] ; then
		echo event $SN not found 1>&2
		return 1
	fi
	if [ -n "$WANTED_SUBS" ] ; then
		WANTED=no
		for S in $WANTED_SUBS; do
			test "$SUBS" == "$S" && WANTED=yes
		done
	else
		WANTED=yes
		for S in $EXCLUDE_SUBS; do
			test "$SUBS" == "$S" && WANTED=no
		done
	fi
	echo event $SN $PID $ACT $SUBS $WANTED 1>&2
	test $WANTED == no && return 1
	echo " _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $SN _ $ACT _ $SUBS _ _ _ _"
	grep "seq $SN \|\[$PID\]" $MESSAGES |
		grep -v "pass_env\|geventrec\|hal.hot"
	echo
}

declare -i START=$1 STOP=$2
if [ "$2" == last ] ; then
	read STOP_STRING < <(grep -o "udev_done.*exit" $MESSAGES | cut -d" " -f3 | sort -n | tail -1)
	STOP=`echo $STOP_STRING | sed 's/^[^0-9]*\([0-9]\+\).*$/\1/'`
fi
test $START -lt 0 -a $STOP -gt 0 && START=$((STOP+START+1))
test $START -gt $STOP && STOP=$START
shift; shift; WANTED_SUBS="$*"
for n in `seq $START $STOP`; do
	show_event $n
done

exit

# list all events
(read x; while read x s x x x c x; read x s2 x a d; do echo $s $s2 $a $c $d; done) < <( sed -n "s/^.*udev.*\(seq [0-9]* [qf].*$\)/\1/p" /var/log/messages | sort -n -k 2 | tr -d ',' )

./show_event_log 1 last > boot.events.log.`date +%A_%H:%M` 2> boot.events.list.`date +%A_%H:%M`
