#!/usr/bin/env bash
set -euo pipefail

# Activate virtualenv
if [ -f venv/bin/activate ]; then
  # shellcheck disable=SC1091
  . venv/bin/activate
fi

# Ensure results directory exists
mkdir -p results

# If running in Kubernetes or a container where a PVC is mounted at /workspace/results/chrome-profile,
# use that as the Chrome user data dir so Chrome uses a persistent profile.
if [ -d /workspace/results/chrome-profile ]; then
  export CHROME_USER_DATA_DIR=/workspace/results/chrome-profile
elif [ -d ${PWD}/results/chrome-profile ]; then
  export CHROME_USER_DATA_DIR=${PWD}/results/chrome-profile
fi

# Create profile dir if not present
mkdir -p "${CHROME_USER_DATA_DIR-./results/chrome-profile}"

# Run robot framework on tests folder; adjust args as needed
robot -d results tests || RC=$?

# Exit with robot return code if set
if [ -n "${RC-}" ]; then
  exit $RC
fi
