#!/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 RHGitaly server"
RHGITALY_SOCKET=tmp/tests/hgitaly/rhgitaly.socket
rm -f $RHGITALY_SOCKET

CONFIG=tmp/tests/hgitaly/rhgitaly.toml

cat > ${CONFIG} <<EOF
listen_url = "unix:${RHGITALY_SOCKET}"
[mercurial]
  executable = "${HG}"
  hgrc = "${HGRCPATH}"
  repositories_root = "${REPOSITORIES_ROOT}"
[sidecar]
  client_id_path = "tmp/tests/hgitaly/rhgitaly.client-id"
  managed = true
EOF

export HEPTAPOD_SKIP_ALL_GITLAB_HOOKS=yes

tmp/tests/rhgitaly --config ${CONFIG} > log/rhgitaly-test.log 2>&1 &
