#!/bin/bash
#
#
# Changes:
#   01/30/2006  J.Stapels   (-01) Added support for the PCNet32 driver specifically for VMware.
#   03/22/2006  M. Clark    (-02) Changes to support /etc/modprobe.conf
#   05/02/2006  M. Clark    (-03) Changes to fix random eth switching on MCP4
#   06/02/2006  M. Clark          Added ethtool support for e100 module
#   06/13/2006  M. Clark          Added bnx2 support.
#   09/26/2006  M. Clark          Added 0x105e to e1000 for El-Paso Dual Ethernet
#   03/08/2007  M. Clark          Added firewall settings migration
#   05/10/2007  M. Clark    (-05) 605576 - Remove modules before loading to ensure correct order
#

check_config_file() {
    local config_file=$1
    local unique_id=$2
    local if_id=$3

    if grep -q "STARTMODE.*manual" $config_file >/dev/null 2>&1; then
        # The startmode for everything should be onboot
        echo "Changing STARTMODE for $config_file" >> $LOG
        tmpfile="/tmp/ifcfg"
        sed -e "/^STARTMODE/d" $config_file > $tmpfile
        echo "STARTMODE='onboot'" >> $tmpfile
        mv -f $tmpfile $config_file
    fi

    if grep $unique_id $config_file >/dev/null 2>&1; then
        # Config file exists and has the correct unique id in it
        echo "Already had config file $config_file" >> $LOG
        return 0
    fi

    if [ -e $config_file ]; then
        # Config file exists but does not have the correct unique id in it
        # Let's see if we can find the right card for this config
        local id=$(grep "UNIQUE" $config_file | sed -e "s/UNIQUE=//;s/'//g")
        if [ -n "$id" ]; then
            echo "looking for device with unique id $id" >> $LOG
            local new_eth_num=0
            local found=0
            local i=0
            while [ $i -lt $num_e1000 ]; do
                if [ "${e1000_unique_ids[$i]}" = "$id" ]; then
                    echo "Found the device with unique id $id" >> $LOG
                    local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                    move_config_file $config_file $new_config_file
                    local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                    local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                    migrate_firewall_settings $fw_file $new_fw_file

                    found=1
                    break
                fi
                i=$(($i+1))
                new_eth_num=$(($new_eth_num+1))
            done

            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_e100 ]; do
                    if [ "${e100_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_eth_num=$(($new_eth_num+1))
                done
            fi

            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_tg3 ]; do
                    if [ "${tg3_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_eth_num=$(($new_eth_num+1))
                done
            fi

            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_bcm5700 ]; do
                    if [ "${bcm5700_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_eth_num=$(($new_eth_num+1))
                done
            fi

            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_bnx2 ]; do
                    if [ "${bnx2_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_eth_num=$(($new_eth_num+1))
                done
            fi

            # (-01)
            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_amd ]; do
                    if [ "${amd_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-eth$new_eth_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.eth$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_eth_num=$(($new_eth_num+1))
                done
            fi

            new_tr_num=0
            if [ "$found" = "0" ]; then
                i=0
                while [ $i -lt $num_tr ]; do
                    if [ "${tr_unique_ids[$i]}" = "$id" ]; then
                        echo "Found the device with unique id $id" >> $LOG
                        local new_config_file="/etc/sysconfig/network/ifcfg-tr$new_tr_num"
                        move_config_file $config_file $new_config_file

                        local fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
                        local new_fw_file="/opt/ccfw/data/FirewallSettings.tr$new_eth_num-NETAPP-INPUT"
                        migrate_firewall_settings $fw_file $new_fw_file

                        found=1
                        break
                    fi
                    i=$(($i+1))
                    new_tr_num=$(($new_tr_num+1))
                done
            fi

            if [ "$found" = "0" ]; then
                echo "Did not find any device with unique id $id.. this card must have been removed" >> $LOG
            fi
        else
            echo "No unique id found in config file $config_file" >> $LOG
        fi
    fi

    # Try to find our config
    local config_files=$(find /etc/sysconfig/network -name "ifcfg-eth*" -or -name "ifcfg-tr*")
    if [ -z "$config_files" ]; then
        echo "No config files found at all.. writing default config" >> $LOG
        write_default_config $config_file $unique_id
        return
    fi

    if grep $unique_id $config_files >/dev/null 2>&1; then
        local found_config_file=$(grep -H $unique_id $config_files | head -n 1 | sed -e "s/:.*//")
        echo "Found config file $found_config_file with unique id $unique_id.. moving" >> $LOG
        move_config_file $found_config_file $config_file

        local tmp_if_id="${found_config_file##*ifcfg-}"
        tmp_if_id="${tmp_if_id%%.*}"
        local fw_file="/opt/ccfw/data/FirewallSettings.$tmp_if_id-NETAPP-INPUT"
        local new_fw_file="/opt/ccfw/data/FirewallSettings.$if_id-NETAPP-INPUT"
        migrate_firewall_settings $fw_file $new_fw_file

        return
    else
        echo "Did not find a config file with unique id $unique_id.. this card must be new" >> $LOG
        if [ -e $config_file ]; then
            echo "Config file $config_file already exists.. adding unique id to config" >> $LOG
            local tmpconfig="/tmp/detectNetCard.tmpconfig"
            sed -e "/^UNIQUE/d" $config_file > $tmpconfig
            echo "UNIQUE='$unique_id'" >> $tmpconfig
            mv -f $tmpconfig $config_file
#            echo -e "\ec" # clear the screen
#            echo "Found a new network card with an existing configuration file"
#            echo
#            echo "Current config: $config_file"
#            cat $config_file
#            
#            local done=0
#            while [ "$done" = "0" ]; do
#                echo
#                key="y" # default to keep file
#                read -p "Would you like to keep the current Config? [Yy|Nn]: " -t 30 key
#                case $key in
#                    y|Y)
#                        echo "User chose to keep current config.. adding unique id to config" >> $LOG
#                        local tmpconfig="/tmp/detectNetCard.tmpconfig"
#                        sed -e "/^UNIQUE/d" $config_file > $tmpconfig
#                        echo "UNIQUE='$unique_id'" >> $tmpconfig
#                        mv -f $tmpconfig $config_file
#                        done=1
#                        ;;
#                    n|N)
#                        echo "User chose to overwrite current config" >> $LOG
#                        write_default_config $config_file $unique_id
#                        done=1
#                        ;;
#                    *) ;;
#                esac
#            done
        else
            write_default_config $config_file $unique_id
        fi

        return
    fi
}

