#!/bin/sh
#
# rozofs-exportd       This shell script takes care of starting and stopping
#                      the exportd services.
#
#chkconfig: 35 20 80
#description: rozofs exportd server
#processname: exportd

### BEGIN INIT INFO
# Provides:          rozofs-exportd
# Required-Start:    $network $local_fs $remote_fs $portmap
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RozoFS exportd server
# Description:       RozoFS is a scale-out NAS file system. This service
#                    provides the rozofs-exportd server functionality.
### END INIT INFO

RETVAL=0
exec=/usr/bin/exportd
prog="rozofs-exportd"
pidfile=/var/run/exportd.pid
launcher=/usr/bin/rozolauncher
config=/etc/rozofs/export.conf

# Source function library.
. /etc/rc.d/init.d/functions

rh_status()
{
  # run checks to determine if the service is running or use generic status
  status -p "$pidfile" "$exec"
}

rh_status_q()
{
  rh_status >/dev/null 2>&1
}

start()
{
  # user had insufficient privilege
  if [ $UID -ne 0 ] ; then
      echo "User has insufficient privilege."
      exit 4
  fi

  # program is not installed
  [ -x $exec ] || exit 5
  [ -x $launcher ] || exit 5

  # program is not configured
  [ -f $config ] || exit 6

  echo -n $"Starting $prog: "
  daemon $exec
  retval=$?
  echo
}

stop()
{
  # user had insufficient privilege
  if [ $UID -ne 0 ] ; then
    echo "User has insufficient privilege."
    exit 4
  fi

  echo -n $"Stopping $prog: "

  if [ -n "`pidfileofproc $exec`" ]; then
    killproc -p $pidfile $exec
    RETVAL=3
  else
    failure $"Stopping $prog"
  fi
  retval=$?
  echo

  [ $retval -eq 0 ] && rm -f $lockfile
}

restart()
{
  rh_status_q && stop
  start
}

force_reload()
{
  # new configuration takes effect after restart
  restart
}

reload()
{
  echo -n $"Reloading $prog: "

  if [ -n "`pidfileofproc $exec`" ]; then
    killproc -p $pidfile $exec -HUP
  else
    failure $"Reloading $prog"
  fi

  retval=$?
  echo
}

case "$1" in

  start)
    rh_status_q && exit 0
    start
    ;;

  restart)
    restart
    ;;

  status)
    rh_status
    ;;

  force-reload)
    force_reload
    ;;

  stop)
    rh_status_q || exit 0
    stop
    ;;

  reload)
    reload
    ;;

  condrestart|try-restart)
    rh_status_q || exit 0
    restart
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
    exit 2
    ;;
esac

exit $?
