#!/usr/bin/env bash

# Usage: codecept-cached [codecept args...]

# Find the first argument that matches <file>:<method>
for arg in "$@"; do
  if [[ "$arg" =~ ^([^. ]+\.php|[^. ]+):([a-zA-Z0-9_]+)$ ]]; then
    cest_file="${BASH_REMATCH[1]}"
    test_method="${BASH_REMATCH[2]}"
    # Remove .php extension if present for consistency
    cest_file="${cest_file%.php}.php"
    # Remove the file:method argument from the list
    args=()
    for a in "$@"; do
      [[ "$a" == "$arg" ]] && continue
      args+=("$a")
    done
    # Call codecept-cached.php
    exec php "$(dirname "$0")/codecept-cached.php" "$cest_file" "$test_method" "${args[@]}"
    exit
  fi
done

# Otherwise, just run codecept as normal
exec "$(dirname "$0")/shared/tools/codecept" "$@"