move_config_file() {
    local src=$1
    local dest=$2
    echo "Moving config file $src to $dest" >> $LOG
    if [ -e $dest ]; then
        echo "Destination file exists.. creating backup" >> $LOG
        mv -f $dest "$dest.old"
        mv -f $src $dest
    else 
        mv -f $src $dest
    fi
}

migrate_firewall_settings() {
    local fw_file=$1
    local new_fw_file=$2
    local fw_file_bak="/tmp/${fw_file##*/}"

    echo "Migrating firewall configuration $fw_file -> $new_fw_file" >> $LOG

    if [ -e "$new_fw_file" ]; then
        echo "Backing up existing file $new_fw_file" >> $LOG
        cp -f "$new_fw_file" "/tmp/${new_fw_file##*/}"
    else 
        echo "$new_fw_file did not exist." >> $LOG
    fi

    if [ -e "$fw_file_bak" ]; then
        echo "Copying $fw_file_bak -> $new_fw_file" >> $LOG
        mv -f "$fw_file_bak" "$new_fw_file"
    else
        if [ -e "$fw_file" ]; then
            echo "Copying $fw_file -> $new_fw_file" >> $LOG
            cp -f "$fw_file" "$new_fw_file"
        else
            echo "$fw_file did not exist." >> $LOG
        fi
    fi
}

