#!/bin/bash

#
# lsdevmod for KAP - Kernel Autoconfig Project 
# (alfa-stage)
#
# $ usage: lsdevmod [update]
#
# Andrea "bunker" Purificato
# bunker A.T. fastwebnet D.O.T. it
#
# da un'idea di Emiliano "marte" Di Marzio
#

LSPCI=$(env PATH="${PATH}:/bin:/sbin:/usr/sbin" which lspci)
URLIDS="http://kmuto.jp/svn/hcl/trunk/prog/modules.pcimap"

DEFDIR="$HOME/.lsdevmod"
TMPD="$DEFDIR/modules.pcimap"
TMPL="$DEFDIR/localids"
TMPM="$DEFDIR/modulesids"
if [ ! -d $DEFDIR ]; then 
    mkdir $DEFDIR
fi

update() {
    wget -O $TMPD $URLIDS &&\
    $LSPCI -n|grep -o '[a-z0-9]\{4\}:[a-z0-9]\{4\}'|sed s/://g > $TMPL &&\
    cat $TMPD|awk '{print $1,$2$3}'|sed s/0x0000//g > $TMPM &&\
    rm $TMPD
}

list() {
    if [ -e $TMPL ] && [ -e $TMPM ]; 
    then
	for i in `cat $TMPL`; do grep $i $TMPM; done
    else
	echo "Try with \"$0 update\" first!"
	exit -1
    fi
}

case "$1" in
   update)
      echo -e "Creating pcids db from $URLIDS...\n"
      update
      echo "Now try with: $0"
      ;;
   *)
      list
      ;;
esac
exit 0

