#
# snaldmod
#
# Load modules if not already loaded
# Use snatrydrivers to find driver if one not available
#
LOGFILE=/var/opt/ibm/sna/snaldmod.out

if [ "x$READINSTCAT" = "x" ]
then
  #
  # Not run from other 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

#
# Exit silently if Driver is already running
#
cat /proc/modules | grep sna_linux > /dev/null 2>&1
if [ $? = 0 ]
then
  exit 0
fi

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

#
# Ensure streams is loaded
# (this is done by snatrydrivers)

#
# Call snatrydrivers to ensure compatable
# Kernel Isolation Driver
#
/opt/ibm/sna/bin/snatrydrivers
TRYRC=$?
if [ $TRYRC != 0 ]
then
  exit $TRYRC
fi

rm $LOGFILE 2>/dev/null
if [ $KERNEL26 = 0 ]
then
  # Load up the Isolation Layer driver
  /sbin/insmod -f -o sna_linux /etc/opt/ibm/sna/drivers/snapix0/Driver.o > $LOGFILE 2>&1
fi

# Load up the Trace driver
if [ $KERNEL26 = 1 ]
then
  /sbin/insmod /etc/opt/ibm/sna/drivers/snapixt/snapixt.ko > $LOGFILE 2>&1
else
  /sbin/insmod -f -o sna_trace /opt/ibm/sna/drivers/snapixt/Driver.o >> $LOGFILE 2>&1
fi

# Create the trace device entry
MAJOR=`awk '/sna_trace/ { print $1 }' /proc/devices`
MINOR=0
rm /dev/sna_trace > /dev/null 2>&1
mknod /dev/sna_trace c $MAJOR $MINOR 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi


# Find out the major number of the clone device
CLONEMAJOR=`awk '/clone-drvr/ { print $1 }' /proc/devices`

# Load up the router
if [ $KERNEL26 = 1 ]
then
  /sbin/insmod /etc/opt/ibm/sna/drivers/snapixr/snapixr.ko >> $LOGFILE 2>&1
else
  /sbin/insmod -f -o streams-sixr /opt/ibm/sna/drivers/snapixr/Driver.o >> $LOGFILE 2>&1
fi

# Create device entries for the router
MAJOR=`awk '/sixr/ { print $1 }' /proc/devices`
rm /dev/sna_v5access > /dev/null 2>&1
mknod /dev/sna_v5access c $CLONEMAJOR $MAJOR 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi
rm /dev/sna_v5router > /dev/null 2>&1
mknod /dev/sna_v5router c $MAJOR 0 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi
chmod a+rw /dev/sna_v5access

# Load up the LAN glue
if [ $KERNEL26 = 1 ]
then
  /sbin/insmod /etc/opt/ibm/sna/drivers/snapixy/snapixy.ko >> $LOGFILE 2>&1
else
  /sbin/insmod -f -o streams-sixy /opt/ibm/sna/drivers/snapixy/Driver.o >> $LOGFILE 2>&1
fi

# Create the LAN glue device
MAJOR=`awk '/sixy/ { print $1 }' /proc/devices`
rm /dev/sna_LLG > /dev/null 2>&1
mknod /dev/sna_LLG c $CLONEMAJOR $MAJOR 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi

# Load up the SNAP-LLC2 driver
if [ $KERNEL26 = 1 ]
then
  /sbin/insmod /etc/opt/ibm/sna/drivers/snapixn/snapixn.ko >> $LOGFILE 2>&1
else
  /sbin/insmod -f -o streams-sixn /opt/ibm/sna/drivers/snapixn/Driver.o >> $LOGFILE 2>&1
fi

# Create the SNAP-LLC2 device entry
MAJOR=`awk '/sixn/ { print $1 }' /proc/devices`
rm /dev/sna_LAN > /dev/null 2>&1
mknod /dev/sna_LAN c $CLONEMAJOR $MAJOR 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi

# Load up the SNAP-IPDLC driver
if [ $KERNEL26 = 1 ]
then
  /sbin/insmod /etc/opt/ibm/sna/drivers/snapixi/snapixi.ko >> $LOGFILE 2>&1
