#!/bin/sh
### BEGIN INIT INFO
# Provides:          elog
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Logbook system to manage notes through a Web interface
# Description:       Logbook system to manage notes through a Web interface
#  The Electronic Logbook (ELOG) provides a Web interface to manage notes.  Its
#  general purpose is to make it easy for people to put and access information
#  online; in the form of short, time stamped text messages with optional HTML
#  markup for presentation, and optional file attachments.  ELOG has its own
#  daemon, there is no need for a full-fledged server (e.g. Apache).  It stores
#  notes as simple as plain text; no special formatting.  Its features:
#  .
#    * Personal logbooks: Personal notes can be written into ELOG and can then be
#      retrieved from anywhere with a Web browser.
#    * Shared logbooks: Logbooks can be shared by several people, for reading and
#      optionally for writing.  Workgroups can share and exchange information like
#      in a (simplified) news group.
#    * Small databases: Since arbitrary attributes can be defined for a logbook,
#      it can be used as a small database with search facilities.
#    * Problem collections: A system can consist of two logbooks, in one of which
#      users enter bugs or problems.  Whenever someone adds a problem, an email is
#      automatically sent to the administrator.
#    * Shift logbooks: If the Allow delete and Allow edit flags are off, an entry
#      cannot be modified once it's been entered.  This can be useful for shift
#      logbooks for example in Physics experiments where each entry becomes a
#      "document" with a time and author stamp.
#    * File collections: Since files can be attached to ELOG entries, the system
#      can be used to store and retrieve files.
### END INIT INFO

# Author: 2014 Roger Kalt <roger.kalt@gmail.com>
# Author: 2003, 2005  Recai Oktaş <roktas@omu.edu.tr>

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/bin:/sbin:/usr/sbin:/usr/bin
DESC="elog daemon"           # Introduce a short description here
NAME=elog                    # Introduce the short server's name here
DAEMON=/usr/sbin/elogd       # Introduce the server's location here
DAEMON_ARGS=""               # Arguments to run the daemon with
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
CONFFILE=${CONFFILE:-/etc/elog.conf}
LOGBOOKDIR=${LOGBOOKDIR:-/var/lib/elog/logbooks}
RESOURCEDIR=${RESOURCEDIR:-/usr/share/elog}
# ELOGVERBOSE="-v"

# Exit if the package is not installed
[ -x $DAEMON ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Add the options to argument list only if defined previously.  Since
# some options may also be present in the conffile, we couldn't preset
# those options which would otherwise overwrite the settings in the
# conffile.  Also note that, all have reasonable compiled-in defaults.
ARGS="${PIDFILE+"-f $PIDFILE"}         \
      ${CONFFILE+"-c $CONFFILE"}       \
      ${LOGBOOKDIR+"-d $LOGBOOKDIR"}   \
      ${RESOURCEDIR+"-s $RESOURCEDIR"} \
      ${PORT+"-p $PORT"}               \
      ${HOST+"-n $HOST"}               \
      ${ELOGVERBOSE+"-v"}"
      
# Always run as a daemon.
DAEMON_ARGS=`$DAEMON_ARGS echo $ARGS -D`

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# override VERBOSE setting:
VERBOSE="yes"

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start  --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
	start-stop-daemon --start  --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
		|| return 2
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	
	# immediately kill elogd
	killall $DAEMON
	rm -f $PIDFILE
	return '0'
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# The daemon can reload its configuration with HUP:
	killall HUP $DAEMON	
	return 0
}

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
    do_start
    case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
  ;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
       status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#log_daemon_msg "Reloading $DESC" "$NAME"
	#do_reload
	#log_end_msg $?
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
