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

source ./test_env.sh

# This minimum value corresponds to sasl version 2.1.22
minimum_sasl_version=131350

sasl_version=`$QPID_TEST_EXEC_DIR/sasl_version`

# This test is necessary becasue this sasl version is the first one that permits 
# redirection of the sasl config file path.
if [ "$sasl_version" -lt  "$minimum_sasl_version" ]; then
  echo "sasl_fed: must have sasl version 2.1.22 or greater.  ( Integer value: $minimum_sasl_version )  Version is: $sasl_version"
  exit 0
fi

# In a distribution, the python tools will be absent.
if [ ! -f $QPID_CONFIG_EXEC ] || [ ! -f $QPID_ROUTE_EXEC ] ; then
    echo "python tools absent - skipping sasl_fed."
    exit 0
fi


sasl_config_file=$builddir/sasl_config

my_random_number=$RANDOM
tmp_root=/tmp/sasl_fed_$my_random_number
mkdir -p $tmp_root


#--------------------------------------------------
#echo " Starting broker 1"
#--------------------------------------------------
$QPIDD_EXEC                                  \
  -p 0                                       \
  --data-dir $tmp_root/data_1                \
  --auth=yes                                 \
  --mgmt-enable=yes                          \
  --log-enable info+                         \
  --log-source yes                           \
  --log-to-file $tmp_root/qpidd_1.log        \
  --sasl-config=$sasl_config_file            \
  -d > $tmp_root/broker_1_port

broker_1_port=`cat $tmp_root/broker_1_port`


#--------------------------------------------------
#echo " Starting broker 2"
#--------------------------------------------------
$QPIDD_EXEC                                  \
  -p 0                                       \
  --data-dir $tmp_root/data_2                \
  --auth=yes                                 \
  --mgmt-enable=yes                          \
  --log-enable info+                         \
  --log-source yes                           \
  --log-to-file $tmp_root/qpidd_2.log        \
  --sasl-config=$sasl_config_file            \
  -d > $tmp_root/broker_2_port

broker_2_port=`cat $tmp_root/broker_2_port`

sleep 2

# I am not randomizing these names, because the test creates its own brokers.
QUEUE_NAME=sasl_fed_queue
ROUTING_KEY=sasl_fed_queue
EXCHANGE_NAME=sasl_fedex

#--------------------------------------------------
#echo "  add exchanges"
#--------------------------------------------------
$QPID_CONFIG_EXEC -b localhost:$broker_1_port add exchange direct $EXCHANGE_NAME
$QPID_CONFIG_EXEC -b localhost:$broker_2_port add exchange direct $EXCHANGE_NAME


#--------------------------------------------------
#echo "  add queues"
#--------------------------------------------------
$QPID_CONFIG_EXEC -b localhost:$broker_1_port add queue $QUEUE_NAME
$QPID_CONFIG_EXEC -b localhost:$broker_2_port add queue $QUEUE_NAME

sleep 5

#--------------------------------------------------
#echo " create bindings"
#--------------------------------------------------
$QPID_CONFIG_EXEC -b localhost:$broker_1_port bind $EXCHANGE_NAME $QUEUE_NAME $ROUTING_KEY
$QPID_CONFIG_EXEC -b localhost:$broker_2_port bind $EXCHANGE_NAME $QUEUE_NAME $ROUTING_KEY

sleep 5


#--------------------------------------------------
#echo "  qpid-route route add"
#--------------------------------------------------
$QPID_ROUTE_EXEC route add zag/zag@localhost:$broker_2_port zag/zag@localhost:$broker_1_port $EXCHANGE_NAME $ROUTING_KEY "" "" DIGEST-MD5

sleep 5


n_messages=100
#--------------------------------------------------
#echo "  Sending 100 messages to $broker_1_port "
#--------------------------------------------------
$builddir/datagen --count $n_messages | $SENDER_EXEC --mechanism DIGEST-MD5 --username zag --password zag --exchange $EXCHANGE_NAME --routing-key $ROUTING_KEY --port $broker_1_port

sleep 5

#--------------------------------------------------
#echo "  Examine Broker $broker_1_port"
#--------------------------------------------------
broker_1_message_count=`$PYTHON_COMMANDS/qpid-stat -q -b localhost:$broker_1_port | grep sasl_fed_queue | awk '{print $2}'`
#echo " "

#--------------------------------------------------
#echo "  Examine Broker $broker_2_port"
#--------------------------------------------------
broker_2_message_count=`$PYTHON_COMMANDS/qpid-stat -q -b localhost:$broker_2_port | grep sasl_fed_queue | awk '{print $2}'`
#echo " "

#--------------------------------------------------
#echo "  Asking brokers to quit."
#--------------------------------------------------
$QPIDD_EXEC --port $broker_1_port --quit
$QPIDD_EXEC --port $broker_2_port --quit


#--------------------------------------------------
#echo   "Removing temporary directory $tmp_root"
#--------------------------------------------------
rm -rf $tmp_root

if [ "$broker_2_message_count" -eq  "$n_messages" ]; then
  # echo "good: |$broker_2_message_count| == |$n_messages|"
  exit 0
else
  # echo "not ideal: |$broker_1_message_count| != |$n_messages|"
  exit 1
fi






