##(c) 2016 Real-Time Technology Solutions, Inc. All Rights Reserved.
##
## Sample QuerySurge(TM) Run Multiple Test Suites Script in order
## version 0.1

#!/bin/bash

##  ' *******************************************************************************
##  ' ********************** Setup Connection Details *******************************
##  ' *****  Edit the following configuration variables to work with your       *****
##  ' *****  QuerySurge environment and preferences.                            *****
##  ' *******************************************************************************

##  ' ********************* START OF CONFIGURATION VARIABLES ************************

myHostname='192.168.0.92'
myPort=8081
myUsername='admin'
#add corresponding username and password to the qsapi.properties file to authenticate
ordered="true"

cli="/opt/QuerySurge/cli/qscli.sh"
cliOptions="--hostname $myHostname --port $myPort --username $myUsername"

suiteId="45"
inputFile="/opt/QuerySurge/cli/samples/input_data.csv"

snippet1Name="startDate"
snippet2Name="endDate"

##  ' ********************* END OF CONFIGURATION VARIABLES ************************

while IFS="," read f1 f2
do
        echo "Modifying Start Date Snippet"
        output=`$cli modifyReusableQueryTextByName $cliOptions --snippetName $snippet1Name --snippetText $f1`
        echo "Modifying End Date Snippet"
        output=`$cli modifyReusableQueryTextByName $cliOptions --snippetName $snippet2Name --snippetText $f2`
        echo "Running Suite $suiteId..."
        scenarioId=`$cli runTestSuiteById $cliOptions $suiteId`
        echo "The scenario $scenarioId has started"
        if $ordered = "true"
        then
                status=$($cli getScenarioOutcome $cliOptions $scenarioId)
                status=${status// /}
                while [[ "$status" == "NotRun" ]] || [[ "$status" == "Running" ]]
                do
                        sleep 5
                        echo "Waiting for scenario $scenarioId to finish"
                        status=`$cli getScenarioOutcome $cliOptions $scenarioId`
                        status=${status// /}
                        echo "Scenario $suiteId Status: $status"
                done
        fi
done < $inputFile

echo "Done!"
