#!/bin/sh

# looks at pci cards installed on the system and loads ethernet modules

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

# mostly assumes you will only need 1 ethernet card....

if [ -n "$(/sbin/ifconfig eth0 2> /dev/null)" ]; then
  # already have an ethernet card, no need to keep looking
  exit 0
fi

echo "looking for PCI ethernet cards..."

pci_devices=/proc/bus/pci/devices
pci_list=/usr/share/discover/pci.lst
desired_card_type="ethernet"

load_module () {
  card_type=$(echo "$1" | cut -f 1)
  card_module=$(echo "$1" | cut -f 2)
  if [ -z "$desired_card_type" ]; then
    echo "WARNING: desired_card_type not defined..."
  fi
  if [ "$desired_card_type" = "$card_type" ]; then
    if [ "unknown" = "$card_module" -o "ignore" = "$card_module" -o -z "$card_module" ]; then
      echo "unknown $desired_card_type card module"
    else
      echo "loading $card_module for $desired_card_type card"
      modprobe -q $card_module
    fi
  fi
}

for pci_id in $(cut -f 2 "$pci_devices") ; do
  card_info=$(grep "$pci_id" "$pci_list" | cut -f 3,4)
  if [ -n "$card_info" ]; then
    load_module "$card_info"
  fi
done

if [ -n "$(/sbin/ifconfig eth0 2> /dev/null)" ]; then
  echo "no need to look for more ethernet cards..."
  exit 0
fi

echo "looking for more cards..."

# probes through a stupid list of cards

nic_modules=$(grep -v ^# /etc/network_cards)

for card in $nic_modules; do
  if [ "ne" = "$card" ]; then
    for io in 0x300 0x280; do
      modprobe -q $card io=$io
    done
  else
    modprobe -q $card
  fi
  if [ "0" = "$?" ]; then
    echo "found $card.."
    exit 0
  fi
done

echo "didn't find any cards!"
exit 1
