#!/bin/bash

set -e
#set -x

# Install dev packages from Pipfile.lock if necessary.
pipenv sync --dev

BATCH_SIZE=1
BATCH_NUMBER=""
batch_args="$1"

case $batch_args in
    -b|--batch)
    BATCH_NUMBER="$2"
    ;;
    --default)
    BATCH_NUMBER=""
    ;;
esac

if [[ -z "$BATCH_NUMBER" ]]; then
    echo "Batch number not passed in. Please pass a batch number to run tests."
    exit -1
fi

case ${BATCH_NUMBER} in
    ''|*[1-9]*) ;;
    *) echo "Bad Input for Batch number. ${BATCH_NUMBER} is not a valid batch number (Should be a number >= 1)" && exit -1 ;;
esac

scenarios=( $(find roles -type f -name 'molecule.yml' -exec dirname {} ';' | sort) )

offset=$(( ${BATCH_NUMBER} - 1))
test_start_index=$(( ${offset} * $BATCH_SIZE ))
scenario_batch="${scenarios[@]:$test_start_index:$BATCH_SIZE}"

if [[ ${scenario_batch[@]} ]]; then
    echo "Scenarios that will be executed as part of this batch: ${scenario_batch}"
else
    echo "Scenario Batch for ${BATCH_NUMBER} is empty. Exiting (Non zero) now"
    exit -1
fi

for scenario in ${scenario_batch}; do
    scenario_name=$(basename ${scenario})
    role_name=$(dirname $(dirname ${scenario}))
    echo "Running scenario ${scenario_name} in ${role_name}"
    pushd ${role_name}
    pipenv run \
       molecule test -s ${scenario_name}
    popd
done;
