#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2000
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
#

. /usr/lib/gibraltar-bootsupport/common-definitions.sh
. /usr/lib/gibraltar-bootsupport/probe-devs.sh

echo "Searching for installed network adapters..."
old_printk=`cat /proc/sys/kernel/printk`
echo 0 0 0 0 > /proc/sys/kernel/printk

discover_devs network | while read line; do
  # separate module name from description
  mod=`echo $line | cut -d';' -f1`
  desc=`echo $line | cut -d';' -f2`
  if [ `echo "$mod" | wc -w` -eq 1 ]; then
    mod_known=1;
  else
    mod_known=0;
  fi

  if [ $mod_known -eq 1 ] && lsmod | grep -q "$mod"; then
    echo "  already loaded module $mod, skipping."
    continue
  fi
  
  echo -n -e "  found $desc ... "
  # skip if no module is known (because we can't load it then anyways)
  if [ $mod_known -eq 0 ]; then
    echo -e "no driver known, ${COLOR_RED}skipping${COLOR_NORMAL}"
    continue
  fi
  
  # before loading the model, remember which devices we had previously
  prev_devs=`ip link | grep ": .*: <" | cut -d: -f2`

  if modprobe -q $mod ; then
    echo -e "loaded ${COLOR_GREEN}${mod}${COLOR_NORMAL}"

    # remember the module so that it gets loaded on the next reboots
    # (but only once)
    if ! grep -q $mod /etc/modules; then
      echo $mod >> /etc/modules
    fi

    # bring the new device(s) up immediately
    cur_devs=`ip link | grep ": .*: <" | cut -d: -f2`
    for dev in $cur_devs; do
      if ! echo $prev_devs | grep -q $dev; then
        ip link set dev $dev up
      fi
    done
  else
    echo -e "${COLOR_RED}load failed${COLOR_NORMAL}"
  fi
done

echo $old_printk > /proc/sys/kernel/printk

exit 0
