#!/bin/sh

#
# 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.
#

# Run the qmf interoperability tests.
MY_DIR=`dirname \`which $0\``
QPID_DIR=${MY_DIR}/../../../..
BUILD_DIR=../../..
PYTHON_DIR=${QPID_DIR}/python
BROKER_DIR=${BUILD_DIR}/src
API_DIR=${BUILD_DIR}/bindings/qmf
SPEC_DIR=${QPID_DIR}/specs

trap stop_broker INT TERM QUIT

start_broker() {
    ${BROKER_DIR}/qpidd --daemon --port 0 --no-data-dir --no-module-dir --auth no > _qpidd.port
    BROKER_PORT=`cat _qpidd.port`
}

stop_broker() {
    ${BROKER_DIR}/qpidd -q --port $BROKER_PORT
    echo "Broker stopped"
}

start_ruby_agent() {
    ruby -I${MY_DIR}/../ruby -I${API_DIR}/ruby/.libs ${MY_DIR}/agent_ruby.rb localhost $BROKER_PORT &
    AGENT_PID=$!
}

stop_ruby_agent() {
    kill $AGENT_PID
}

if test -d ${PYTHON_DIR} ;  then
    start_broker
    echo "Running qmf interop tests using broker on port $BROKER_PORT"
    PYTHONPATH=${PYTHON_DIR}:${MY_DIR}
    export PYTHONPATH
    echo "    Ruby Agent vs. Pure-Python Console"
    start_ruby_agent
    echo "    Ruby agent started at pid $AGENT_PID"
    ${PYTHON_DIR}/qpid-python-test -m python_console -b localhost:$BROKER_PORT $@
    RETCODE=$?
    stop_ruby_agent
    stop_broker
    if test x$RETCODE != x0; then 
        echo "FAIL qmf interop tests"; exit 1;
    fi
fi