else
  /sbin/insmod -f -o streams-sixi /opt/ibm/sna/drivers/snapixi/Driver.o >> $LOGFILE 2>&1
fi

# Create the SNAP-IPDLC device entry
MAJOR=`awk '/sixi/ { print $1 }' /proc/devices`
rm /dev/sna_HPRIP > /dev/null 2>&1
mknod /dev/sna_HPRIP c $CLONEMAJOR $MAJOR 2>> $LOGFILE
if [ $? != 0 ]
then
  exit 99
fi

# Load up the SDLC mapping
# Test to see if SDLC will load before actually trying it for real
grep S_Dlc /proc/k*syms > /dev/null 2> /dev/null
SRC=$?
if [ $SRC = 0 ]
then
  if [ $KERNEL26 = 1 ]
  then
    /sbin/insmod /etc/opt/ibm/sna/drivers/snapixs/snapixs.ko >> $LOGFILE 2>&1
    SRC=$?
  else
    /sbin/insmod -f -o streams-sixs /opt/ibm/sna/drivers/snapixs/Driver.o >> $LOGFILE 2>&1
    SRC=$?
  fi

  if [ $SRC = 0 ]
  then
    # Create the SDLC mapping entry
    MAJOR=`awk '/sixs/ { print $1 }' /proc/devices`
    rm /dev/sna_SDLC > /dev/null 2>&1
    mknod /dev/sna_SDLC c $CLONEMAJOR $MAJOR 2>> $LOGFILE
    if [ $? != 0 ]
    then
      exit 99
    fi
  fi
fi

# Load up the QLLC mapping
# Test to see if QLLC will load before actually trying it for real
grep Q_Dlc /proc/k*syms > /dev/null 2> /dev/null
QRC=$?
if [ $QRC = 0 ]
then
  if [ $KERNEL26 = 1 ]
  then
    /sbin/insmod /etc/opt/ibm/sna/drivers/snapixq/snapixq.ko >> $LOGFILE 2>&1
    QRC=$?
  else
    /sbin/insmod -f -o streams-sixq /opt/ibm/sna/drivers/snapixq/Driver.o >> $LOGFILE 2>&1
    QRC=$?
  fi

  if [ $QRC = 0 ]
  then
    # Create the QLLC mapping entry
    MAJOR=`awk '/sixq/ { print $1 }' /proc/devices`
    rm /dev/sna_QLLC > /dev/null 2>&1
    mknod /dev/sna_QLLC c $CLONEMAJOR $MAJOR 2>> $LOGFILE
    if [ $? != 0 ]
    then
      exit 99
    fi
  fi
fi

# Load up the DLC to Channel mapper
# Test to see if CTC will load before actually trying it for real
grep ctc_mpc_ /proc/k*syms > /dev/null 2> /dev/null
CRC=$?
if [ $CRC = 0 ]
then
  if [ $KERNEL26 = 1 ]
  then
    /sbin/insmod /etc/opt/ibm/sna/drivers/snapixc/snapixc.ko >> $LOGFILE 2>&1
    CRC=$?
  else
    /sbin/insmod -f -o streams-sixc /opt/ibm/sna/drivers/snapixc/Driver.o >> $LOGFILE 2>&1
    CRC=$?
  fi
  
  if [ $CRC = 0 ]
  then
    # Create the DCL to Channel mapper device entry
    MAJOR=`awk '/sixc/ { print $1 }' /proc/devices`
    rm /dev/sna_DCM > /dev/null 2>&1
    mknod /dev/sna_DCM c $CLONEMAJOR $MAJOR 2>> $LOGFILE
    if [ $? != 0 ]
    then
      exit 99
    fi
  fi
else
  # echo "CTC channel driver not available"
  $READINSTCAT 56
fi

mv /var/opt/ibm/sna/ksyms.gz /var/opt/ibm/sna/ksyms.bak.gz >>  $LOGFILE 2>&1
echo `date` > /var/opt/ibm/sna/ksyms
if [ $KERNEL26 = 1 ]
then
  cat /proc/kallsyms | sort >> /var/opt/ibm/sna/ksyms
else
  cat /proc/ksyms | sort >> /var/opt/ibm/sna/ksyms
fi
gzip /var/opt/ibm/sna/ksyms

exit 0
