#!/bin/bash
### BEGIN INIT INFO
# Provides:          ocsmanager
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OCSManager server
### END INIT INFO

# OCSManager init script for Debian GNU/Linux
#
# Copyright (C) 2012 Inverse inc.
#
# Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME=ocsmanager
DAEMON=/usr/bin/paster
DESC="OCSManager application"

# OCSManager must be run as root, for now
USER=root

CONFFILE=/etc/ocsmanager/$NAME.ini
PIDFILE=/var/run/ocsmanager/$NAME.pid
LOGFILE=/var/log/ocsmanager/$NAME.log

if [ ! -x $DAEMON ]; then
    echo "$DAEMON is not executable."
    exit 1
fi

set -e

case "$1" in
  start)
	echo "Starting $DESC"

        # Ensure directory's existence and permissions
        install -o $USER -g adm -m 755 -d /var/run/$NAME
        install -o $USER -g adm -m 750 -d /var/log/$NAME

	$DAEMON serve $CONFFILE --daemon --pid-file $PIDFILE --log-file $LOGFILE || /bin/true
	;;
  stop)
	echo "Stopping $DESC"
	$DAEMON serve $CONFFILE --stop-daemon --pid-file $PIDFILE || /bin/true
	;;
  restart|force-reload)
	echo "Restarting $DESC"
	$DAEMON serve $CONFFILE --stop-daemon --pid-file $PIDFILE || /bin/true
	$DAEMON serve $CONFFILE --daemon --pid-file $PIDFILE --log-file $LOGFILE || /bin/true
	;;
  status)
	$DAEMON serve $CONFFILE --status --pid-file $PIDFILE
	;;
  *)
	echo "Usage: $NAME {start|stop|restart|status}" >&2
	exit 1
	;;
esac

exit 0
