#!/bin/bash

# 02 Jan 2002
# Harley A. Stenzel
# Move kernel module selection to script
#
# returns 0 on success
# 1 when the kernel does not load
# 2 when mknod fails
# 3 when usage is incorrect

KERNELVERSION=`uname -r`
NDBINPATH=/opt/nd/servers/bin

UPND=ibmnd-up-$KERNELVERSION
SMPND=ibmnd-smp-$KERNELVERSION

function makedevice() {
   NDMAJOR=`/bin/grep ibmnd /proc/devices | /bin/awk '{print $1}'`
   /bin/mknod /dev/ibmnd c $NDMAJOR 0 2>/dev/null >/dev/null || exit 2
   ## echo "DEBUG: mknod success on major $NDMAJOR, kernel successfully loaded"
}

function executorstart() {
  # Perform cleanup in case traces are left behind from the last
  # running module
  executorstop

  # Check for the override option first. If ibmnd exists (i.e. linking
  # to a chosen module) then automatically try to force the load
  if [ -f $NDBINPATH/ibmnd ]
  then
    if /sbin/insmod -f -q -o ibmnd $NDBINPATH/ibmnd 2>/dev/null > /dev/null
    then
      ## echo "DEBUG: insmod force success on ibmnd"
      makedevice
      echo $NDBINPATH/ibmnd > $NDBINPATH/loaded_version 2>/dev/null
      exit 0
    fi
  fi

  # If the user did not have the "ibmnd" override option, then try to load the
  # SMP module
  if [ -f $NDBINPATH/$SMPND ]
  then
    if /sbin/insmod -q -o ibmnd $NDBINPATH/$SMPND 2>/dev/null > /dev/null
    then
      ## echo "DEBUG: insmod success on $SMPND"
      makedevice
      echo $NDBINPATH/$SMPND > $NDBINPATH/loaded_version 2>/dev/null
      exit 0
    fi
  fi

  # We exited above if SMP worked, so check UP now
  if [ -f $NDBINPATH/$UPND ]
  then
    if /sbin/insmod -q -o ibmnd $NDBINPATH/$UPND 2>/dev/null > /dev/null
    then
      ## echo "DEBUG: insmod success on $UPND"
      makedevice
      echo $NDBINPATH/$UPND > $NDBINPATH/loaded_version 2>/dev/null
      exit 0
    fi
  fi

  # If none of the modules match the user's kernel, we'll try to find
  # the closest match (i.e. 2.4.9-X) and try to force that. If the modules
  # do not have any close matches (i.e. user is 2.4.10 and we have 2.4.2 and
  # 2.4.9) then quit with the "unsupported kernel" message
  if [ `uname -a |grep -c -i smp` -eq 1 ]
  then
    ## echo "DEBUG: Multiprocessor kernel found"
    SMP=1
    KERNELMODULES=`cd $NDBINPATH; ls ibmnd-smp*`
  else
    ## echo "DEBUG: Uniprocessor kernel found"
    SMP=0
    KERNELMODULES=`cd $NDBINPATH; ls ibmnd-up*`
  fi
  
  ## echo "DEBUG: Running kernel version is $KERNELVERSION"
  ## echo "DEBUG: List of modules to check: "
  ## echo $KERNELMODULES
  # Cycle through all the appropriate list of modules, checking each one
  for KERNELMODULE in $KERNELMODULES; do
    ## echo "DEBUG: Testing $KERNELMODULE"
    if echo $KERNELVERSION | grep `echo $KERNELMODULE | cut --delimiter="-" -f 3` 2>/dev/null >/dev/null
    then
      if /sbin/insmod -f -q -o ibmnd $NDBINPATH/$KERNELMODULE 2>/dev/null > /dev/null
      then
        ## echo "DEBUG: insmod force success on $KERNELMODULE"
        makedevice
        echo $KERNELMODULE > $NDBINPATH/loaded_version 2>/dev/null
        exit 0
      fi
    fi

  done

  echo "IBMND $0: Supported kernel version not present."
  if [ $SMP -eq 1 ]
  then
    echo " Multiprocessor compiled modules were searched."
  else
    echo " Uniprocessor compiled modules were searched."
  fi
  echo " If the running kernel is close to one of the modules in $NDBINPATH,"
  echo " you may attempt to force the load by linking $NDBINPATH/ibmnd"
  echo " to the module you believe will work. Run the 'executor start' command"
  echo " again at that point, or alternatively, please contact IBM Support for"
  echo " assistance."
  exit 1
}

function executorstop() {
  ## echo "DEBUG: stopping the executor"
  /sbin/lsmod | /bin/grep -q -s ^ibmnd && \
  rmmod `/sbin/lsmod | /bin/grep ^ibmnd | awk '{print $1}'`
  /bin/rm -f /dev/ibmnd 2>/dev/null > /dev/null
  /bin/rm -f $NDBINPATH/loaded_version 2>/dev/null > /dev/null
}

if [ "$1" = "start" ]
then
  executorstart
elif [ "$1" = "stop" ]
then
  executorstop
else
  ## echo "Usage: $0 [start|stop]"
  exit 3
fi

