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

# This starts the controller for coordinating perf tests/

. check-qpid-java-env

PROGRAM_NAME="qpid-jms-receive"
URL="amqp://guest:guest@clientid/testpath?brokerlist='tcp://localhost:5672'"
ADDRESS="queue;{create:always}"
TIMEOUT="0"
FOREVER="false"
MESSAGES="1"
IGNORE_DUPLICATES="false"
CHECK_REDELIVERED="false"
CAPACITY="1000"
ACK_FREQUENCY="100"
TX="0"
ROLLBACL_FREQUENCY="0"
PRINT_CONTENT="false"
PRINT_HEADERS="false"
REPORT_TOTAL="false"
REPORT_EVERY="0"
REPORT_HEADER="true"
READY_ADDRES="''"
EXTRA_JVM_ARGS=""
VERBOSE="0"

TEST_ID=`echo ${HOSTNAME} |  awk -F . '{print $1}'`

TEMP=$(getopt -n $PROGRAM_NAME -o b:a:f:m:vh\
 --long broker:,address:,timeout:,forever\
,messages:,ignore-duplicates,check-redelivered\
,capacity:,ack-frequency:,tx:,rollback-frequency:\
,print-content:,print-headers:,report-total\
,report-every:,report-header:,ready-address:\
,jvm-args:,verbose,help -- "$@")                                                            

# padding the option string with 4 spaces
# padding the desc string with 30 spaces
usage()
{
  printf "\n%s\n" "Usage: $PROGRAM_NAME [option].."

  printf "\n%20s\n%57s\n"  "-b, --broker URL" "url of broker to connect to"

  printf "\n%24s\n%53s\n"  "-a,--address ADDRESS" "address to receive from"

  printf "\n%25s\n%71s\n"  "--timeout TIMEOUT (0)" "timeout in seconds to wait before exiting"

  printf "\n%17s\n%61s\n"  "-f, --forever" "ignore timeout and wait forever"

  printf "\n%24s\n%89s\n"  "-m, --messages N (0)" "Number of messages to receive; 0 means receive indefinitely"

  printf "\n%23s\n%84s\n"  "--ignore-duplicates" "Detect and ignore duplicates (by checking 'sn' header)"

  printf "\n%23s\n%82s\n%92s\n"  "--check-redelivered" "Fails with exception if a duplicate is not marked as" " redelivered (only relevant when ignore-duplicates is selected)"

  printf "\n%23s\n%71s\n"  "--capacity N (1000)" "Pre-fetch window (0 implies no pre-fetch)"

  printf "\n%27s\n%94s\n"  "--ack-frequency N (100)" "Ack frequency (0 implies none of the messages will get accepted)"

  printf "\n%14s\n%94s\n"  "--tx N (0)" "batch size for transactions (0 implies transaction are not used)"

  printf "\n%30s\n%94s\n"  "--rollback-frequency N (0)" "rollback frequency (0 implies no transaction will be rolledback)"

  printf "\n%30s\n%55s\n"  "--print-content yes|no (0)" "print out message content"

  printf "\n%30s\n%55s\n"  "--print-headers yes|no (0)" "print out message headers"

  printf "\n%18s\n%76s\n"  "--report-total" "Report total throughput and latency statistics"

  printf "\n%24s\n%87s\n"  "--report-every N (0)" "Report throughput and latency statistics every N messages"

  printf "\n%30s\n%47s\n"  "--report-header yes|no (1)" "Headers on report"

  printf "\n%27s\n%82s\n"  "--ready-address ADDRESS" "send a message to this address when ready to receive"

  printf "\n%14s\n%69s\n"  "--jvm-args" "Extra jvm arguments you want to specify"

  printf "\n%17s\n%69s\n\n"  "-v, --verbose" "Print debug information for this script"
}

eval set -- "$TEMP"
while true; do     
        case $1 in
                -b|--broker)
                        URL="$2"; shift; shift; continue
                ;;
                -a|--address)
                        ADDRESS="$2"; shift; shift; continue
                ;;
                --timeout)
                        TIMEOUT="$2"; shift; shift; continue
                ;;
                -f|--forever)
                        FOREVER="$2"; shift; shift; continue
                ;;
                -m|--messages)
                        MESSAGES="$2"; shift; shift; continue
                ;;
                --ignore-duplicates)
                        IGNORE_DUPLICATES="true"; shift; continue
                ;;
                --check-redelivered)
                        CHECK_REDELIVERED="true"; shift; continue
                ;;
                --capacity)
                        CAPACITY="$2"; shift; shift; continue
                ;;
                --ack-frequency)
                        ACK_FREQUENCY="$2"; shift; shift; continue
                ;;
                --tx)
                        TX="$2"; shift; shift; continue
                ;;
                --rollback-frequency)
                        ROLLBACK_FREQUENCY="$2"; shift; shift; continue
                ;;
                --print-content)
                        if [ "$2" == "yes" ]; then PRINT_CONTENT="true"; else PRINT_CONTENT="false"; fi; shift; shift; continue
                ;;
                --print-headers)
                        if [ "$2" == "yes" ]; then PRINT_HEADERS="true"; else PRINT_HEADERS="false"; fi; shift; shift; continue
                ;;
                --report-total)
                        REPORT_TOTAL="true"; shift; continue
                ;;
                --report-every)
                        REPORT_EVERY="$2"; shift; shift; continue
                ;;
                --report-header)
                        if [ "$2" == "yes" ]; then REPORT_HEADER="true"; else REPORT_HEADER="false"; fi; shift; shift; continue
                ;;
                --ready-address)
                        READY_ADDRESS="$2"; shift; shift; continue
                ;;
                -a|--jvm-args)
                        EXTRA_JVM_ARGS="$2"; shift; shift; continue
                ;;
                -h|--help)
                        usage
                        exit 0
                ;;   
                -v|--verbose)
                        VERBOSE="1"; shift; continue
                ;;                                                              
                --)                                                                 
                        # no more arguments to parse                                
                        break                                                       
                ;;                                                                  
                *)       
                        # no more arguments to parse                                                                                            
                        break                                                      
                ;;                                                                  
        esac                                                                        
done     

RECEIVER_ARGS="-server -Durl=$URL \
-Daddress=$ADDRESS \
-Dtimeout=$TIMEOUT \
-Dmsg-count=$MESSAGES \
-Dack-frequency=$ACK_FREQUENCY \
-Dtx=$TX \
-Drollback-frequnecy=$ROLLBACL_FREQUENCY \
-Dprint-content=$PRINT_CONTENT \
-Dprint-headers=$PRINT_HEADERS \
-Dreport-total=$REPORT_TOTAL \
-Dreport-every=$REPORT_EVERY \
-Dreport-header=$REPORT_HEADER \
-Dmax_prefetch=$CAPACITY "

if [ "x$READY_ADDRESS" != "x" ]; then RECEIVER_ARGS="$RECEIVER_ARGS -Dready-address=$READY_ADDRESS"; fi
if [ "$VERBOSE" == "1" ]; then echo $RECEIVER_ARGS; fi
echo $RECEIVER_ARGS
$JAVA -cp $CLASSPATH $LOG_CONFIG $JAVA_MEM $RECEIVER_ARGS org.apache.qpid.tools.QpidReceive