write_default_config() {
    local config_file=$1
    local unique_id=$2

    echo "Writing config file $config_file with unique id $unique_id" >> $LOG

    echo "BOOTPROTO='static'" > $config_file
    echo "IPADDR='0.0.0.0'" >> $config_file
    echo "NETMASK='255.255.255.0'" >> $config_file
    echo "MTU='1500'" >> $config_file
    echo "STARTMODE='onboot'" >> $config_file
    echo "UNIQUE='$unique_id'" >> $config_file
}

query_delete_config() {
    local file=$1
    local backup_dir="/etc/sysconfig/network/saved-configs"
    local backup_file="$backup_dir/${file##*/}"

    echo "Found a config file for nonexistent card.. prompting user" >> $LOG
    echo -e "\ec"
    echo "Found a config file for a card that no longer exists"
    echo 
    echo "Config file: $file"
    cat $file
    echo
    echo "If you choose to *not* delete the file, it will be saved as:"
    echo "$backup_file"

    local done=0
    while [ "$done" = "0" ]; do
        echo
        local key="y" # default to delete file
        read -p "Delete the uneeded file? [Yy|Nn]: " -t 30 key
        case $key in
            Y|y)
                done=1
                echo "User chose to delete config file" >> $LOG
                rm -f $file
                ;;
            N|n)
                done=1
                echo "User chose to keep config file" >> $LOG
                if [ -e $backup_dir ]; then
                    if [ ! -d $backup_dir ]; then
                        echo "Backup directory $backup_dir existed but wasn't a directory" >> $LOG
                        rm -f $backup_dir
                        mkdir -p $backup_dir
                    fi
                else
                    echo "Creating backup dir $backup_dir" >> $LOG
                    mkdir -p $backup_dir
                fi

                echo "Saving file as $backup_file" >> $LOG
                mv -f $file $backup_file
                ;;
            *) ;;
        esac
    done
}

#-03 start
#get_module_options() {
#    local module=$1
#
#    if grep -q "options $module" $modules_conf; then
#        opts=$(grep -m 1 "options $module" $modules_conf)
#        opts=${opts##options $module }
#        echo "Options found for $module: $opts" >> $LOG
#        echo $opts
#    else
#        echo "No options found for $module" >> $LOG
#    fi
#}
#-03 end

#-05 start
remove_module() {
    local module=$1

    # the modules have version information appended when loaded (ie. bcm5700_8_2_7)
    local full_name=$(lsmod | grep -m 1 "$module" | cut -d' ' -f 1)
    if [ -n "$full_name" ]; then
        echo "Found module $full_name loaded.. removing." >> $LOG
        rmmod $full_name >> $LOG 2>&1
    else
        echo "Module $module was not loaded." >> $LOG
    fi
}
#-05 end

modules_conf="/etc/modules.conf"
modules_conf_copy="/tmp/modules.conf.copy"
LOG="/var/log/detectNetCards.log"

rm -f $LOG
echo "Running detectNetCards at $(date)" > $LOG

lsmod >> $LOG 2>&1

#-05 start
# try to remove any modules that may be loaded for some stupid reason
remove_module e1000
remove_module e100
remove_module tg3
remove_module bcm5700
remove_module bnx2
remove_module pcnet32
remove_module xircom_tulip_cb
#-05 end

#-02 start
# Create a symbolic link to modprobe.conf if it exists
if [ -e /etc/modprobe.conf ]; then
    if [ ! -e /etc/modules.conf ]; then
        echo "Creating symbolic link to modprobe.conf" >> $LOG
        ln -s /etc/modprobe.conf /etc/modules.conf >> $LOG 2>&1
    elif [ ! -L /etc/modules.conf ]; then
        echo "/etc/modules.conf was *not* a link.. moving to modprobe.conf and creating link." >> $LOG
        mv -f /etc/modules.conf /etc/modprobe.conf >> $LOG 2>&1
        ln -s /etc/modprobe.conf /etc/modules.conf >> $LOG 2>&1
    fi
    modules_conf="/etc/modprobe.conf"
fi
#-02 end

