#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

usage() {
    echo "Usage: `basename $0` [options] start|stop|restart|check [hosts]
Start/stop/restart a cluster on specified hosts or on \$HOSTS via ssh.

Options:
 -l USER     Run qpidd and copy files as USER.
 -e SCRIPT   Source SCRIPT for environment settings. Copies SCRIPT to each host.
             Default is $DEFAULT_ENV.
 -c CONFIG   Use CONFIG as qpidd config file. Copies CONFIG to each host.
             Default is $DEFAULT_CONF
 -d          Delete data-dir and log file before starting broker.	     
"
    exit 1
}

DEFAULT_CONF=~/qpid-test-qpidd.conf
DEFAULT_ENV=~/qpid-test-env.sh

test -f $DEFAULT_CONF && CONF_FILE=$DEFAULT_CONF
test -f $DEFAULT_ENV && ENV_FILE=$DEFAULT_ENV

while getopts "l:e:c:d" opt; do
    case $opt in
	l) SSHOPTS="-l$OPTARG $SSHOPTS" ; RSYNC_USER="$OPTARG@" ;;
	e) ENV_FILE=$OPTARG ;;
	c) CONF_FILE=$OPTARG ;;
	d) DO_DELETE=1 ;;
	*) usage;;
    esac
done
shift `expr $OPTIND - 1`
test "$*" || usage
CMD=$1; shift
HOSTS=${*:-$HOSTS}

conf_value() { test -f "$CONF_FILE" && awk -F= "/^$1=/ {print \$2}" $CONF_FILE; }

if test -n "$CONF_FILE"; then
    test -f "$CONF_FILE" || { echo Config file not found: $CONF_FILE; exit 1; }
    RSYNCFILES="$RSYNCFILES $CONF_FILE"
    QPIDD_ARGS="$QPIDD_ARGS --config $CONF_FILE"
    CONF_PORT=`conf_value port`
    CONF_DATA_DIR=`conf_value data-dir`
    CONF_LOG_FILE=`conf_value log-to-file`
fi

if test -n "$ENV_FILE"; then
    test -f "$ENV_FILE" || { echo Environment file not found: $ENV_FILE; exit 1; }
    RSYNCFILES="$RSYNCFILES $ENV_FILE"
    SOURCE_ENV="source $ENV_FILE ; "
fi

test -n "$RSYNCFILES" && rsynchosts $RSYNCFILES	# Copy conf/env files to all hosts

do_start() {
    for h in $HOSTS; do
	COMMAND="qpidd -d $QPIDD_ARGS"
	id -nG | grep '\<ais\>' >/dev/null && COMMAND="sg ais -c '$COMMAND'"
	if test "$DO_DELETE"; then COMMAND="rm -rf $CONF_DATA_DIR $CONF_LOG_FILE; $COMMAND"; fi
	ssh $h "$SOURCE_ENV $COMMAND" || { echo "Failed to start on $h"; exit 1; }
    done
}

do_stop() {
    for h in $HOSTS; do
	ssh $h "$SOURCE_ENV qpidd -q --no-module-dir --no-data-dir $QPIDD_ARGS"
    done
}

do_status() {
    for h in $HOSTS; do
	if ssh $h "$SOURCE_ENV qpidd -c --no-module-dir --no-data-dir $QPIDD_ARGS > /dev/null"; then
	    echo "$h ok"
	else
	    echo "$h not running"
	    STATUS=1
	fi
    done
}

case $CMD in
    start) do_start ;;
    stop) do_stop ;;
    restart) do_stop ; do_start ;;
    status) do_status ;;
    *) usage;;
esac

exit $STATUS
