#!/bin/sh
# Startup script for the idsd
#
# chkconfig: 2345 90 10
# description: idsd is a daemon for Novell iPrint
# processname: idsd

# pidfile: /var/opt/novell/run/idsd.pid
# config: /etc/opt/novell/iprint/conf/idsd.conf

DAEMON="/opt/novell/iprint/bin/idsd"
OPTIONS=""
CONF_FILE="/etc/opt/novell/iprint/conf/idsd.conf"

if [ -f "/var/opt/novell/run/iprint/idsd.pid" ]; then
   # Our new location
   PIDFILE="/var/opt/novell/run/iprint/idsd.pid"
elif [ -f "/var/opt/novell/run/idsd.pid" ]; then
   # The old location
   PIDFILE="/var/opt/novell/run/idsd.pid"
else
   # Default to our new location
   PIDFILE="/var/opt/novell/run/iprint/idsd.pid"
fi

### BEGIN INIT INFO
# Provides: novell-idsd
# Required-Start: $local_fs $network $syslog ndsd namcd
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 1 4 6
# Short-Description: Novell iPrint Printer Manager
# Description: idsd Novell iPrint module
### END INIT INFO

#
# LSB and RH have killproc and pidofproc in common

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

if [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  alias START_DAEMON=start_daemon
  alias STATUS=MyStatus
  alias LOG_SUCCESS=log_success_msg
  alias LOG_FAILURE=log_failure_msg
  alias LOG_WARNING=log_warning_msg
  alias LOG_SKIPPED="rc_status -s"
elif [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
  alias START_DAEMON=daemon
  alias STATUS=MyStatus # for now, we report status with MyStatus()
  alias LOG_SUCCESS="success; echo"
  alias LOG_FAILURE="failure; echo"
  alias LOG_WARNING="passed; echo"
  alias LOG_SKIPPED="skipped; echo"
else
  echo "Error: your platform is not supported by $0" > /dev/stderr
  exit 1
fi

MyStatus()
{
  if ! [ -f "$CONF_FILE" ] ; then
     RVAL=6
  elif [ -f $PIDFILE ]; then
    MYPID=`cat $PIDFILE`
    ps -p $MYPID | grep $MYPID > /dev/null 2>&1
    if [ "x$?" = "x0" ]; then
      RVAL=0
    else
      RVAL=3
    fi
  else
    RVAL=3
  fi
  return $RVAL
}

StartIDSD()
{
  echo -n "Starting Novell iPrint DriverStore:"
  if ! [ -f "$CONF_FILE" ] ; then
     echo -e -n " ... service not configured"
     RVAL=6
  else
     export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/novell/eDirectory/eDir-exclusive/lib64
     $DAEMON $OPTIONS
     RVAL=$?
  fi
  if [ $RVAL -eq 0 ]; then
    LOG_SUCCESS
  elif [ $RVAL -eq 6 ]; then
    LOG_SKIPPED
  else
    LOG_FAILURE
  fi
}

StopIDSD()
{
  echo -n "Shutting down Novell iPrint DriverStore"
  if [ -f $PIDFILE ]; then
      PID=`cat $PIDFILE`
      #(kill `cat $PIDFILE` && rm -f $PIDFILE) > /dev/null 2>&1
      kill $PID > /dev/null 2>&1
      RVAL=$?
      ITTR=0
      while ps -C $(basename ${DAEMON}) > /dev/null 2>&1 ; do
         echo -n "."
         if [ ${ITTR} -gt 29 ]; then
            RVAL=1
            break
         fi
         ITTR=$(( ${ITTR} + 1 ))
         sleep 1
      done
      if ps -p $PID > /dev/null 2>&1; then
         # we're still running, something's wrong
         echo -e -n " ... error shutting down $(basename ${DAEMON})"
         kill -9 $PID > /dev/null 2>&1
      fi
      rm -f $PIDFILE > /dev/null 2>&1
  elif ! pidof idsd > /dev/null 2>&1; then
      RVAL=0
  else
    RVAL=1
  fi
  if [ $RVAL -eq 0 ]; then
    LOG_SUCCESS
  else
    LOG_FAILURE
  fi
}

case "$1" in
start)
  StartIDSD
  ;;
stop)
  StopIDSD
  ;;
restart)
  StopIDSD
  sleep 1
  StartIDSD
  ;;
try-restart|condrestart)
  ## Do a restart only if the service was active before.
  ## Note: try-restart is now part of LSB (as of 1.9).
  ## RH has a similar command named condrestart.
  if test "$1" = "condrestart"; then
    echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
  fi
  $0 status
  if test $? = 0; then
    $0 restart
  else
    rc_reset	# Not running is not a failure.
  fi
  # Remember status and be quiet
  rc_status
  ;;
reload|force-reload)
  # for now, we do the same as restart.
  StopIDSD
  sleep 1
  StartIDSD
  ;;
status)
  STATUS
  if [ $RVAL -eq 0 ]; then
     echo "$(basename $DAEMON) is running: $(grep IDSObjectDN /etc/opt/novell/iprint/conf/idsd.conf | sed -e "s/IDSObjectDN \(.*$\)/\1/g")"
  elif [ $RVAL -eq 3 ]; then
     echo "$(basename $DAEMON) is not running"
  elif [ $RVAL -eq 6 ]; then
     echo "$(basename $DAEMON) is not configured"
  fi
  ;;
*)
  echo "Usage: $0 start|stop|restart|try-restart|reload|force-reload|status" > /dev/stderr
  RVAL=1
  ;;
esac

exit $RVAL


