#!/bin/bash

if ! [ -f /.dockerenv ] && ! grep -q 'devices:/docker' /proc/1/cgroup ; then
  echo "Not running inside docker, exiting to avoid data damage." >&2
  exit 1
fi

set -e
set -u

PASSWORD="selenium"

if [ -z "${1:-}" ] ; then
  echo "Usage: $0 <testsystem> [<output_directory>] [junit] [test_plan]" >&2
  echo
  echo "Usage examples:

  $0 203.0.113.42
  $0 203.0.113.42 /code/ tap"
  exit 1
fi

RC=0
SERVER="${1}"
OUTPUT_DIRECTORY="${2:-/code/}"
OUTPUT_TYPE=${3:-}
BROWSER=${4:-"firefox"}
shift; shift; shift; # remove first four argv to use ${@} below.

# vnc
echo "Setting VNC password"
printf '%s\n%s\n\n' "${PASSWORD}" "${PASSWORD}" | vncpasswd >/dev/null
PASSWORD_ENCODED=$(hexdump -v -e '"\\""x" 1/1 "%02X"' < "${HOME}/.vnc/passwd")
xvnc_process=$(pgrep -f 'Xtigervnc :99' || true)
export DISPLAY=":99"
if [ -n "${xvnc_process:-}" ] ; then
  echo "Warning: existing VNC server found, not restarting Xtigervnc process (PID: $xvnc_process)."
else
  echo "Starting VNCserver on display :99"
  vncserver -localhost no -geometry 1280x1024 :99
fi

if [ "${BROWSER:-}" = "firefox" ] ; then
  echo "################################################################################"
  echo "Firefox version:"
  firefox --version
elif [ "${BROWSER:-}" = "chrome" ] ; then
  echo "################################################################################"
  echo "Chrome version:"
  google-chrome --version
elif [ "${BROWSER:-}" = "all" ] ; then
  echo "################################################################################"
  echo "Firefox version:"
  firefox --version
  echo "Chrome version:"
  google-chrome --version
else
  echo "################################################################################"
  echo "Invalid browser, exiting..."
  echo "################################################################################"
  exit "${RC}"
fi

export BROWSER

# ensure we don't leave any process behind causing problems with re-execution
pkill -f 'geckodriver' || true

echo "################################################################################"
echo "Finished main setup, now running tests ..."
echo "Selenium server log file available at /home/selenium/selenium.log"
echo "Watch at test runs by connecting via VNC (password: '${PASSWORD}'):"
echo "echo -e '$PASSWORD_ENCODED' >/tmp/vncpasswd ; vncviewer geometry=1280x1024x16 passwd=/tmp/vncpasswd localhost:5999"

export CATALYST_SERVER="https://${SERVER}"
cd t/selenium

if [ "${OUTPUT_TYPE:-}" = "junit" ] ; then
  #Change thread count here and in nose2cfg/jenkinstest.cfg
  export THREADS=2
  export JENKINS=1
  python3 testrun.py -v -c nose2cfg/jenkinstest.cfg | \
    tee -a "${OUTPUT_DIRECTORY}/selenium.xml"
  RC="${PIPESTATUS[0]}"
else
  #Change thread count here and in nose2cfg/localtest.cfg
  export THREADS=1
  python3 testrun.py -v -c nose2cfg/localtest.cfg|| RC=$?
fi

echo "Finished test execution, test execution returned with exit code ${RC}."
if [ -f "${OUTPUT_DIRECTORY}/selenium.xml" ] ; then
  echo "Test results available at '${OUTPUT_DIRECTORY}/selenium.xml'"
fi

echo "################################################################################"

exit "${RC}"
