#!/bin/sh
# -----------------------------------------------------------------------------
# First coded by Luca - De Whiskey's - De Vitis <luca@debian.org>
# -----------------------------------------------------------------------------
# $Id: zope.init,v 1.6 2003/09/26 21:10:39 luca Exp $
#
#   Copyright (C) 2003 Luca - De Whiskey's - De Vitis <luca@debian.org>
#
#   This program 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 of the License, or
#   (at your option) any later version.
#
#   This program 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; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

case "$1" in
    start|stop|restart|force-reload) ;; #later
    *)
        echo "Usage: $0 {start|stop|restart|force-reload}"
        exit 1
    ;;
esac

zope=zope2.7

D=/var/lib/$zope/instance

[ -d $D -a -d /usr/lib/$zope ] || exit 0

. /etc/default/$zope

cd $D

p=''; [ "$1" = "stop" ] && p='p'

if [ "$INSTANCES" = "NONE" -o  "$INSTANCES" = "" ]; then
        echo "$0 does not $1 any instance - edit /etc/default/$zope to enable automatic $1${p}ing of instances ."
        exit 0
elif [ "$INSTANCES" = ALL ]; then
      INSTANCES=$(echo *)
fi
if [ "$INSTANCES" = '*' ]; then    
    if [ "$1" != stop ] ; then
	echo "There are currently no $zope instances in $D . To create them, use mk${zope}instance "
    fi
    exit 0
fi

echo Zope ${1}${p}ing all instances....
for i in $INSTANCES ; do
	    if [ -x $i/bin/zopectl ] ; then
		echo -n $i/bin/zopectl "$1"  " ... "
		$i/bin/zopectl "$1"
		echo
            fi
	    if [ -x $i/bin/zeoctl ] ; then
		echo -n $i/bin/zeoctl "$1"  " ... "
		$i/bin/zeoctl "$1"
		echo
	    fi
	    if ! [ -x $i/bin/zeoctl -o -x $i/bin/zopectl ] ; then 
		echo  "'"$i"' is an old/purged instance, not ${1}${p}ed"
		# use mkzopeinstance to upgrade it
	    fi
done


    # I will include this in a future version
    #poll-restart) 
    # 	for i in $INSTANCES ; do
    #        ${zope}_poll_restart  $i         
    #	done
    #;;

exit 0
