#!/bin/sh
### BEGIN INIT INFO
# Provides:          foswiki
# Required-Start:    $syslog $remote_fs $network
# Required-Stop:     $syslog $remote_fs $network
# Should-Start:      fam
# Should-Stop:       fam
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the foswiki backend server.
### END INIT INFO

DESC="Foswiki backend server"
NAME=foswiki

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# Tailor the USER and GROUP settings appropriate for your installation
# Typical settings:
#  RedHat, Fedora, CentOS, Gentoo, Mandriva USER=apache   GROUP=apache
#  debian, Ubuntu, Kubuntu                  USER=www-data GROUP=www-data
#  Suse:                                    USER=wwwrun   GROUP=www
#  FreeBSD:                                 USER=www      GROUP=wwwadmin

USER=www-data
GROUP=www-data
HOME=/var/www

# The following defaults are overridden in etc/default/foswiki

FOSWIKI_ROOT=$HOME/foswiki
FOSWIKI_FCGI=foswiki.fcgi
FOSWIKI_BIND=127.0.0.1:9000
FOSWIKI_CHILDREN=3
FOSWIKI_MAX_REQUESTS=-1
FOSWIKI_MAX_SIZE=250000
FOSWIKI_CHECK_SIZE=10
FOSWIKI_QUIET=true
FOSWIKI_PNAME=foswiki
FOSWIKI_WARMING=1

FOSWIKI_PIDFILE=/var/run/$NAME.pid

# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
    . /etc/default/$NAME
fi

if [ ! -d $FOSWIKI_ROOT ]; then
    exit
fi

cd $HOME || exit 3
export HOME

if [ -d $HOME/.plenv ]; then
  export PATH=$HOME/.plenv/bin:$PATH
  eval "$(plenv init -)"
fi

if [ -d $HOME/local/lib ]; then
  export LD_LIBRARY_PATH=$HOME/local/lib
fi

FOSWIKI_DAEMON=$FOSWIKI_ROOT/bin/$FOSWIKI_FCGI
FOSWIKI_DAEMON_OPTS="-n $FOSWIKI_CHILDREN -l $FOSWIKI_BIND -p $FOSWIKI_PIDFILE -d -c $FOSWIKI_CHECK_SIZE -x $FOSWIKI_MAX_REQUESTS -s $FOSWIKI_MAX_SIZE -a $FOSWIKI_PNAME -w $FOSWIKI_WARMING"

if $FOSWIKI_QUIET; then
  FOSWIKI_DAEMON_OPTS="$FOSWIKI_DAEMON_OPTS -q"
fi


start() {
        log_daemon_msg "Starting $DESC" $NAME
        if test -f $FOSWIKI_PIDFILE; then
          log_failure_msg "There's a $DESC already running."
	else 
	  :> $FOSWIKI_PIDFILE
	  chown $USER:$GROUP $FOSWIKI_PIDFILE
	  if ! start-stop-daemon --start --oknodo --quiet \
	      --chuid $USER:$GROUP \
	      --chdir $FOSWIKI_ROOT/bin \
	      --pidfile $FOSWIKI_PIDFILE \
	      --exec $FOSWIKI_DAEMON -- $FOSWIKI_DAEMON_OPTS
	  then
	      log_end_msg 1
	  else
	      log_end_msg 0
	  fi
	fi
}

force_stop() {
        log_daemon_msg "Killing $DESC" $NAME
        if start-stop-daemon --stop --signal KILL --retry 30 --oknodo --quiet --pidfile $FOSWIKI_PIDFILE 
        then
            rm -f $FOSWIKI_PIDFILE
            log_end_msg 0
        else
            log_end_msg 1
        fi
}

stop() {
        log_daemon_msg "Stopping $DESC" $NAME
        if start-stop-daemon --stop --signal TERM --retry 30 --oknodo --quiet --pidfile $FOSWIKI_PIDFILE 
        then
            rm -f $FOSWIKI_PIDFILE
            log_end_msg 0
        else
            log_end_msg 1
        fi
}

reload() {
        log_daemon_msg "Reloading $DESC" $NAME
        if start-stop-daemon --stop --signal HUP --oknodo --quiet --pidfile $FOSWIKI_PIDFILE 
        then
            log_end_msg 0
        else
            log_end_msg 1
        fi
}

status() {
	status_of_proc -p "$FOSWIKI_PIDFILE" "$FOSWIKI_DAEMON" $NAME 
}

. /lib/lsb/init-functions

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  force-stop)
    force_stop
    ;;
  reload) 
    reload
    ;;
  restart)
    stop
    start
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: $NAME {start|stop|force-stop|restart|reload|status}"
    exit 1
    ;;
esac
