#!/bin/sh
#extracts usb and pcmcia information out of the pci.ids file.

DESTPCIUSB="pci-usb.ids"
DESTPCIPCMCIAUSB="pci-usb-pcmcia.ids"
rm -f $DESTPCIUSB
rm -f pci-usb-pcmcia.ids

echo "Processing pci.ids, creating"
echo "$DESTPCIUSB and $DESTPCIPCMCIAUSB ..."

# [ ! "`echo -n "$ONELINE" | grep '^#'`" = "" ] && continue #comment
# [ "$ONELINE" = "" ] && continue #blank line
# [ "`echo -n $ONELINE | grep '[0-9a-zA-Z]'`" = "" ] && continue #whitespace only.
cat pci.ids  | grep -v '^#' | grep '[a-zA-Z0-9]' > /tmp/pci.ids
sync
cp -f /tmp/pci.ids pci.ids
sync


NUMLINES=`wc -l pci.ids | tr -s " " | cut -f 2 -d " "`
CNT=1

while [ $CNT -le $NUMLINES ];do

 echo "Processing line $CNT of $NUMLINES ..."
 ONELINE="`head -n $CNT pci.ids | tail -n 1`"

 #stuff at end of file relates generic device numbers to names... need it...
 [ ! "`echo -n $ONELINE | grep '^C\W\+'`" = "" ] && CGENERIC="yes"
 if [ "$CGENERIC" = "yes" ];then
  #write everything at end of file as-is...
  echo "$ONELINE" >> $DESTPCIUSB
  echo "$ONELINE" >> $DESTPCIPCMCIAUSB
  CNT=`expr $CNT + 1`
  continue
 fi

 NEXTVENDOR="`echo -n "$ONELINE" | grep '^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\W\+'`"
 if [ ! "$NEXTVENDOR" = "" ];then
  UVENDOR="$ONELINE"
  UPVENDOR="$ONELINE"
 fi

 SUBLINE="`echo -n "$ONELINE" | grep '^\W[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\W\+'`"
 if [ ! "$SUBLINE" = "" ];then
  USUBLINE="$ONELINE"
  UPSUBLINE="$ONELINE"
 fi

 if [ "$NEXTVENDOR" = "" ];then
  if [ ! "`echo -n "$ONELINE" | grep -i "usb"`" = "" ];then
   [ ! "$UVENDOR" = "" ] && echo "$UVENDOR" >> $DESTPCIUSB
   #test for sub-sub-line...
   if [ ! "`echo -n "$ONELINE" | grep '^\W\W[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\W\+'`" = "" ];then
    [ ! "$USUBLINE" = "" ] && echo "$USUBLINE" >> $DESTPCIUSB
   fi
   USUBLINE=""
   echo "$ONELINE" >> $DESTPCIUSB
   UVENDOR=""
  fi
 fi

 if [ "$NEXTVENDOR" = "" ];then
  if [ ! "`echo -n "$ONELINE" | grep -i --extended-regexp "usb|pcmcia|cardbus"`" = "" ];then
   [ ! "$UPVENDOR" = "" ] && echo "$UPVENDOR" >> $DESTPCIPCMCIAUSB
   #test for sub-sub-line...
   if [ ! "`echo -n "$ONELINE" | grep '^\W\W[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\W\+'`" = "" ];then
    [ ! "$UPSUBLINE" = "" ] && echo "$UPSUBLINE" >> $DESTPCIPCMCIAUSB
   fi
   UPSUBLINE=""
   echo "$ONELINE" >> $DESTPCIPCMCIAUSB
   UPVENDOR=""
  fi
 fi

 CNT=`expr $CNT + 1`
done

sync
echo "Have created $DESTPCIUSB and $DESTPCIPCMCIAUSB,"
echo "the former you will have to manually copy to boot/initrd-tree/,"
echo "the latter to packages/0rootfs_skeleton-xxx/usr/share/"

###END###
