#!/usr/bin/env bash
set -o errexit -o pipefail
DIR=$(dirname "${0}")
MAIN_DIR=$(readlink -f "${DIR}/..")
# GIT_DIR to support workdirs/worktrees
GIT_DIR="$(dirname "$(realpath "${MAIN_DIR}/.git/config")")"
# shellcheck disable=SC2016
IMAGE=$(grep ^image: "${MAIN_DIR}/.gitlab-ci.yml" | \
sed -e 's,^image: ,,g' | sed -e 's,\$CI_REGISTRY,registry.gitlab.com,g')
declare -a docker_opts=(
-i
--rm
--user "$(id -u):$(id -g)"
--mount "type=bind,src=${MAIN_DIR},dst=${MAIN_DIR}"
--mount "type=bind,src=${GIT_DIR},dst=${GIT_DIR}"
--workdir "${MAIN_DIR}"
)
if tty -s; then
docker_opts+=( -t )
fi
exec docker run "${docker_opts[@]}" "${IMAGE}" "${@}"