# Check to see if we should start pcmcia
echo "Checking to see if we should start pcmcia" >> $LOG
# find what the default runlevel is
run_level=$(grep "id:.:initdefault" /etc/inittab | cut -d : -f 2)
if [ -e /etc/init.d/rc$run_level.d/S*pcmcia ]; then
    echo "Starting pcmcia from run level $run_level" >> $LOG
    /etc/init.d/rc$run_level.d/S*pcmcia start >> $LOG 2>&1
else
    echo "Didn't find pcmcia script" >> $LOG
fi

# load the keyboard drivers too
if [ -e /etc/init.d/rc$run_level.d/Skbd ]; then
    echo "Starting keyboard from run level $run_level" >> $LOG
    /etc/init.d/rc$run_level.d/S*kbd start >> $LOG 2>&1
else
    echo "Didn't find keyboard script" >> $LOG
fi

# create a copy of the modules.conf file with all 
# references to ethernet devices removed
echo "Creating copy of $modules_conf file" >> $LOG
# save old e100 options if they are there
e100_options=$(grep "^options e100 " $modules_conf | sed -e "s/^options e100 .*e100_speed_duplex=//")
sed -e '/^alias eth[[:digit:]]\+/d' -e '/^alias tr[[:digit:]]\+/d' -e '/^options e100 /d' $modules_conf > $modules_conf_copy
mv -f $modules_conf_copy $modules_conf

num_e100=0
num_e1000=0
num_tg3=0
num_bcm5700=0
num_bnx2=0
num_tr=0
num_xircom=0
num_amd=0       # (-01)
echo "Executing hardware information utility" >> $LOG
hwinfo_output="/tmp/hwinfo.out"
/usr/sbin/hwinfo --netcard > $hwinfo_output

i=0
for id in $(grep "\<Unique ID\>" $hwinfo_output | sed -e "s/.*Unique ID: //"); do
    unique_ids[$i]=$id
    i=$(($i+1))
done

if [ $i -gt 0 ]; then
    num_devices=$i
    echo "Found $num_devices network card(s)." >> $LOG

    i=0
    for id in $(grep "\<Vendor\>" $hwinfo_output | sed -e "s/.*0x/0x/;s/ .*$//"); do
        vendor_ids[$i]=$id
        i=$(($i+1))
    done

    i=0
    for id in $(grep "\<Device\>" $hwinfo_output | sed -e "s/.*0x/0x/;s/ .*$//"); do
        device_ids[$i]=$id
        i=$(($i+1))
    done

    i=0
    while [ $i -lt $num_devices ]; do
        echo "Found network card with Vendor Id: ${vendor_ids[$i]} Device Id: ${device_ids[$i]}" >> $LOG

        case ${vendor_ids[$i]} in
            0x8086) # Intel
                case ${device_ids[$i]} in
                    0x1000|0x1001|0x1004|0x1008|0x1009|0x100c|0x100d|\
                    0x100e|0x100f|0x1010|0x1011|0x1012|0x1013|0x1014|\
                    0x1015|0x1016|0x1017|0x1018|0x1019|0x101d|0x101e|\
                    0x1026|0x1027|0x1028|0x1075|0x1076|0x1077|0x1078|\
                    0x1079|0x107a|0x107b|0x105e)
                        # Gigabit ethernet cards
                        e1000_unique_ids[$num_e1000]=${unique_ids[$i]}
                        num_e1000=$(($num_e1000+1))
                        ;;
                    0x1229|0x2449|0x1059|0x1209|0x1029|0x1030|0x1031|\
                    0x1032|0x1033|0x1034|0x1038|0x1039|0x103a|0x103b|\
                    0x103c|0x103d|0x103e|0x1050|0x1051|0x1052|0x1053|\
                    0x1054|0x1055|0x2459|0x245d)
                        # 10/100 ethernet cards
                        e100_unique_ids[$num_e100]=${unique_ids[$i]}
                        num_e100=$(($num_e100+1))
                        ;;
                    *)
                        echo "Unsupported network card found." >> $LOG
                        exit 1
                        ;;
                esac
                ;;

            0x1014) # IBM
                case ${device_ids[$i]} in
                    0x003e)
                        # Token ring cards
                        tr_unique_ids[$num_tr]=${unique_ids[$i]}
                        num_tr=$(($num_tr+1))
                        ;;
                    *)
                        echo "Unsupported network card found." >> $LOG
                        exit 1
                        ;;
                esac
                ;;

            0x115d) # Xircom
                case ${device_ids[$i]} in
                    0x0003)
                        # PCMCIA ethernet
                        xircom_unique_ids[$num_xircom]=${unique_ids[$i]}
                        num_xircom=$(($num_xircom+1))
                        ;;
                    *)
                        echo "Unsupported network card found." >> $LOG
                        exit 1
                        ;;
                esac
                ;;

            0x14e4) # Broadcom
                case ${device_ids[$i]} in
                    0x1644|0x1645|0x1646|0x1647|0x164d|0x16a6|\
                    0x16a7|0x16a8|0x16c6|0x16c7)
                        # Gigabit ethernet cards
                        tg3_unique_ids[$num_tg3]=${unique_ids[$i]}
                        num_tg3=$(($num_tg3+1))
                        ;;
                    0x1648|0x1653|0x1654|0x1659|0x165d|0x165e|0x1696)
                        # More Gigabit ethernet cards
                        bcm5700_unique_ids[$num_bcm5700]=${unique_ids[$i]}
                        num_bcm5700=$(($num_bcm5700+1))
                        ;;
                    0x164c)
                        bnx2_unique_ids[$num_bnx2]=${unique_ids[$i]}
                        num_bnx2=$(($num_bnx2+1))
                        ;;
                    *)
                        echo "Unsupported network card found." >> $LOG
                        exit 1
                        ;;
                esac
                ;;

            0x1022) # AMD (-01)
                case ${device_ids[$i]} in
                    0x2000)
                        # PCnet32 (-01)
                        amd_unique_ids[$num_amd]=${unique_ids[$i]}
                        num_amd=$((num_amd+1))
                        ;;
                    *)
                        echo "Unsupported network card found." >> $LOG
                        exit 1
                        ;;
                esac
                ;;

            *)
                echo "Unsupported network card found." >> $LOG
                exit 1
                ;;
        esac

        i=$(($i+1))
    done
