#
# sna [parameters as sna_commands ]
#
# start/stop command for Communications Server for Linux.
#
# Essentially, parameters are passed unchanged to
# the sna_commands executable.
# However, the drivers must be loaded or sna_commands will
# fail to run. Hence, before running sna_commands
#  sna start - will attempt to start them
#  sna everything else - will check they are loaded.
#
ALLPARMS=$*

#
# 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

if [ x$1 = xstart ]
then
#
# Start Communications Server for Linux
# ensure drivers are loaded
#
  /opt/ibm/sna/bin/snaldmod
  LOADRC=$?

  #
  # Some O/S file systems are bugged such that mknod sometimes fails. The
  # workaround to this is to simply retry a couple of times.
  #
  if [ x$LOADRC = x99 ]
  then
    /opt/ibm/sna/bin/snaulmod
    /opt/ibm/sna/bin/snarmdrv
    /opt/ibm/sna/bin/snaldmod
    LOADRC=$?
    if [ x$LOADRC = x99 ]
    then
      /opt/ibm/sna/bin/snaulmod
      /opt/ibm/sna/bin/snarmdrv
      /opt/ibm/sna/bin/snaldmod
      LOADRC=$?
      if [ x$LOADRC = x99 ]
      then
        /opt/ibm/sna/bin/snaulmod
        /opt/ibm/sna/bin/snarmdrv
        /opt/ibm/sna/bin/snaldmod
        LOADRC=$?
      fi
    fi
  fi
  if [ $LOADRC != 0 ]
  then
    # echo "Communications Server for Linux cannot start because drivers failed to load"
    # echo "Refer to the readme for further information"
    $READINSTCAT 5
    $READINSTCAT 47
    exit $LOADRC
  fi

  #
  # On ppc64 and x86_64 platforms the LiS driver's mknod calls fail during 
  # startup, so we need to do them manually instead.
  #
  if [ `uname -m` = ppc64 -o `uname -m` = x86_64 ]
  then
    if [ ! -e /dev/loop_clone ]; then mknod /dev/loop_clone c 231 234; fi
    if [ ! -e /dev/loop.1     ]; then mknod /dev/loop.1     c 234 1; fi
    if [ ! -e /dev/loop.2     ]; then mknod /dev/loop.2     c 234 2; fi
    if [ ! -e /dev/loop.9     ]; then mknod /dev/loop.9     c 234 9; fi
    if [ ! -e /dev/loop.255   ]; then mknod /dev/loop.255   c 234 255; fi
  fi

else
#
# Other sna commands (e.g. stop, -getsense)
#
  cat /proc/modules | grep sna_linux > /dev/null 2>&1
  if [ $? = 1 ]
  then
    # echo "Communications Server for Linux not running"
    $READINSTCAT 6
    exit 10
  fi

fi

# start daemons
/opt/ibm/sna/bin/sna_commands $ALLPARMS
exit $?

