#!/bin/sh
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
nic=0
hba=0

MOD=""
NICSTOWAIT=""
touch /etc/motd
echo "cat /etc/motd" >> /etc/profile

#Load common usb drivers
modprobe ohci-hcd
modprobe uhci-hcd
modprobe ehci-hcd


for d in /proc/sys/net/ipv4/conf/*; do 
   echo 1 > $d/arp_filter
   echo 1 > $d/arp_ignore
done
for i in $(lspci -n | awk '{print $1 "%" $3}')
do
    PCI=$(echo $i | awk -F% '{print $1}')
    VID="0x0000$(echo $i | awk -F% '{print $2}' |awk -F: '{print $1}')"
    DID="0x0000$(echo $i | awk -F% '{print $2}' |awk -F: '{print $2}')"
    if egrep "^[^   ]*[     ]*$VID[     ]*$DID" /lib/modules/`uname -r`/modules.pcimap >/dev/null
    then
        TYPE=$(
            lspci | \
            grep "^$PCI " | \
            awk '{print $2}' | \
            tr '[A-Z]' '[a-z]'
        )
        DESC=$(
            lspci | \
            grep "^$PCI " | \
            awk -F: '{print $3}' | \
            sed 's/^ *//'
        )
        MOD=$(
            egrep "^[^  ]*[     ]*$VID[     ]*$DID" /lib/modules/`uname -r`/modules.pcimap | \
            head -1 | \
            awk '{print $1}' | \
            tr -d '"'
        )
        case "$TYPE" in
            ethernet|network)
                echo "Found ($MOD) $DESC"
                GOTNIC=1
                if [ "$MOD" = "gm" ]
                then 
                    echo "alias myri0 $MOD"
                    echo "alias myri0 $MOD" >>/etc/modules.conf
                    echo "alias myri0 $MOD" >>/etc/modprobe.conf
                else
                    modprobe $MOD
                    if [ "mlx4_core" = "$MOD" ]; then
					    modprobe mlx4_en
                    fi
                fi
                ;;
            scsi|raid)
                echo "Found ($MOD) $DESC"
                GOTHBA=1
                modprobe $MOD & #We background so that messed up SANs don't stop shell
                modprobe sd_mod
                modprobe scsi_mod
                hba=$(($hba + 1))
                ;;
            *)
                continue
                ;;
        esac
    fi
done
NEEDVETH=0;
for dir in /proc/device-tree/vdevice/l-lan*; do
    if [ -d $dir ]; then NEEDVETH=1; fi
done
if [ $NEEDVETH =  1 ]; then
	modprobe ibmveth
fi
NEEDEHEA=0;
for dir in /proc/device-tree/lhea*; do
    if [ -d $dir ]; then NEEDEHEA=1; fi
done
if [ $NEEDEHEA = 1 ]; then
		    modprobe ehea
fi

NICSTOWAIT=`/sbin/ifconfig -a|grep HWaddr|grep ^eth|sed -e 's/ .*//'`
for nic in $NICSTOWAIT; do
	ifconfig $nic up
	udhcpc -i $nic -R &
done



extrat=0
until [ $extrat = 80 -o -z "$NICSTOWAIT" ]; do
   sleep 1
   extrat=$(($extrat+1))
   for nic in $NICSTOWAIT; do 
      if ifconfig $nic|grep "inet addr"; then
         NICSTOWAIT=`echo $NICSTOWAIT|sed -e s/$nic//`
         if [ $extrat -gt 45 ]; then
            echo "Warning: $nic took more than 45 seconds to receive DHCP reply, spanning-tree may not be configured well, examine switch configuration" >> /etc/motd
            echo "Warning: $nic took more than 45 seconds to receive DHCP reply, spanning-tree may not be configured well, examine switch configuration"
         fi
      elif [ $extrat = 15 ]; then
            if ethtool $nic | grep "Link detected: no"; then
               echo "$nic did not have any link when bringing up network"
               echo "$nic did not have any link when bringing up network" >> /etc/motd
               NICSTOWAIT=`echo $NICSTOWAIT|sed -e s/$nic//`
            fi
      fi
   done
done
if [ ! -z "$NICSTOWAIT" ]; then
   echo "Warning: the following network devices appeared to be connected to networks, but received no DHCP response: $NICSTOWAIT" >> /etc/motd
   echo "Warning: the following network devices appeared to be connected to networks, but received no DHCP response: $NICSTOWAIT"
fi