#!/bin/bash
########################################################################
# Check to see if any kernel modules are installed, if not, end here
if rpm -qa | grep -q kernel-module ; then
  echo "This machine has kernel modules installed ... fixing"

  ########################################################################
  # Make sure grubby exists, if it does fix up grub
  if [ -f /sbin/grubby ] ; then
    DKERNEL=`/sbin/grubby --default-kernel`
	echo "checking on kernel $DKERNEL"
	if [ -f $DKERNEL ] ; then
	  echo "Default kernel is real"
	elif [ -f /boot/$DKERNEL ] ; then
	  echo "Default kernel is real"
	else
	  echo "Default kernel is not real"
	  echo "removing it from grub"
	  grubby --remove-kernel=$DKERNEL
	fi
  else
    echo "There is no grubby, we cannot fixup grub"
  fi
  ########################################################################
  # Check to see if we are on the network
  if ping -q -c 1 -w 2 131.225.111.89 > /dev/null 2>&1; then
    echo "  Found a network.. "
  
    ######################################################################
    # Make sure we can do a name lookupg
    if grep -q nameserver /etc/resolv.conf ; then
      echo "  Found a nameserver.. "
    else
      echo "  Nameserver not found ... adding one.. "
  	  echo "nameserver 131.225.17.150" >> /etc/resolv.conf
    fi
  
    if ping -q -c 1 -w 2 linux.fnal.gov > /dev/null 2>&1; then
      echo "  Network setup properly: Fixing up the kernel modules"
    
      ######################################################################
      # Do this one kernel module at a time, one kernel at a time
	  #  Hopefully that really is only for one kernel and one kernel module
	  #  But we want to make sure
	  rpm -qa | grep kernel-module | cut -d'-' -f3 | sort -u | while read module
	  do
	    cd /lib/modules
		ls -1d 2.6* | while read kern
		do
			echo "  Installing kernel-module-$module-$kern"
			yum -y install kernel-module-$module-$kern
		done
	  done    
    else
      ######################################################################
      # Notify the user that we weren't able to do their updates
      echo "We could not access the distribution servers"
      echo "Since we could not access the distribution servers, "
      echo "we could not fix any problem with the proper kernel module being installed."
    fi
  
  else
    ######################################################################
    # Since we don't have a network, we cannot fix any problem
	# with the proper kernel module being installed.
    echo "Since we don't have a network, we cannot fix any problem"
    echo "  with the proper kernel module being installed."
  fi
  ########################################################################

else
  ######################################################################
  # No kernel modules installed, so nothing to do
  echo "No kernel modules installed, nothing to fixup"
fi
exit 0
