#!/bin/bash
#
# mongrel_cluster Startup script for mongrel clusters
#
# chkconfig: - 85 15
# description: The mongrel_cluster manages multiple mongrel processes \
#              for the use behind a load balancer or a proxy server.
# processname: mongrel_rails
# config: /etc/mongrel_cluster/*.yml
# config: /etc/mongrel_cluster/*.conf

### BEGIN INIT INFO
# Provides: mongrel_cluster
# Required-Start: $local_fs $network $remote_fs $syslog
# Required-Stop: $local_fs $network $remote_fs $syslog
# Short-Description: Startup script for mongrel clusters
# Description: The mongrel_cluster manages multiple mongrel processes
#              for the use behind a load balancer or a proxy server.
### END INIT INFO

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

# Paths to the scripts and files
mongrel="/usr/bin/mongrel_rails"
prog="mongrel_cluster"
configdir="/etc/mongrel_cluster"
lockfile="/var/lock/subsys/mongrel_cluster"
RETVAL=0

# See how we were called.
case "$1" in
  start)
	if [ $# -gt 1 ]; then
	  shift 1
	  configs="$@"
	else
	  configs="$configdir/*.yml $configdir/*.conf"
	fi

	for config in $configs; do
	  if [ "`basename $config`" = "$config" ]; then
	    config="$configdir/$config"
	  fi

	  if [ -f $config ]; then
	    file=`basename $config`
	    echo -n $"Starting $prog for $file: "
	    $mongrel cluster::start -C $config >> /dev/null 2>&1
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo_success || echo_failure
	    echo
	  fi
	done

	[ $RETVAL -eq 0 ] && touch $lockfile
	;;
  stop)
	if [ $# -gt 1 ]; then
	  shift 1
	  configs="$@"
	else
	  configs="$configdir/*.yml $configdir/*.conf"
	fi

	for config in $configs; do
	  if [ "`basename $config`" = "$config" ]; then
	    config="$configdir/$config"
	  fi

	  if [ -f $config ]; then
	    file=`basename $config`
	    echo -n $"Stopping $prog for $file: "
	    $mongrel cluster::stop -C $config >> /dev/null 2>&1
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo_success || echo_failure
	    echo
	  fi
	done

	[ $RETVAL -eq 0 ] && rm -f $lockfile
	;;
  status)
	if [ $# -gt 1 ]; then
	  shift 1
	  configs="$@"
	else
	  configs="$configdir/*.yml $configdir/*.conf"
	fi

	for config in $configs; do
	  if [ "`basename $config`" = "$config" ]; then
	    config="$configdir/$config"
	  fi

	  if [ -f $config ]; then
	    file=`basename $config`
	    echo $0 status $file
	    $mongrel cluster::status -C $config 2> /dev/null
	  fi
	done
	;;
  restart|reload|force-reload)
	if [ $# -gt 1 ]; then
	  shift 1
	  configs="$@"
	else
	  configs="$configdir/*.yml $configdir/*.conf"
	fi

	for config in $configs; do
	  if [ "`basename $config`" = "$config" ]; then
	    config="$configdir/$config"
	  fi

	  if [ -f $config ]; then
	    file=`basename $config`
	    echo -n $"Restarting $prog for $file: "
	    $mongrel cluster::restart -C $config >> /dev/null 2>&1
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo_success || echo_failure
	    echo
	  fi
	done

	[ $RETVAL -eq 0 ] && touch $lockfile
	;;
  condrestart|try-restart)
	if [ -f $lockfile ]; then
	  shift 1
	  $0 stop $@
	  $0 start $@
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|status|reload|restart|condrestart|force-reload|try-restart} [<configfile.{yml|conf}>]"
	RETVAL=1
esac

exit $RETVAL
