#!/bin/sh
# some vars to work with
NCD_tmp="NCD_tmp.txt"; WCD_tmp="WCD_tmp.txt"; WCD_run="NCD"; i=""; j=""; c=0;
NCD_rmt(){
  # start fresh
  NCD_run="$1"
  if [ "$NCD_run" == "NCD" ] ; then
  if [ -e "$NCD_tmp" ]; then
  rm $NCD_tmp
  touch $NCD_tmp
  sleep 1
  fi
  fi
  if [ "$NCD_run" == "WCD" ] ; then
  if [ -e "$WCD_tmp" ]; then
  rm $WCD_tmp
  touch $WCD_tmp
  sleep 1
  fi
  fi
}
# 02  Network Controller Device
NCD_src=`sysctl -a dev | grep class=0x02`
NCD_rmt $WCD_run
for i in $NCD_src; do
  echo ${i} >> $NCD_tmp
done
i="";
NCD_fld=$(grep ":" $NCD_tmp | awk -F : '{ print $1 }')
if [ ! -z "$NCD_fld" ]; then
  NCD_rmt $WCD_run
  for i in $NCD_fld; do
  NCD_dev=$(echo ${i} | cut -d'.' -f2)
  NCD_cnt=$(echo ${i} | cut -d'.' -f3)
  NCD_dsc=$(sysctl -a dev.${NCD_dev}.${NCD_cnt}.%desc | cut -d':' -f2)
  echo "${NCD_dev}${NCD_cnt}:${NCD_dev}.${NCD_cnt}:${NCD_dsc}:Hooked" >> $NCD_tmp
  done
fi
# lets run pciconf
WCD_run="WCD"; i="";
WCD_src=`pciconf -lv | grep class=0x02`
NCD_rmt $WCD_run
for i in $WCD_src; do
  echo ${i} >> $WCD_tmp
done
i="";
WCD_fld=$(grep ":" $WCD_tmp | awk -F : '{ print $1 }')
if [ ! -z "$WCD_fld" ]; then
  WCD_xmp=$(grep ":" $NCD_tmp | awk -F : '{ print $1 }')
  for i in $WCD_fld; do
  WCD_nme=$(grep "${i}" $WCD_tmp)
  WCD_sta=$(pciconf -a ${WCD_nme} | cut -d' ' -f2 -f3 -f4)
  # device
  WCD_dsc=$(pciconf -lv ${WCD_nme} | grep device)
  WCD_dsc=$(echo ${WCD_dsc} | cut -d'=' -f2)
  WCD_dsc=$(echo "$WCD_dsc" | sed "s/\'//g")
  WCD_pci=$(echo ${i} | cut -d'@' -f1)
  NCD_res=$(grep "${WCD_pci}" $NCD_tmp)
  # if no results from grep, should be a new device
  if [ -z "$NCD_res" ]; then
  # This will not work if double digits
  WCD_max=9
  for j in $(seq 1 $WCD_max); do
  if [ ! -z $(echo ${WCD_pci} | grep ${j}) ]; then
  WCD_two=$(echo "$WCD_pci" | sed "s/$j/\.$j/g")
  echo "${WCD_pci}:${WCD_two}:${WCD_dsc}:${WCD_sta}" >> $NCD_tmp
  fi
  done
  fi
  done
fi
  if [ -e "$WCD_tmp" ]; then
  rm $WCD_tmp
  sleep 1
  fi
# Example usage of data colected in tmp file:
XMP_exm(){
  i=""
  NCD_xmp=$(grep ":" $NCD_tmp | awk -F : '{ print $1 }')
  echo ""
  echo "-------------------------------------------------------------------------------"
  for i in $NCD_xmp; do
  c=`expr $c + 1`
  XMP_dsc=$(grep "${i}" $NCD_tmp | awk -F : '{ print $3 }')
  echo "[${c}] [${i}] ${XMP_dsc}"
  done
  echo ""
  echo "Select Network Controller Device to work with"
  echo "Enter the number coresponding to network device listed above (default [1]):"
  read XMP_sel
  if [ -z "$XMP_sel" ]; then
  XMP_sel=1
  fi
  echo "Selection made is: "${XMP_sel}
  echo ""
}
XMP_exm
echo ""
echo "ALL NETWORK ADAPTERS:"
echo "-------------------------------------------------------------------------------"
cat $NCD_tmp
echo "-------------------------------------------------------------------------------"