else
    echo "No network cards found." >> $LOG
fi

rm -f $hwinfo_output

# add lines to modules.conf for each card
eth_num=0

i=0
while [ $i -lt $num_e1000 ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading e1000 module" >> $LOG
        modprobe e1000 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num e1000" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${e1000_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

i=0
while [ $i -lt $num_e100 ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        #-XX start
        if [ "$e100_options" != "" ]; then
            echo "Converting e100 speed options." >> $LOG
            eth_num_copy=$eth_num
            for speed in ${e100_options//,/ }; do
                case $speed in
                    0)
                        echo "Setting e100 eth$eth_num_copy to autonegotiate." >> $LOG
                        echo "#e100 /usr/sbin/ethtool -s eth$eth_num_copy autoneg on" >> $modules_conf
                        ;;
                    1)
                        echo "Setting e100 eth$eth_num_copy to 10/half." >> $LOG
                        echo "#e100 /usr/sbin/ethtool -s eth$eth_num_copy autoneg off speed 10 duplex half" >> $modules_conf
                        ;;
                    2)
                        echo "Setting e100 eth$eth_num_copy to 10/full." >> $LOG
                        echo "#e100 /usr/sbin/ethtool -s eth$eth_num_copy autoneg off speed 10 duplex full" >> $modules_conf
                        ;;
                    3)
                        echo "Setting e100 eth$eth_num_copy to 100/half." >> $LOG
                        echo "#e100 /usr/sbin/ethtool -s eth$eth_num_copy autoneg off speed 100 duplex half" >> $modules_conf
                        ;;
                    4)
                        echo "Setting e100 eth$eth_num_copy to 100/full." >> $LOG
                        echo "#e100 /usr/sbin/ethtool -s eth$eth_num_copy autoneg off speed 100 duplex full" >> $modules_conf
                        ;;
                    *)
                        echo "Unrecognized speed/duplex setting in e100_options" >> $LOG
                        ;;
                esac
                $eth_num_copy=$(($eth_num_copy+1))
            done
        fi
        #-XX end
        echo "Loading e100 module" >> $LOG
        modprobe e100 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num e100" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${e100_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

