#
# snatrydrivers
#
# Adds correct Kernel Interface Driver for this version of LINUX
# - Checks for LiS (as cannot Probe CS 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/snapix0
DRIVEROK=1

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
  /sbin/insmod streams
  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 CS 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

cd $DRVOPTIONS
#
# 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

#
# 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 2>&1 | tee cc_snalinux.out
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

