#!/bin/sh

# This script is used only in CI
set -eu

# CI base image has Mercurial, installed system-wide with pip using
# a custom-built Python 3.
# We need the specific exact Python interpreter, because the `python3`
# provided by the base distribution is usually a different one.
HG=/usr/local/bin/hg
INTERPRETER=$($HG debuginstall -T "{pythonexe}")
HEPTAPOD_PKG_PATH=$($INTERPRETER -c 'import heptapod,os; print(os.path.dirname(heptapod.__file__))')
export HGRCPATH=${CI_PROJECT_DIR}/config/tests.hgrc:${HEPTAPOD_PKG_PATH}/required.hgrc
export GITALY_TESTING_ENABLE_ALL_FEATURE_FLAGS=true

echo "Using ${HEPTAPOD_PKG_PATH}/required.hgrc"
echo "Content:"
cat ${HEPTAPOD_PKG_PATH}/required.hgrc

echo "Calling hpd-versions"
$HG hpd-versions
echo

REPOSITORIES_ROOT=$PWD/tmp/tests/repositories

echo "Starting HGitaly server"
HGITALY_SOCKET=tmp/tests/hgitaly/hgitaly.socket
set -x
$HG --config extensions.hgitaly= \
    hgitaly-serve \
    --repositories-root ${REPOSITORIES_ROOT} \
    --listen unix:${HGITALY_SOCKET} &


echo "Starting RHGitaly server"
RHGITALY_SOCKET=tmp/tests/hgitaly/rhgitaly.socket
rm -f $RHGITALY_SOCKET
RHGITALY_REPOSITORIES_ROOT=${REPOSITORIES_ROOT} \
  RHGITALY_LISTEN_URL=unix:${RHGITALY_SOCKET} \
  RHGITALY_SIDECAR_ADDRESS=unix:${HGITALY_SOCKET} \
  HEPTAPOD_SKIP_ALL_GITLAB_HOOKS=yes \
  RHGITALY_CONFIG_DIRECTORY=$PWD/tmp/tests/hgitaly \
  tmp/tests/rhgitaly > log/rhgitaly-test.log 2>&1 &
