#!/usr/bin/env bash
set -e
PATTERN="$1"
shift

# CircleCI has a bug in its workspace code where it can't handle filenames with some chars
CLEANED_PATTERN=`echo $PATTERN | tr '^?()$' '_'`

# If no match -> grep returns 1 -> script stops
ENVLIST=$(tox -l | grep "$PATTERN")

if [[ -z "${CIRCLE_NODE_TOTAL}" && -z "${CIRCLE_NODE_INDEX}" ]]; then
    exec echo "$ENVLIST" | tr '\n' ',' | xargs -I ARGS tox -e ARGS -- $@
else
    exec echo "$ENVLIST" | sort | python -c "import os, sys; t, i = int(os.getenv('CIRCLE_NODE_TOTAL')), int(os.getenv('CIRCLE_NODE_INDEX')); inp = sys.stdin.readlines(); base = len(inp) // t; remainder = len(inp) % t; start_index = (i * base) + (i if i < remainder else remainder); end_index = start_index + base + (1 if i < remainder else 0); print(''.join(inp[start_index:end_index]).strip())" | tr '\n' ',' | xargs -I ARGS tox -e ARGS -- $@
fi
