#!/bin/bash

# Set up either Cypress or Playwright tests.
# Can copy or link the tests to their destination as well as copy
# the tests back to their original location, which is useful
# for ATK contributors.
#
# Change WEB_ROOT_DIR as needed (usually "html" or "web").
#
# How to use:
# atk_setup <framework> <"back"|"link"|"">
#
# 1st argument:
# framework = "playwright" OR "cypress"
# 2nd argument:
# <blank>   = copy tests
# "link"    = symlink tests instead of copying them
# "back"    = copy tests back to module
#
# For the initial install, from the project root
# (above web or html), use:
# web/modules/contrib/automated_testing_kit/module_support/atk_setup playwright
#
# The tests and support files will be copied to their default locations
# for each test framework.
#
# To install in a custom directory:
# export ATK_HOME=./my/test/dir
# web/modules/contrib/automated_testing_kit/module_support/atk_setup playwright
#
# == For Automated Testing Kit Contributors ==
# You can copy your modified and new tests located in <project root>/tests
# or <project root>/cypress back to the repo. The repo is usually located in
# <project root>/web/modules/contrib/automated_testing_kit).
# Use one of:
# <project root>/modules/contrib/automated_testing_kit/module_support/atk_setup playwright back
# <project root>/modules/contrib/automated_testing_kit/module_support/atk_setup cypress back

MODULE_DIR=$(dirname $(dirname $0))
MODULE_SUPPORT_DIR="$MODULE_DIR/module_support"
TARGET_DIR=${ATK_HOME:-.}

if [ -z "$1" ]; then
  echo "Please provide target framework (playwright OR cypress)."
  exit 1
fi

DO_COPY=1
DO_BACK=0
DO_LINK=0
if [ ! -z "$2" ]; then
  DO_COPY=0
  if [ "$2" == 'back' ]; then
    DO_BACK=1
  elif [ "$2" == 'link' ]; then
    DO_LINK=1
  else
    echo "Second parameter must be 'back'|'link'."
    exit 1
  fi
fi

# Assume this is being run from the project directory and the
# module is in the standard contrib location (web/modules/contrib).

DEVELOPMENT_SOURCE_DIR="$MODULE_DIR/module_support/development"

if [ "$1" == "cypress" ]; then
  echo "== Setting up for Cypress. =="

  TEST_SOURCE_DIR="$MODULE_DIR/cypress/e2e"
  TEST_DESTINATION_DIR="$TARGET_DIR/cypress/e2e"

  DATA_SOURCE_DIR="$MODULE_DIR/data"
  DATA_DESTINATION_DIR="$TARGET_DIR/cypress"

  SUPPORT_SOURCE_DIR="$MODULE_DIR/cypress/support"
  SUPPORT_DESTINATION_DIR="$TARGET_DIR/cypress"
else
  echo "== Setting up for Playwright. =="

  TEST_SOURCE_DIR="$MODULE_DIR/playwright/e2e"
  TEST_DESTINATION_DIR="$TARGET_DIR/tests"

  DATA_SOURCE_DIR="$MODULE_DIR/data"
  DATA_DESTINATION_DIR="${TEST_DESTINATION_DIR}"

  SUPPORT_SOURCE_DIR="$MODULE_DIR/playwright/support"
  SUPPORT_DESTINATION_DIR="${TEST_DESTINATION_DIR}"
fi

echo -e "Test source:             $TEST_SOURCE_DIR"
echo -e "Test destination:        $TEST_DESTINATION_DIR"
echo -e "Data source:             $DATA_SOURCE_DIR"
echo -e "Data destination:        $DATA_DESTINATION_DIR"
echo -e "Support source:          $SUPPORT_SOURCE_DIR"
echo -e "Support destination:     $SUPPORT_DESTINATION_DIR"
echo -e "Development source:      $DEVELOPMENT_SOURCE_DIR"
echo -e "Development destination: <project root>\n"

#
# Copy xx.atk.config.js and xx.config.js to the project root.
#
if [[ $DO_BACK == 0 ]]; then
  if [ "$1" == "cypress" ]; then
    echo "Copying cypress.atk.config.js to $TARGET_DIR/cypress.atk.config.js."
    cp $MODULE_SUPPORT_DIR/cypress.atk.config.js $TARGET_DIR

    echo "Copying cypress.config.js to $TARGET_DIR/cypres.config.js."
    cp $MODULE_SUPPORT_DIR/cypress.config.js $TARGET_DIR
  else
    echo "Copying playwright.atk.config.js to $TARGET_DIR/playwright.atk.config.js."
    cp $MODULE_SUPPORT_DIR/playwright.atk.config.js $TARGET_DIR

    echo "Copying playwright.config.js to $TARGET_DIR/playwright.config.js."
    cp $MODULE_SUPPORT_DIR/playwright.config.js $TARGET_DIR
  fi
