#!/bin/bash
#
#
######################################################################
# GPL 
# (c) 2002 SuSE Linux AG
# Authors Fabian Herschel, Holger Mueller, Joerg Steffens
#
# $Id: sc_hardware_net 2 2010-01-19 14:09:19Z joergs $
###########################################################
#
### BEGIN INIT INFO
# Provides:        sc_hardware_net
# Required-Start:  $local_fs 
# Should-Start:    sc_hardware_prepare
# Required-Stop:   $local_fs
# Should-Stop:     $null
# Default-Start:   3 5
# Default-Stop:
# Description:     configure the network hardware
### END INIT INFO
#
#
###########################################################
#
# NOTE:
#    sc_hardware_net 
#    is designed to configure the network cards at the boot time.
#    So "network" should run after sc_hardware_net. 
#    You should change the file /etc/init.d/network:
#      Should-Start sc_hardware_net
#    In newer systems coldplug is used to configure
#    /etc/sysconfig/hardware/hwcfg-*
#    devices. Therefore sc_hardware_net should also start before coldplug
#    coldplug:
#      Should-Start sc_hardware_net
#
###########################################################

. /etc/rc.status
rc_reset

# source SmartClient config
test -r /etc/smartclient/sc_hardware && . /etc/smartclient/sc_hardware

# include SmartClient hardware helper functions
test -r /usr/lib/smartclient/sc_hardware.sh && . /usr/lib/smartclient/sc_hardware.sh



do_the_network_card_config()
{
    echo "Starting network card config on boot ... "

    logger -p local0.info -t sc_hardware_net "HW_NETWORK_CONFIG STARTED"

    # call helper function
    create_netcard_info_file

    # removing existing config files
    rm -f $REMOVE_OLD_CONF

    OLDIFS=$IFS
    IFS="|"
    while read NCTYPE UNIQUEID ACTIVATION_CMD TheModule AdditionalStuff SYSFSBUSID HWADDRESS DUMMY; do

        if [ "$NCTYPE" = "eth" -o "$NCTYPE" = "tr" ]; then

            # HWADRESS is only available, if module is loaded
            if [ -z "$HWADDRESS" ]; then
                    MISSING_HWADDRESS="yes"
                    # load module (to get MAC address)
                    eval $ACTIVATION_CMD
                    rc_status
            fi

        fi

    done < $NETCARD_INFO_FILE



    if [ "$MISSING_HWADDRESS" ]; then
        # call helper function again
        # this time, all required modules should be loaded
        create_netcard_info_file
    fi



    while read NCTYPE UNIQUEID ACTIVATION_CMD TheModule AdditionalStuff SYSFSBUSID HWADDRESS DUMMY; do

        printf "  %-5s %17s  [%s]" "$NCTYPE" "$HWADDRESS" "$ACTIVATION_CMD"

        if [ "$NCTYPE" = "eth" -o "$NCTYPE" = "tr" ]; then

            # netcard attached to PCI bus is assumed 
            BUSID="bus-pci-$SYSFSBUSID"
            
            HWCFG_FILE="/etc/sysconfig/hardware/hwcfg-$BUSID"
            logger -p local0.info -t sc_hardware_net "CONFIGURE $HWCFG_FILE"
            (
                    echo "# sc_hardware_net"
                    echo "MODULE='$TheModule'"
                    echo "MODULE_OPTIONS='$AdditionalStuff'"
                    echo "STARTMODE='auto'"
            ) >"$HWCFG_FILE"

            DEVICE_FILE="/etc/sysconfig/network/ifcfg-$NCTYPE-id-$HWADDRESS"
            
            #
            # configure the sysconfig file 
            # of the ethernet or tokenring card
            #    
            logger -p local0.info -t sc_hardware_net "CONFIGURE $DEVICE_FILE"
            (
            echo "BOOTPROTO='dhcp'"
            echo "MTU=''"
            echo "REMOTE_IPADDR=''"
            echo "STARTMODE='onboot'"
            echo "UNIQUE='$UQID'"
            echo "_nm_name='$BUSID'"
            ) >"$DEVICE_FILE"

            rc_status -v; rc_reset
            # hwup "hwcfg-$BUSID"

        else
            rc_status -s; rc_reset
        fi

    done < $NETCARD_INFO_FILE
    IFS=$OLDIFS
		
    logger -p local0.info -t sc_hardware_net "HW_NETWORK_CONFIG FINISHED"
}

case $1 in
    start | restart | reload ) 
        do_the_network_card_config
        ;;
    stop) 
        exit 
        ;;
    status)
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac

rc_exit