i=0
while [ $i -lt $num_tg3 ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading tg3 module" >> $LOG
        modprobe tg3 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num tg3" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${tg3_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

i=0
while [ $i -lt $num_bcm5700 ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading bcm5700 module" >> $LOG
        modprobe bcm5700 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num bcm5700" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${bcm5700_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

i=0
while [ $i -lt $num_bnx2 ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading bnx2 module" >> $LOG
        modprobe bnx2 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num bnx2" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${bnx2_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

# (-01)
i=0
while [ $i -lt $num_amd ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading pcnet32 module" >> $LOG
        modprobe pcnet32 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num pcnet32" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${amd_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

i=0
while [ $i -lt $num_xircom ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading xircom module" >> $LOG
        modprobe xircom_tulip_cb >> $LOG 2>&1
    fi
    #-03 end
    echo "alias eth$eth_num xircom_tulip_cb" >> $modules_conf
    check_config_file "/etc/sysconfig/network/ifcfg-eth$eth_num" ${xircom_unique_ids[$i]} "eth$eth_num"
    i=$(($i+1))
    eth_num=$(($eth_num+1))
done

tr_num=0
i=0
while [ $i -lt $num_tr ]; do
    #-03 start
    if [ $i -eq 0 ]; then
        echo "Loading olympic module" >> $LOG
        modprobe olympic ringspeed=16 >> $LOG 2>&1
    fi
    #-03 end
    echo "alias tr$tr_num olympic" >> $modules_conf
#-03    ringspeed_line="options olympic ringspeed=16"
#-03    if ! grep "$ringspeed_line" $modules_conf >/dev/null 2>&1; then
#-03        echo "$ringspeed_line" >> $modules_conf
#-03    fi
    check_config_file "/etc/sysconfig/network/ifcfg-tr$tr_num" ${tr_unique_ids[$i]} "tr$tr_num"
    i=$(($i+1))
    tr_num=$(($tr_num+1))
done

# get rid of configs for cards that don't exist
for file in /etc/sysconfig/network/ifcfg-eth*; do
    if [ -e "$file" ]; then
        if echo $file | grep ".old" >/dev/null 2>&1; then
            query_delete_config $file
        else
            num=${file#/etc/sysconfig/network/ifcfg-eth}
            if [ $num -ge $eth_num ]; then
                query_delete_config $file
            fi
        fi
    fi
done

for file in /etc/sysconfig/network/ifcfg-tr*; do
    if [ -e "$file" ]; then
        if echo $file | grep ".old" >/dev/null 2>&1; then
            query_delete_config $file
        else
            num=${file#/etc/sysconfig/network/ifcfg-tr}
            if [ $num -ge $tr_num ]; then
                query_delete_config $file
            fi
        fi
    fi
done

if grep -q "^IPADDR=.*0.0.0.0" /etc/sysconfig/network/ifcfg-eth0; then
    echo "eth0 address was 0.0.0.0 - changing to 127.0.0.1" >> $LOG
    tmpfile="/tmp/ifcfg"
    sed -e "/^IPADDR/d" /etc/sysconfig/network/ifcfg-eth0 > $tmpfile
    echo "IPADDR='127.0.0.1'" >> $tmpfile
    mv -f $tmpfile /etc/sysconfig/network/ifcfg-eth0
fi

rm -f /tmp/FirewallSettings*

# overwrite the modules.conf file
echo "Touching modules.dep" >> $LOG
find /lib/modules -name "modules.dep" -exec touch {} \;

# Execute any ethtool commands found
grep "^#.*/usr/sbin/ethtool " $modules_conf | sed -e "s|.*/usr|/usr|" | while read cmd; do
    echo "Executing command: $cmd" >> $LOG
    $cmd >> $LOG 2>&1
done

exit $?
