#!/usr/bin/python
#
# Don't remove the following two lines used for the RedHat chkconfig framework.
#chkconfig: 2345 80 01
#description: Zope servers
#
# (C) Copyright 2006 Nuxeo SAS <http://nuxeo.com>
# Authors:
# Stefane Fermigier <sf@nuxeo.com>
# M.-A. Darche <madarche@nuxeo.com>
# Ruslan Spivak <rspivak@nuxeo.com>
#
# $Id: zopes 33716 2006-03-02 12:32:50Z madarche $
"""A script to use in /etc/init.d/ to start and stop the multiple Zope instances
(simple and ZEO instances alike) that might be hosted on a server.

Steps to have this script functional:

- Be sure that "effective-user zope" is present in etc/zope.conf of each
  instance.

- Copy this script into /etc/init.d/

- Register this script in the various runlevels of the server.

  Usually Zope instances should be started from runlevel 2 to 5 and stopped in
  levels 0, 1 and 6.

  Debian
  ------

  On a Debian server the registration should be done with the following
  command:
  
  update-rc.d -f zopes start 92 2 3 4 5 . stop 92 0 1 6 .

  The 92 number is the order number to start the instances after the Apache
  httpd service.

  RedHat
  ------

  On a RedHat or Fedora server the registration should be done with the
  following command:

  chkconfig --add zopes

  This command line can only work if chkconfig and description comment lines are
  present at the beginning of the script:

  #chkconfig: 2345 80 01
  #description: Zope servers

  Those lines denote that the script will start from runlevels 2 to 5.
  Start priority will be set to 80 and stop priority will be 01.

  To see info about this service:
  $ chkconfig --list zopes
  zopes         0:off   1:off   2:on    3:on    4:on    5:on    6:off

  One can manually switch on/off service to/from runlevel with --levels on/off
  $ chkconfig --level 2 zopes off
  $ chkconfig --list zopes
  zopes         0:off   1:off   2:off   3:on    4:on    5:on    6:off
"""

import sys, os

INSTANCES = [
    '/usr/local/zope/instance/mysite.net',
    '/usr/local/zope/instance/myothersite.net',
    '/usr/local/zope/instance/myothersite.com',
    '/usr/local/zope/instance/myothersite.org',
]


if len(sys.argv) > 1:
    arg = sys.argv[1]
else:
    arg = ''

if not arg in ('start', 'stop', 'restart', 'status'):
    print "Usage: /etc/init.d/zopes {start|stop|restart|status}"
    sys.exit(1)

for instance in INSTANCES:
    print instance, ":"
    for cmd in ('zopectl', 'zeoctl'):
        full_cmd = "%s/bin/%s" % (instance, cmd)
        if os.path.exists(full_cmd):
            break
    os.system("%s %s" % (full_cmd, arg))

