#!/bin/sh
#
# Startup script for the novfs Daemon
#
# /etc/init.d/novfsd
#
# description: novfs daemon provides 
# the interface for the novfs file
# system to the xtier libraries.
#

# processname: novfsd
# pidfile: None
# config utility: None


### BEGIN INIT INFO
# Provides: novfsd
# Required-Start: $local_fs $network $remote_fs $syslog $network-remotefs
# Required-Stop: $local_fs $network $remote_fs $syslog $network-remotefs
# Should-Start: novell-xregd
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: novfs Daemon
# Description: Start novfs Daemon
### END INIT INFO

#
# LSB and RH have killproc and pidofproc in common

MyStatus()
{
  /sbin/checkproc $DAEMON
  RVAL=$?
}

if [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  START_DAEMON=start_daemon
  STATUS=MyStatus
  LOG_SUCCESS=log_success_msg
  LOG_FAILURE=log_failure_msg
  LOG_WARNING=log_warning_msg
elif [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
  START_DAEMON=daemon
  STATUS=status
  LOG_SUCCESS=success
  LOG_FAILURE=failure
  LOG_WARNING=passed
else
  echo "Error: your platform is not supported by $0" > /dev/stderr
  exit 1
fi

NOVFS_MOUNT_PATH=/var/opt/novell/nclmnt
DAEMON=/opt/novell/ncl/bin/novfsd
SHORTNAME=novfsd

StartDAEMON()
{
  RVAL=0

  # Update the limit parameters
  ulimit -c unlimited
  ulimit -n 4096
  ulimit -f unlimited
  
  # Setup environment needed for xtier
  ARCH_SUFFIX=`uname -p | grep x86_64 | cut -b 5,6`
  export LD_LIBRARY_PATH="\${LD_LIBRARY_PATH}":/opt/novell/xtier/lib$ARCH_SUFFIX:/opt/novell/nmas/client/lib$ARCH_SUFFIX
  export XTIER_CODE_PAGE=`locale charmap`
  export NCPL_DO_NOT_OVERWRITE_OPENLOG=1

  #Check to see if daemon is already running
  MyStatus
  if [ "x$RVAL" = "x0" ]; then
    echo "novfs daemon already running..."
    exit 0
  fi

  # Start the daemon
  echo "Starting Novell novfs daemon..."

  #Try and start novfs
  modprobe novfs

  lsmod | grep novfs 2>&1 >/dev/null
  if [ $? -ne 0 ]; then
    depmod
    modprobe novfs
  fi

  #Mount our filesystem
  mount -t novfs novfs $NOVFS_MOUNT_PATH

  EnableNMAS=1
  if [ -f /etc/opt/novell/ncl/login.conf ]; then
    cat /etc/opt/novell/ncl/login.conf | grep "NMAS_Authentication=false" 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
       EnableNMAS=0
    fi
  fi

  if [ $EnableNMAS -eq 0 ]; then
    $START_DAEMON $DAEMON -m > /dev/stderr
  else
    $START_DAEMON $DAEMON  > /dev/stderr
  fi

  RVAL=$?

  rc_failed $RVAL
  rc_status -v
}

StopDAEMON()
{
  RVAL=0

  #Check to see if daemon is already running
  MyStatus
  if [ ! "x$RVAL" = "x0" ]; then
    echo "novfs daemon not running..."
  else
    echo "Stopping Novell novfs daemon..."

    killproc ncl_tray
    killproc $SHORTNAME

    #Check to see if mount point is still mounted
    mount | grep novfs 2>&1 > /dev/null
    if [ $? -eq 0 ]; then
      umount $NOVFS_MOUNT_PATH 2>/dev/null
    fi
  fi

  RVAL=$?

  rc_failed $RVAL
  rc_status -v
}

case "$1" in
start)
  StartDAEMON
  ;;
stop)
  StopDAEMON
  ;;
restart|reload|force-reload)
  StopDAEMON
  sleep 3
  StartDAEMON
  ;;
status)
  $STATUS
  rc_failed $RVAL
  rc_status -v
  ;;
*)
  echo -n "Usage: $0 <start|stop|restart|reload|force-reload>" > /dev/stderr
  echo ""
  RVAL=1
  ;;
esac

rc_exit
