#
# snatrydrivers
#
# Adds correct Kernel Interface Driver for this version of LINUX
# - Checks for LiS (as cannot Probe Communications Server for Linux Drivers without it)
# - Checks for the correct driver for this kernel level
# - If not found tries to build a kernel
#
DRVOPTIONS=/etc/opt/ibm/sna/isolationdrivers
DRVDIR=/etc/opt/ibm/sna/drivers/snapix0
DRIVEROK=1

if [ `uname -r | cut -c 1-3` = 2.6 ]
then
  KERNEL26=1
else
  KERNEL26=0
fi

if [ "x$READINSTCAT" = "x" ]
then
  #
  # Not run from install script.  Set path for message catalog file.
  # Set "C" to be the default.
  NLSPATH=/opt/ibm/sna/lib/nls/msg/LC_MESSAGES/%N:/opt/ibm/sna/lib/nls/msg/%L/%N
  export NLSPATH=${NLSPATH}:/opt/ibm/sna/lib/nls/msg/C/%N

  #
  # Set path for snareadinstcat binary
  READINSTCAT=/opt/ibm/sna/bin/snareadinstcat
fi

#
# Ensure streams is loaded
# (If this is first time called, may not have streams loaded)
#
cat /proc/modules | grep streams > /dev/null 2>&1
if [ $? = 1 ]
then
  /usr/sbin/strmakenodes
  if [ $KERNEL26 = 1 ]
  then
    /sbin/modprobe streams
  else
    /sbin/insmod streams
  fi
  if [ $? = 1 ]
  then
    # echo  "Error: Could not load streams driver"
    # echo  "Refer to the readme for further information"
    $READINSTCAT 9
    $READINSTCAT 47
    exit 1
  fi
fi
#
#
#  Check that Communications Server for Linux daemon/appl is not running
#
ACTIVE=`ps -ef | egrep "snadaemon" | grep -v grep`
if [ "x$ACTIVE" != "x" ]
then
  # echo "Error: SNA must be stopped before running this script"
  $READINSTCAT 18
  exit 2
fi

#
# check that sna drivers not loaded
#
cat /proc/modules | grep sna_linux > /dev/null 2>&1
if [ $? = 0 ]
then
  # echo "Error: SNA drivers must be uninstalled before running this script"
  $READINSTCAT 16
  exit 3
fi

#
# work out required kernel name
#
SMP=`uname -a | awk ' { print $5 } ' `
if [ x$SMP = xSMP ]
then
  UNAMEINFO=`uname -a | \
    awk ' { print $3"_"$7"_"$8"_"$9"_"$10"_"$11"_"$12 } ' `
else
  UNAMEINFO=`uname -a | \
    awk ' { print $3"_"$6"_"$7"_"$8"_"$9"_"$10"_"$11 } ' `
fi

if [ $KERNEL26 = 1 ]
then
  cd $DRVDIR 2> /dev/null
  /sbin/insmod sna_linux.ko 2> /dev/null
  if [ $? = 0 ]
  then
    # echo "Installed driver for kernel $UNAMEINFO OK"
    $READINSTCAT 28 "$UNAMEINFO"
    exit 0
  else
    # try to rebuild drivers
    cd $DRVOPTIONS
    ./cc_snalinux
    if [ $? = 0 ]
    then
      cd $DRVDIR
      /sbin/insmod sna_linux.ko
      if [ $? = 0 ]
      then
        # echo "Compiled and installed driver for kernel $UNAMEINFO OK"
        $READINSTCAT 4 "$UNAMEINFO"
        exit 0
      else
        # echo "Failed to install kernel isolation layer driver"
        $READINSTCAT 60
        exit 6
      fi
    else
      exit 7
    fi
  fi

else
  # 2.4 kernel
  cd $DRVOPTIONS
  #
  # have we got the correct driver already?
  #
  if [ -f Driver.o.$UNAMEINFO ]
  then
    /sbin/insmod -p -q Driver.o.$UNAMEINFO >/dev/null 2>&1
    if [ $? = 0 ]
    then
      cp Driver.o.$UNAMEINFO $DRVDIR/Driver.o
      # echo "Installed driver for kernel $UNAMEINFO OK"
      $READINSTCAT 28 "$UNAMEINFO"
      exit 0
    fi
  fi

  #
  # try and build the driver again
  #
  ./cc_snalinux > cc_snalinux.out 2>&1
  if [ -f Driver.o.$UNAMEINFO ]
  then
    /sbin/insmod -p -q Driver.o.$UNAMEINFO >/dev/null 2>&1
    if [ $? = 0 ]
    then
      cp Driver.o.$UNAMEINFO $DRVDIR/Driver.o
      # echo "Compiled and installed driver for kernel $UNAMEINFO OK"
      $READINSTCAT 4 "$UNAMEINFO"
      exit 0
    else
      # echo "Kernel isolation layer driver not compatible with kernel"
      # echo "cd $DRVOPTIONS and run ./cc_snalinux <kernel source>"
      $READINSTCAT 38
      $READINSTCAT 3 "$DRVOPTIONS"
      exit 4
    fi
  else
    # echo "Failed to build kernel isolation layer driver"
    # echo "cd $DRVOPTIONS and run ./cc_snalinux <kernel source>"
    $READINSTCAT 26
    $READINSTCAT 3 "$DRVOPTIONS"
    exit 5
  fi
fi

