#! /bin/sh
#----------------------------------------------------------------------------
# /etc/init.d/eth - configuration of your ethernet card
#
# Creation:	2001-10-15  fm
# Last Update:  $Id$
#
# Copyright (c) 2001-2008 the eisfair team, team(at)eisfair(dot)org>
#
# 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 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------
script='eth:'

. /etc/config.d/base

if [ "$ETH_DO_DEBUG" = yes ]
then
    set -x
fi

kernel_version=`uname -r`

#----------------------------------------------------------------------------
# show mapping message only wich start
#----------------------------------------------------------------------------
show_mapping_msg ()
{
    _job=${1}
    _drv_old=${2}
    drv=${3}

    case ${_job} in
        start)
            echo "mapping eth driver ${_drv_old} to ${drv} ..."
        ;;
    esac
}
#----------------------------------------------------------------------------
# drv mapping
#----------------------------------------------------------------------------
drv_mapping ()
{
    _job=${1}

    case "${kernel_version}" in
        2.4.26*)
            case ${drv} in
                rtl8139-orig)
                    _old_drv_=${drv}
                    drv=8139too
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;
                via-rhine)
                    _old_drv_=${drv}
                    drv=rhinefet
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;  # fm: via-rhine crashes kernel
            esac
        ;;
        2.4.35*)
            case ${drv} in
                "rtl8139"|"rtl8139-orig")
                    _old_drv_=${drv}
                    drv=8139too
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;
                ns820)
                    _old_drv_=${drv}
                    drv=ns83820
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;
                intel-gige)
                    _old_drv_=${drv}
                    drv=e1000
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;
                myson803)
                    _old_drv_=${drv}
                    drv=fealnx
                    show_mapping_msg ${_job} ${_old_drv_} ${drv}
                ;;
            esac
        ;;
    esac
}
#----------------------------------------------------------------------------
# main
#----------------------------------------------------------------------------
case $1 in
    start)
        /usr/local/bin/colecho "loading ethernet drivers ..." gn
        idx=1

        case "$kernel_version" in
            2.2.*)
                insmod 8390 2>/dev/null
                insmod pci-scan 2>/dev/null
            ;;
        esac

        while [ "$idx" -le "$ETH_DRV_N" ]
        do
            eval drv='$ETH_DRV_'$idx
            eval opt='$ETH_DRV_'$idx'_OPTION'

            case $drv in
                *_cs)
                    echo "$drv should be loaded by pcmcia cardmgr in a few seconds ..."
                ;;
                *)
                    case "$kernel_version" in
                        2.2.*)
                            insmod $drv $opt
                            rtc=$?
                        ;;
                        *)
                            drv_mapping "start"
                            modprobe $drv $opt
                            rtc=$?

                            if [ $rtc != 0 -a "$opt" != "" ]
                            then
                                echo "Loading of $drv failed, trying again without options..."
                                modprobe $drv   # some drivers (e.g. ne.o) don't want options anymore
                                rtc=$?
                            fi
                        ;;
                    esac

                    if [ $rtc != 0 ]
                    then
                        echo $script" modul $drv $opt failed!" >>/bootmsg.txt
                    fi
                ;;
            esac

            idx=`/usr/bin/expr $idx + 1`
        done
    ;;
    stop)
        /usr/local/bin/colecho "unloading ethernet drivers ..." gn
        idx=1

        while [ "$idx" -le "$ETH_DRV_N" ]
        do
            eval drv='$ETH_DRV_'$idx

            case $drv in
                *_cs)
                ;;
                *)
                    case "$kernel_version" in
                        2.2.*)
                            rmmod $drv
                        ;;
                        *)
                            drv_mapping
                            rmmod -r $drv 2>/dev/null
                        ;;
                    esac
                ;;
            esac

               idx=`/usr/bin/expr $idx + 1`
        done

        case "$kernel_version"	in
            2.2.*)
                rmmod pci-scan 2>/dev/null
                rmmod 8390 2>/dev/null
            ;;
        esac
    ;;
esac

set +x