fi

#
# Copy xx.package.json to the project root.
#
if [[ $DO_BACK == 0 ]]; then
  if [ "$1" == "cypress" ]; then
    echo "Copying cypress.package.json to $TARGET_DIR/package.json."
    cp $DEVELOPMENT_SOURCE_DIR/cypress.package.json $TARGET_DIR/package.json
  else
    echo "Copying playwright.package.json to $TARGET_DIR/package.json."
    cp $DEVELOPMENT_SOURCE_DIR/playwright.package.json $TARGET_DIR/package.json
  fi
fi

#
# Copy, link or copy back framework tests.
#
if [[ $DO_COPY == 1 ]]; then
  echo "Copying tests to $TEST_DESTINATION_DIR."
  # Ensure the destination directory exists.
  mkdir -p ${TEST_DESTINATION_DIR}
  cp -R $TEST_SOURCE_DIR/* $TEST_DESTINATION_DIR

elif [[ $DO_BACK == 1 ]]; then
  echo "Copying tests back to $TEST_SOURCE_DIR."
  cp -R $TEST_DESTINATION_DIR/atk** $TEST_SOURCE_DIR

elif [[ $DO_LINK == 1 ]]; then
  echo "Linking tests to $TEST_DESTINATION_DIR."
  # Can't link the parent source directory so need to iterate through
  # just test directories to link individual tests.
  TESTS=$(find "${TEST_SOURCE_DIR}" -mindepth 1 -type d)

  for TEST_DIR in ${TESTS}; do
    echo "Test:"$TEST_DIR
    TEST_DIRS+=("$TEST_DIR")
  done

  for TEST_DIR in "${TEST_DIRS[@]}"; do
    echo "Linking $PWD/$TEST_DIR."
    TEST_BASENAME=$(basename "$TEST_DIR")
    SOURCE=$TEST_DIR
    echo "Before link source:"$SOURCE
    echo "Before link dest:"$PWD/$TEST_DESTINATION_DIR
    ln -s "$PWD/$TEST_DIR" "$PWD/$TEST_DESTINATION_DIR"
  done
fi

#
# Copy, link or put back data files.
#
if [[ $DO_COPY == 1 ]]; then
  echo "Copying data files to $DATA_DESTINATION_DIR."
  cp -R $DATA_SOURCE_DIR $DATA_DESTINATION_DIR

elif [[ $DO_BACK == 1 ]]; then
  echo "Copying data files back to $DATA_SOURCE_DIR."
  cp -R $DATA_DESTINATION_DIR/data/* $DATA_SOURCE_DIR

elif [[ $DO_LINK == 1 ]]; then
  echo "Linking data files to $DATA_DESTINATION_DIR."
  ln -s "$PWD/$DATA_SOURCE_DIR" "$PWD/$DATA_DESTINATION_DIR"
fi

#
# Copy, link or copy back testing framework support files.
#
if [[ $DO_COPY == 1 ]]; then
  echo "Copying test framework support files to $SUPPORT_DESTINATION_DIR."
  cp -R $SUPPORT_SOURCE_DIR $SUPPORT_DESTINATION_DIR

elif [[ $DO_BACK == 1 ]]; then
  echo "Copying test framework support files back to $SUPPORT_SOURCE_DIR."
  cp -R $SUPPORT_DESTINATION_DIR/support/* $SUPPORT_SOURCE_DIR

elif [[ $DO_LINK == 1 ]]; then
  echo "Linking test framework support files to $SUPPORT_DESTINATION_DIR."
  ln -s "$PWD/$SUPPORT_SOURCE_DIR" "$PWD/$SUPPORT_DESTINATION_DIR"
fi

#
# Provide guidance.
#
if [[ $DO_COPY == 1 ]]; then
  echo -e "\nAutomated Testing Kit uses the following configuration files,"
  echo -e "which have been copied to the project root:"
  echo -e "- package.json"
  echo -e "- playwright.atk.config.js or cypress.atk.config.js"
  echo -e "- playwright.config.js or cypress.config.js."
  echo -e "\nIf Drupal is running in a container (DDEV, Lando, Docksal), adjust"
  echo -e "the drushCmd value in xx.atk.config.js. For details, see "
  echo -e "the documentation located at:"
  echo -e "https://performantlabs.com/automated-testing-kit/configuration"
  echo -e "\nBefore running tests, set the target URL in playwright.config.js or cypress.config.js.\n"
fi