#!/bin/sh -e
# Installed by the version-matched predependency, before the main package's %pre.
# A fresh installation must not need the git account, database or application.
[ "${1:-1}" -ge 2 ] || exit 0
version=@VERSION@
pg_min_version=@PG_MIN_VERSION@

sysconfig=/etc/sysconfig/gitlab
dbenv_helper=/usr/lib/gitlab/gitlab-db-env
[ ! -s "$sysconfig" ] || . "$sysconfig"
if [ "$GITLAB_SKIP_UPGRADE_CHECK" = "yes" ] ; then
	echo "gitlab-ce: GITLAB_SKIP_UPGRADE_CHECK=yes, the upgrade check is skipped" >&2
	exit 0
fi
# the version the database is migrated to (gitlab-migrate), else the installed one
from="$(cat /var/lib/gitlab/VERSION 2>/dev/null || cat /usr/share/gitlab/VERSION 2>/dev/null || :)"
[ -n "$from" ] || exit 0
gitlab_minor() { echo "$1" | awk -F. '{ print $1 * 1000 + $2 }' ; }
if [ "$from" != "$version" ] && [ "$(printf '%s\n' "$from" "$version" | sort -V | tail -n1)" != "$version" ] ; then
	echo "gitlab-ce: GitLab $from can not be downgraded to $version: restore the backup of $from instead" >&2
	exit 1
fi
f=$(gitlab_minor "$from")
t=$(gitlab_minor "$version")
stops="$(ruby -ryaml -e 'puts YAML.safe_load_file(ARGV[0]).map { |e| "#{e.fetch("major")}.#{e.fetch("minor")}" }' /usr/share/gitlab-ce-upgrade-check/upgrade_path.yml)"
for stop in $stops ; do
	s=$(gitlab_minor "$stop")
	if [ $s -gt $f ] && [ $s -lt $t ] ; then
		echo "gitlab-ce: GitLab $from can not be upgraded to $version directly:" >&2
		echo "upgrade to the latest $stop.x first (required upgrade stop, https://docs.gitlab.com/update/upgrade_paths/)" >&2
		exit 1
	fi
done
# the database checks use the version-matched, preinstalled gitlab-db-env
if [ "$from" != "$version" ] && [ -s /etc/gitlab/database.yml ] && [ -x "$dbenv_helper" ] ; then
	if ! dbenv="$("$dbenv_helper" 2>&1)" ; then
		echo "gitlab-ce: can not read /etc/gitlab/database.yml: $dbenv" >&2
		exit 1
	fi
	eval "$dbenv"
	gitlab_psql() { /sbin/runuser -u git -- psql -X -At -c "$1" ; }
	if ! pgver="$(gitlab_psql 'SHOW server_version_num')" ; then
		echo "gitlab-ce: can not connect to the database $PGDATABASE to check the upgrade or psql does not work as user git," >&2
		echo "start PostgreSQL or set GITLAB_SKIP_UPGRADE_CHECK=yes in /etc/sysconfig/gitlab" >&2
		exit 1
	fi
	if [ "$pgver" -lt ${pg_min_version}0000 ] ; then
		echo "gitlab-ce: GitLab $version needs PostgreSQL $pg_min_version or newer, the server is $pgver:" >&2
		echo "upgrade the PostgreSQL server (pg_upgrade) first" >&2
		exit 1
	fi
	has_bbm="$(gitlab_psql "SELECT to_regclass('batched_background_migrations') IS NOT NULL")"
	if [ "$has_bbm" = "t" ] ; then
		pending="$(gitlab_psql "SELECT job_class_name || ' ' || table_name || '.' || column_name FROM batched_background_migrations WHERE status NOT IN (3, 6)")"
		if [ -n "$pending" ] ; then
			echo "gitlab-ce: the batched background migrations of GitLab $from are not finished:" >&2
			echo "$pending" >&2
			echo "wait for them (Admin area > Monitoring > Background migrations) and upgrade again" >&2
			exit 1
		fi
	fi
fi
