#!/bin/sh
# initscript for ZIDESTORE_INIT_VERSION
# <frank@opengroupware.org>
#
### BEGIN INIT INFO
# Provides: ZIDESTORE_INIT_VERSION
# Required-Start: $network postgresql apache2
# Required-Stop: 
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: ZideStore Server - ZIDESTORE_INIT_VERSION
### END INIT INFO

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

#reset at first...
rc_reset

RETVAL=0
PROG="ZIDESTORE_INIT_VERSION"
USER="ogo"
DIR="ZIDESTORE_INIT_PREFIX"
BIN="ZIDESTORE_INIT_PREFIX/sbin/ZIDESTORE_INIT_VERSION"
PID_FILE="/var/run/ZIDESTORE_INIT_VERSION.pid"
LOGERR="/var/log/opengroupware/ZIDESTORE_INIT_VERSION-err.log"
LOGOUT="/var/log/opengroupware/ZIDESTORE_INIT_VERSION-out.log"
PLIST="ZideStore"
SLEEP=3

# pull in sysconfig settings
# 
[ -f /etc/sysconfig/ogo-zidestore ] && . /etc/sysconfig/ogo-zidestore

start()
{
  PID=`/sbin/pidof "${BIN}"`
  if [ -n "${PID}" ]; then
    echo -en "${PROG} already runs!"
  else
    echo -en $"Starting ${PROG}:"
    su - ${USER} -s /bin/bash -c "export PGCLIENTENCODING=${PGCLIENTENCODING}; exec ${BIN} 2>>${LOGERR} 1>>${LOGOUT} &"
    sleep ${SLEEP}
    echo $! > ${PID_FILE}
    PID=`/sbin/pidof "${BIN}"`
    if [ -n "${PID}" ]; then
      touch /var/lock/subsys/${PROG}
    else
      rm -f ${PID_FILE}
      rm -f /var/lock/subsys/${PROG}
    fi
  fi
}

status()
{
  echo -en $"Checking for: ${PROG}"
  checkproc -p ${PID_FILE} ${BIN}
}

stop()
{
  echo -en $"Stopping ${PROG}:"
  if [ -f "${PID_FILE}" ] ; then
    PID=`cat ${PID_FILE}`
    if [ "`kill ${PID} 2>>${LOGERR} 1>>${LOGOUT}`" ] ; then
      rm -f ${PID_FILE}
      rm -f /var/lock/subsys/${PROG}
    else
      PID="`pgrep -f ${BIN}`"
      kill -9 ${PID}
      rm -f ${PID_FILE}
      rm -f /var/lock/subsys/${PROG}
    fi
  else
    killproc ${PROG}
    RETVAL=$?
    if [ ${RETVAL} -eq 0 ]; then
      rm -f /var/lock/subsys/${PROG}
      rm -f ${PID_FILE}
    fi
  return ${RETVAL}
  fi
}

showdefaults()
{
  su - ${USER} -s /bin/bash -c "exec Defaults read ${PLIST}"
}
  


restart()
{
  stop
  sleep ${SLEEP}
  rc_status -v
  start
}

[ -x ${BIN} ] || exit 0

# See how we were called.
case "$1" in
  start)
  start
  rc_status -v
  ;;
  stop)
  stop
  sleep ${SLEEP}
  rc_status -v
  ;;
  restart)
  restart
  rc_status -v
  ;;
  status)
  status
  rc_status -v
  ;;
  showdefaults)
  showdefaults
  rc_status -v
  ;;
  *)
  echo $"Usage: $0 {start|stop|restart|showdefaults|status}"
  exit 1
esac

exit 0