#!/usr/bin/env sh
#
# NAME
#   bootstrap — bootstrap the project
#
# vim: set ts=4 sw=4 sts=4 noet:

# NOTE THAT THIS FILE IS INDENTED WITH 4-SPACE HARD TABS
# as is required for <<-EOF heredocs to work.  Please keep
# it this way!

# -- Start bootstrap setup --
set -e
if test -n "$CI_DEBUG_TRACE" -o -n "$SHELLDEBUG"
then
	set -x
fi

if test "${CI}" = 'true'
then
	echo "::group::Build logs"
fi

unset PYTHONDEBUG
export COMPOSE_DISABLE_ENV_FILE=1
export PIP_DISABLE_PIP_VERSION_CHECK=1
export PYTHON_ROOT_USER_ACTION=ignore
export PYTHONWARNINGS=ignore

TEST_HOST=${TEST_HOST:-127.0.0.1}

docker_exec() {
	if test "${CI}" = 'true'
	then
		docker exec -t postgres "$@"
	else
		docker compose exec postgres "$@"
	fi
}

get_exposed_port() {
	if test -f compose.yaml
	then
		if port=$(docker compose port "$1" --protocol "${3:-tcp}" "$2")
		then
			echo "${port#*:}"
		else
			echo "Failed to find port for $1:$2" >&2
			return 1
		fi
	else
		echo "Failed to find port for $1:$2 - compose.yaml not found" >&2
		return 1
	fi
}

if test "$CI" != 'true'
then
	if test -e ./.venv/bin/activate
	then
		. ./.venv/bin/activate
	else
		python3 -m venv --upgrade-deps .venv
		. ./.venv/bin/activate
	fi
fi

mkdir -p build

pip install --upgrade pip

printf 'Installing package...'
pip install -e '.[dev]'
echo ' done.'

if test -d .git && test -f .pre-commit-config.yaml
then
	printf 'Installing pre-commit hook...'
	pre-commit install --install-hooks
	echo ' done.'
fi

if test "${CI}" != 'true'
then
	printf 'Cleaning environment...'
	docker compose down --remove-orphans --volumes
	echo ' done.'

	printf 'Starting environment...'
	docker compose up --wait --wait-timeout 120
	echo ' done.'
fi

if test "${CI}" = 'true'
then
	PGPORT=5432
else
	PGPORT="$(get_exposed_port postgres 5432)"
fi

printf 'Generating .env...'
if test "${CI}" = 'true'
then
	# In CI, use localhost since port is exposed from service container
	cat > .env <<EOF
export FILE_CACHE_ENABLED=no
export TEST_HOST=localhost
export PGHOST=localhost
export PGPORT=5432
export PGDATABASE=postgres
export PGUSER=postgres
export PGPASSWORD=postgres
export POSTGRES_URI=postgresql://postgres:postgres@localhost:5432/postgres
EOF
else
	cat > .env <<EOF
export FILE_CACHE_ENABLED=no
export TEST_HOST=${TEST_HOST}
export PGHOST=${TEST_HOST}
export PGPORT=${PGPORT}
export PGDATABASE=postgres
export PGUSER=postgres
export POSTGRES_URI=postgresql://postgres@${TEST_HOST}:${PGPORT}/postgres
EOF
fi
echo ' done.'

echo "Running pgbench ... "
docker_exec /usr/bin/pgbench -i -U postgres postgres

printf "Loading fixture schema ... "
docker_exec /usr/bin/psql -U postgres -d postgres -q -o /dev/null -f /fixtures/schema.sql
echo ' done.'

printf "Generating fixture data ... "
if test "${CI}" = 'true'
then
	# In CI, connect to localhost since port is exposed from service container
	PGPASSWORD=postgres python bin/generate-fixture-data.py -U postgres -h localhost -p 5432 -d postgres
else
	python bin/generate-fixture-data.py -U postgres -h "${TEST_HOST}" -p "${PGPORT}" -d postgres
fi
echo ' done.'

printf "Creating test backups ... "
docker_exec /usr/bin/pg_dump -Fc -U postgres -f /data/dump.not-compressed -d postgres --compress=0
docker_exec /usr/bin/pg_dump -Fc -U postgres -f /data/dump.compressed -d postgres --compress=9
docker_exec /usr/bin/pg_dump -Fc -U postgres -f /data/dump.no-data -d postgres --compress=0 -s
docker_exec /usr/bin/pg_dump -Fc -U postgres -f /data/dump.data-only -d postgres --compress=0 -a
docker_exec /usr/bin/pg_dump -Fc -U postgres -f /data/dump.inserts -d postgres --compress=0 --inserts
echo ' done.'

printf "Fixing permissions ... "
docker_exec chmod -R a+r /data
echo ' done.'

if test "${CI}" = 'true'
then
	echo '::endgroup::'
fi
