#!/bin/sh -efu
#
# Copyright (C) 2009  Alexey I. Froloff <raorn@altlinux.org>
#
# mongrel_cluster_ctl - controls cluster of mongrel servers
#
# 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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. shell-args
. shell-config
. shell-getopt
. shell-var

PROG="${0##*/}"
PROG_VERSION=1.0

show_usage() {
	[ -z "$*" ] || message "$*"
	echo "Usage: $PROG COMMAND [OPTIONS]" >&2
	echo "Valid commands are \`start', \`stop', \`restart' and \`status'." >&2
	exit 1
}

show_help() {
	cat <<EOF
$PROG - Mongrel Cluster Controller

Usage: $PROG COMMAND [OPTIONS]

Options:
  -c,--conf_path=PATH  path to mongrel_cluster configuration files;
  --clean              remove pid files if needed beforehand;
  --user=USER          default user to run process as, if not specified
                       in configuration files;
  --group=GROUP        default group to run process as, if not specified
                       in configuration files;
  -v,--verbose         print all called commands and output;
  -V,--version         print program version and exit;
  -h,--help            show this text and exit.

Valid commands are \`start', \`stop', \`restart' and \`status'.

Report bugs to http://bugzilla.altlinux.org/

EOF
	exit
}

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION
Written by Alexey I. Froloff <raorn@altlinux.org>

Copyright (C) 2009  Alexey I. Froloff <raorn@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

[ "$#" -gt 0 ] ||
	show_usage 'not enough arguments'

cmd="$1" && shift

case "$cmd" in
	start|stop|restart|reload|status) ;;
	*) show_usage "invalid command '$cmd'";;
esac

log_file=
pid_file=
user=
group=

TEMP=`getopt -n $PROG -o +c:,v,V,h -l conf_path:,clean,user:,group:,log_file:,pid_file:,verbose,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

conf_path=/etc/mongrel_cluster
clean=
verbose=
while :; do
	case "$1" in
		-c|--conf_path) conf_path="$(opt_check_dir "$1" "$2")"; shift;;
		--clean) clean=--clean;;
		--user) shift; user="$1";;
		--group) shift; group="$1";;
		--log_file) shift; log_file="$1";;
		--pid_file) shift; pid_file="$1";;
		-v|--verbose) verbose=-v;;
		-V|--version) print_version;;
		-h|--help) show_help;;
		--) shift; break;;
		*) fatal "unrecognized option '$1'";;
	esac
	shift
done

[ -d "$conf_path" ] ||
	fatal "configuration directory \`$conf_path' not available, exiting"

# Can't use pipes here, since init.d/functions wants terminal
for config in .. $(find "$conf_path" -maxdepth 1 -mindepth 1 -name '*.yml' -o -name '*.conf'); do
	[ "$config" != .. ] ||
		continue

	[ -r "$config" ] || {
		verbose "unable to read \`$config'"
		continue
	}

	mongrel_cluster "$cmd" \
		-C "$config" \
		${user:+--user "$user"} \
		${group:+--group "$group"} \
		${log_file:+--log_file "${log_file}"} \
		${pid_file:+--pid_file "${pid_file}"} \
		$verbose $clean ||:
done
