#!/bin/sh
# Startup script for a oood server based on zope startup script
#
# chkconfig: 2345 81 19
# description: oood, the standalone server for converting various types of docs to/from OpenOffice format

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

NAME="oood"

#LOCKDIR=/var/lock/subsys

HOME=/var/lib/oood
PIDDIR=/var/run/oood
LOCKFILE=$PIDDIR/server_pid.lock
PIDFILE=$LOCKFILE


start() {
  if [ -f $LOCKFILE ] ; then
    gprintf "Another instance of %s is running (lockfile exists)." "$NAME"
    echo_failure
    echo
    exit 4
  fi

  gprintf "Starting %s: " "$NAME"
  # Initialize the Pool
  python $HOME/start.py --init > /dev/null 2>&1 /dev/null
  # Start the server in the background
  python $HOME/runserw.py > /dev/null &
  sleep 2
  PID=`cat $PIDFILE`
  echo $PID
  if [ $PID ]; then
    echo_success
  else
    gprintf "%s is not running (no pid file found)." "$NAME"
    killproc "python $HOME/runserw.py"
    python $HOME/start.py --flush > /dev/null 2>&1 /dev/null
    echo_failure
  fi
  echo
}


stop() {
  if [ ! -f $LOCKFILE ] ; then
    gprintf "%s is not running (no lockfile found)." "$NAME"
    echo_failure
  else
    gprintf "Stopping %s daemon: " "$NAME"
    killproc "python $HOME/runserw.py" && echo_success || echo_failure
  fi
  # Clean up in any case
  rm -f $LOCKFILE
  python $HOME/start.py --flush > /dev/null 2>&1 /dev/null
  echo
}


status() {
  python $HOME/start.py --status
}


case "$1" in
  start)
    touch /var/lock/subsys/$NAME
    start
    ;;

  stop)
    stop
    rm -f /var/lock/subsys/$NAME
    ;;

  restart)
    stop
    start
    ;;

  status)
    status
    ;;
  *)

  gprintf "Usage: %s\n" "$0 {start|stop|restart|status}"
  exit 1

esac

exit
