#! /usr/bin/ksh
#
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2022 Marcel Telka
#


THIS="python-integrate-project"
CONF="$THIS.conf"
SNIPPET="$THIS.snippet"
APIURL="https://pypi.org/pypi"
CURL="/usr/bin/curl -s"


function usage
{
	[[ -n "$1" ]] && printf "ERROR: %s\n\n" "$1" >&2
	printf "Usage: %s [-d DIR] [-f] [-l VERSION] [-o OBSOLETE].. [-u] PROJECT\n" "$THIS" >&2
	[[ -n "$1" ]] && exit 1
	exit 0
}


OPT_VERSION=
OBSOLETE=
UPGRADE_ONLY=0
DIRECTORY=
FORCE=0
while getopts ":hd:fl:o:u" OPT ; do
	case "$OPT" in
	"?"|"h")	usage ;;
	"d")		DIRECTORY="$OPTARG" ;;
	"f")		FORCE=1 ;;
	"l")		OPT_VERSION="$OPTARG" ;;
	"o")		OBSOLETE="$OBSOLETE $OPTARG" ;;
	"u")		UPGRADE_ONLY=1 ;;
	esac
done
shift $((OPTIND - 1))

(($# == 0)) && usage
(($# > 1)) && usage "Too many arguments"

PROJECT="$1"


# Prevent user's environment to affect the integration.
# Allow one exception only: USERLAND_ARCHIVES
GMAKE="env -"
[[ -n "$USERLAND_ARCHIVES" ]] && GMAKE="$GMAKE USERLAND_ARCHIVES=$USERLAND_ARCHIVES"
GMAKE="$GMAKE gmake"


WS_TOP=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -z "$WS_TOP" ]] && usage "The script must be run in git repo"

BASE_DIR="$WS_TOP/components"
[[ -d "$BASE_DIR" ]] || usage "Directory $BASE_DIR not found"


# Distribution match project
DISTRIBUTION="$PROJECT"

function get_PKGINFO_entry
{
	typeset ENTRY="$1"

	[[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/PKG-INFO" ]] || return

	cat "$SOURCE_DIR$COMPONENT_SUBDIR/PKG-INFO" \
		| sed -e '/^$/,$d' \
		| awk 'END{printf("\n")}/^[^:]+: /{$0="\n"$0}1' ORS=' ' \
		| grep "^$ENTRY: " \
		| sed -e "s/^$ENTRY: //" -e 's/ *$//'
}


# Prepare the directory
[[ -z "$DIRECTORY" ]] && DIRECTORY="python/$DISTRIBUTION"
DIR="$BASE_DIR/$DIRECTORY"
mkdir -p "$DIR"
cd "$DIR"
git restore --staged . > /dev/null 2>&1
git checkout . > /dev/null 2>&1


# Following variables could be set by the hook-begin snippet
VERSION=
HOMEPAGE=
DOWNLOAD_URL=
LICENSE_FILE=
SUMMARY=

# Execute hook-begin snippet
if [[ -f "$CONF" ]] ; then
	gsed -e '0,/^%hook-begin%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
	. "./$SNIPPET"
	rm -f "$SNIPPET"
fi

# Version specified as option takes precedence
[[ -n "$OPT_VERSION" ]] && VERSION="$OPT_VERSION"


# Get data from PyPI if needed
if [[ -z "$VERSION" || -z "$HOMEPAGE" || -z "$SUMMARY" ]] ; then
	PYPI_PROJECT=$($CURL "$APIURL/$PROJECT/json")
	if (($? != 0)) || [[ -z "$PYPI_PROJECT" ]] ; then
		printf 'WARNING: Failed to get data for project %s from PyPI\n' "$PROJECT" >&2
		PYPI_PROJECT=
	fi
fi


# Find the latest version if not already provided
if [[ -z "$VERSION" ]] ; then
	VERSION=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.version')
	if (($? != 0)) || [[ -z "$VERSION" || "$VERSION" == "null" ]] ; then
		printf "FATAL: Failed to get version for project %s from pypi\n" "$PROJECT" >&2
		exit 1
	fi
fi


# Is this new project, or just a rebuild?
NEW=1
PREV_VER=
PREV_HVER=
PREV_REV=0
if git ls-files --error-unmatch Makefile > /dev/null 2>&1 ; then
	NEW=0
	PREV_VER=$($GMAKE print-value-COMPONENT_VERSION 2>/dev/null)
	(($? != 0)) && printf "FATAL: 'gmake print-value-COMPONENT_VERSION' failed!\n" >&2 && exit 1
	PREV_REV=$($GMAKE print-value-COMPONENT_REVISION 2>/dev/null)

	# If we were asked to do version upgrade, but we do not have new
	# version, then we are done.
	PREV_HVER=$($GMAKE print-value-HUMAN_VERSION 2>/dev/null)
	((UPGRADE_ONLY)) && [[ "$PREV_HVER" == "$VERSION" ]] && exit 0

	# Pre-flight environment checks
	if ((FORCE == 0)) ; then
		! $GMAKE env-check > /dev/null 2>&1 && printf "FATAL: Pre-flight 'gmake env-check' failed!\n" >&2 && exit 1
		if [[ "$($GMAKE print-value-PYTHON_TEST_BOOTSTRAP)" != "yes" ]] ; then
			! $GMAKE test-env-check > /dev/null 2>&1 && printf "FATAL: Pre-flight 'gmake test-env-check' failed!\n" >&2 && exit 1
		fi
	fi

	$GMAKE clobber > /dev/null 2>&1
fi


# Get project homepage if not already provided
if [[ -z "$HOMEPAGE" ]] ; then
	HOMEPAGE=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.home_page')
	if (($? != 0)) || [[ -z "$HOMEPAGE" || "$HOMEPAGE" == "null" ]] ; then
		HOMEPAGE=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.project_urls.Homepage')
		if (($? != 0)) || [[ -z "$HOMEPAGE" || "$HOMEPAGE" == "null" ]] ; then
			printf "WARNING: Failed to get homepage for project %s from pypi\n" "$PROJECT" >&2
			HOMEPAGE=$(get_PKGINFO_entry "Home-page")
		fi
	fi
fi

# Get download url if not already provided
if [[ -z "$DOWNLOAD_URL" ]] ; then
	# Get release data from pypi
	PYPI_PROJECT_RELEASE=$($CURL "$APIURL/$PROJECT/$VERSION/json")
	if (($? != 0)) || [[ -z "$PYPI_PROJECT_RELEASE" ]] ; then
		printf "FATAL: Failed to get data for version %s from pypi\n" "$VERSION" >&2
		exit 1
	fi

	# Get download url
	DOWNLOAD_URL=$(printf "%s" "$PYPI_PROJECT_RELEASE" | /usr/bin/jq -r '.urls[]|select(.packagetype=="sdist")|.url')
	if (($? != 0)) || [[ -z "$DOWNLOAD_URL" || "$DOWNLOAD_URL" == "null" ]] ; then
		printf "WARNING: Failed to get download url for project %s, version %s from pypi\n" "$PROJECT" "$VERSION" >&2
		DOWNLOAD_URL=
	fi
fi


# Remove everything that is not in git
rm -rf *
git checkout . > /dev/null 2>&1
# Remove everything from git (except known patches, files, history, and $CONF)
[[ -f "$CONF" ]] && grep "^%patch%" "$CONF" | while read TAG PATCH ; do rm -f "patches/$PATCH" ; done
[[ -f "$CONF" ]] && grep "^%file%" "$CONF" | while read TAG FILE ; do rm -f "files/$FILE" ; done
rm -f history "$CONF"
find . -type f | while read f ; do git rm "$f" > /dev/null 2>&1 ; done
rm -rf "$DIR" 2>/dev/null
git checkout history > /dev/null 2>&1
git checkout "$CONF" > /dev/null 2>&1
[[ -f "$CONF" ]] && grep "^%patch%" "$CONF" | while read TAG PATCH ; do
	git checkout "patches/$PATCH" > /dev/null 2>&1
	[[ -f "patches/$PATCH" ]] || printf "WARNING: Patch %s not found\n" "$PATCH" >&2
done
[[ -f "$CONF" ]] && grep "^%file%" "$CONF" | while read TAG FILE ; do
	git checkout "files/$FILE" > /dev/null 2>&1
	[[ -f "files/$FILE" ]] || printf "WARNING: File %s not found\n" "$FILE" >&2
done


# Makefile template
GENERATE_CMD="\$WS_TOOLS/$THIS"
[[ "$DIRECTORY" != "python/$DISTRIBUTION" ]] && GENERATE_CMD="$GENERATE_CMD -d $DIRECTORY"
GENERATE_CMD="$GENERATE_CMD $PROJECT"
(
cat $WS_TOP/transforms/copyright-template | sed -e '/^$/,$d'
cat <<EOF

#
# This file was automatically generated using the following command:
#   $GENERATE_CMD
#

BUILD_STYLE = pyproject
USE_COMMON_TEST_MASTER = no
EOF
[[ -f "$CONF" ]] && gsed -e '0,/^%include-1%/d' -e '/^%/,$d' < "$CONF"
cat <<EOF

include ../../../make-rules/shared-macros.mk

COMPONENT_NAME =		$DISTRIBUTION
HUMAN_VERSION =			$VERSION
COMPONENT_REVISION =		$((PREV_REV + 1))
COMPONENT_SUMMARY =		TODO
EOF
[[ -n "$HOMEPAGE" ]] && printf "COMPONENT_PROJECT_URL =\t\t%s\n" "$HOMEPAGE"
[[ -n "$DOWNLOAD_URL" ]] && printf 'DOWNLOAD_URL =\t\t\\\n\t%s\n' "$DOWNLOAD_URL"
cat <<EOF
COMPONENT_ARCHIVE_HASH =	\\
	sha256:TODO
COMPONENT_LICENSE =		license:TODO
COMPONENT_LICENSE_FILE =	licfile:TODO

_TEST_STYLE = TODO
EOF
[[ -f "$CONF" ]] && cat "$CONF" | gsed -e '0,/^%include-2%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
printf "\ninclude \$(WS_MAKE_RULES)/common.mk\n"
[[ -f "$CONF" ]] && cat "$CONF" | gsed -e '0,/^%include-3%/d' -e '/^%/,$d' | gsed -e '1s/^./\n&/'
printf "\n"
) > Makefile

# If the automatically constructed COMPONENT_ARCHIVE_URL points to the same
# location as DOWNLOAD_URL then we should use it
if [[ -n "$DOWNLOAD_URL" ]] ; then
	COMPONENT_ARCHIVE_URL=$($GMAKE print-value-COMPONENT_ARCHIVE_URL)
	[[ "$COMPONENT_ARCHIVE_URL" == "$DOWNLOAD_URL" ]] && DOWNLOAD_URL=
fi
# The default COMPONENT_ARCHIVE_URL usually redirects to DOWNLOAD_URL so check
# that too
if [[ -n "$DOWNLOAD_URL" ]] ; then
	[[ $($CURL --head --write-out "%{redirect_url}\n" --output /dev/null \
	    "$COMPONENT_ARCHIVE_URL") == "$DOWNLOAD_URL" ]] && DOWNLOAD_URL=
fi
# Use either DOWNLOAD_URL or default COMPONENT_ARCHIVE_URL
if [[ -n "$DOWNLOAD_URL" ]] ; then
	sed -i -e $'s/^DOWNLOAD_URL =/COMPONENT_ARCHIVE_URL =/' Makefile
else
	sed -i -e $'/^DOWNLOAD_URL =/,+1d' Makefile
fi

# Remove COMPONENT_REVISION if not needed
COMPONENT_VERSION=$($GMAKE print-value-COMPONENT_VERSION)
[[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && sed -i -e '/^COMPONENT_REVISION/d' Makefile
git add Makefile

# Calculate sham256 sum for source package
$GMAKE fetch > /dev/null 2>&1
USERLAND_ARCHIVES=$($GMAKE print-value-USERLAND_ARCHIVES)
COMPONENT_ARCHIVE=$($GMAKE print-value-COMPONENT_ARCHIVE)
[[ ! -f "$USERLAND_ARCHIVES$COMPONENT_ARCHIVE" ]] && printf "FATAL: 'gmake fetch' failed!\n" >&2 && exit 1
SHA256=$(digest -a sha256 "$USERLAND_ARCHIVES$COMPONENT_ARCHIVE")
sed -i -e 's/sha256:TODO/sha256:'"$SHA256"'/g' Makefile
git add Makefile

# Unpack sources and apply patches
! $GMAKE patch > /dev/null 2>&1 && printf "FATAL: 'gmake patch' failed!\n" >&2 && exit 1

# Refresh patches
if $GMAKE refresh-patches > /dev/null 2>&1 ; then
	git add patches 2>/dev/null
else
	printf "ERROR: 'gmake refresh-patches' failed!\n" >&2
	git checkout patches 2>/dev/null
fi

# Cleanup after patch refresh
$GMAKE clobber > /dev/null 2>&1

# Prepare sources
! $GMAKE prep > /dev/null 2>&1 && printf "FATAL: 'gmake prep' failed!\n" >&2 && exit 1
SOURCE_DIR=$($GMAKE print-value-SOURCE_DIR)
COMPONENT_SUBDIR=$($GMAKE print-value-COMPONENT_SUBDIR)
[[ -n "$COMPONENT_SUBDIR" ]] && COMPONENT_SUBDIR="/$COMPONENT_SUBDIR"

if [[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/pyproject.toml" ]] ; then
	[[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/setup.py" ]] && printf "FATAL: Neither pyproject.toml nor setup.py found!\n" >&2 && exit 1
	sed -i -e 's/^\(BUILD_STYLE = \).*$/\1setup.py/' Makefile
fi

# Get summary if not already provided
if [[ -z "$SUMMARY" ]] ; then
	SUMMARY=$(printf "%s" "$PYPI_PROJECT" | /usr/bin/jq -r '.info.summary')
	if (($? != 0)) || [[ -z "$SUMMARY" || "$SUMMARY" == "null" ]] ; then
		printf "WARNING: Failed to get summary for project %s from pypi\n" "$PROJECT" >&2
		SUMMARY=$(get_PKGINFO_entry "Summary")
		[[ -z "$SUMMARY" ]] && SUMMARY="TODO"
	fi
fi
# Summary needs to be sanitized
SUMMARY="${SUMMARY//\`/\\\\\`}"
SUMMARY="${SUMMARY//\"/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}"
SUMMARY="${SUMMARY//\//\/}"
SUMMARY="${SUMMARY//\$/\\\\\$\$}"
SUMMARY="${SUMMARY//\&/\\&}"
SUMMARY="${SUMMARY//#/\\\\#}"
sed -i -e 's/\(COMPONENT_SUMMARY.*\)TODO$/\1'"$SUMMARY"'/g' Makefile


# Try to detect license type(s)
function detect_license
{
	typeset -n L="$1"
	typeset F="$2"
	typeset D

	D=$("$WS_TOP/tools/license-detector" "$F")
	[[ -n "$L" ]] && L="$L OR " ; L="$L$D"
}

LICENSE=
LICFILE=
for f in $LICENSE_FILE $(get_PKGINFO_entry "License-File") LICENSE LICENSE.rst LICENSE.txt ; do
	[[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/$f" ]] || continue
	LICFILE="$f"

	detect_license LICENSE "$SOURCE_DIR$COMPONENT_SUBDIR/$LICFILE"
	[[ -n "$LICENSE" ]] && break

	printf "WARNING: Failed to detect license type in %s file\n" "$f" >&2
done
if [[ -z "$LICFILE" ]] ; then
	printf "WARNING: No license file found\n" >&2
else
	sed -i -e 's|licfile:TODO|'"$LICFILE"'|g' Makefile
fi

if [[ -z "$LICENSE" ]] ; then
	# Execute hook-no-license snippet
	if [[ -f "$CONF" ]] ; then
		gsed -e '0,/^%hook-no-license%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
		. "./$SNIPPET"
		rm -f "$SNIPPET"
	fi

	if [[ -f "$DISTRIBUTION.license" ]] ; then
		sed -i -e '/^COMPONENT_LICENSE_FILE/d' Makefile
		git add "$DISTRIBUTION.license"
		[[ -z "$LICENSE" ]] && detect_license LICENSE "$DISTRIBUTION.license"
	fi
	[[ -z "$LICENSE" ]] && LICENSE="TODO"
fi

# Store the detected license into the Makefile
sed -i -e 's/license:TODO/'"$LICENSE"'/g' Makefile


# detect TEST_STYLE
TEST_STYLE=
cd "$SOURCE_DIR$COMPONENT_SUBDIR"
while true ; do
	TOX_OUT=$(tox -l)
	TOX_RET=$?
	((TOX_RET == 0)) && ! printf "%s" "$TOX_OUT" | grep -q 'assuming empty tox\.ini' && TEST_STYLE="tox" && break

	# Disable some pytest plugins that almost always collects tests to run
	# even there are no pytest tests available otherwise.
	#
	# The system-statistics plugin is disabled because it often causes the
	# pytest to fail.
	# See also https://github.com/saltstack/pytest-system-statistics/issues/4
	pytest -p no:black -p no:checkdocs -p no:cov -p no:mypy -p no:relaxed -p no:system-statistics --setup-plan
	(($? != 5)) && TEST_STYLE="pytest" && break

	[[ -f setup.py ]] && python setup.py test --help && TEST_STYLE="setup.py" && break

	TEST_STYLE="none"
	break
done > /dev/null 2>&1
cd "$DIR"

if [[ "$TEST_STYLE" == "$($GMAKE print-value-TEST_STYLE)" ]] ; then
	# If the detected TEST_STYLE is same as the default value or the value
	# forced by the component, then we do not need to set the detected
	# value.
	sed -i -e '/^_TEST_STYLE = TODO$/,+1d' Makefile
else
	# Set the detected TEST_STYLE value
	sed -i -e 's/^_\(TEST_STYLE = \)TODO$/\1'"$TEST_STYLE/" Makefile

	# If the component forces different test style than detected, then drop
	# the detected value
	if [[ "$TEST_STYLE" != "$($GMAKE print-value-TEST_STYLE)" ]] ; then
		sed -i -e '/^TEST_STYLE = '"${TEST_STYLE//./\\.}/,+1d" Makefile
	fi
fi

# Warn if a testing tool is called directly by tox
if [[ "$($GMAKE print-value-TEST_STYLE)" == "tox" && -f "$SOURCE_DIR$COMPONENT_SUBDIR/tox.ini" ]] ; then
	TOX_CALL_INDIRECTLY=$($GMAKE print-value-TOX_CALL_INDIRECTLY)
	for p in $TOX_CALL_INDIRECTLY ; do
		sed -n -e '/^commands *=/,/^$/p' "$SOURCE_DIR$COMPONENT_SUBDIR/tox.ini" \
			| tr '\t' ' ' \
			| grep -q '^\(commands *=\)\{0,1\} *'"$p"'\( \{1,\}.*\)\{0,1\}$' \
		&& printf "WARNING: %s is called directly in tox.ini\n" "$p" >&2
	done
fi


# Create manifests
if ! $GMAKE sample-manifest > /dev/null 2>&1 ; then
	printf "ERROR: 'gmake sample-manifest' failed!\n" >&2
else
	MANIFEST="$DISTRIBUTION-PYVER.p5m"
	[[ "$($GMAKE print-value-SINGLE_PYTHON_VERSION)" == "yes" ]] && MANIFEST="$DISTRIBUTION.p5m"
	cat manifests/sample-manifest.p5m \
		| sed -e 's/^#.*Copyright.*<contributor>.*$/# This file was automatically generated using '"$THIS"'/g' \
		> "$MANIFEST"

	# Execute hook-manifest snippet
	if [[ -f "$CONF" ]] ; then
		gsed -e '0,/^%hook-manifest%/d' -e '/^%/,$d' < "$CONF" > "$SNIPPET"
		. "./$SNIPPET"
		rm -f "$SNIPPET"
	fi

	git add manifests/sample-manifest.p5m $MANIFEST
fi


# Generate REQUIRED_PACKAGES
$GMAKE REQUIRED_PACKAGES > /dev/null 2>&1 || printf "ERROR: 'gmake REQUIRED_PACKAGES' failed!\n" >&2
git add Makefile


# Check for Makefile completeness
grep -q "TODO" Makefile && printf "ERROR: Makefile is not complete (TODO found)\n" >&2


# Make sure the build environment is setup properly and we do have all
# requirements installed.  Otherwise we cannot continue.
! $GMAKE env-check > /dev/null 2>&1 && printf "FATAL: 'gmake env-check' failed!\n" >&2 && exit 1


# Handle history
COMPONENT_FMRI=$($GMAKE print-value-COMPONENT_FMRI)
PYTHON_VERSIONS_OBSOLETING=$($GMAKE print-value-PYTHON_VERSIONS_OBSOLETING)
OV=
OV_PLURAL=
for o in $(echo $OBSOLETE $PYTHON_VERSIONS_OBSOLETING | LC_ALL=C sort -u) ; do
	PYV=${o//.}
	FMRI=$(pkg list -nvH "$COMPONENT_FMRI-$PYV" 2>/dev/null | egrep -v '(o|r)$' | sed -e 's|^.*\('"$COMPONENT_FMRI"'\)|\1|g' -e 's/:[^:]*$//g' -e 's/\(-[^-]*\)$/,5.11\1/g')
	[[ -n "$FMRI" ]] || continue
	FMRI_H=${FMRI%.*}
	FMRI_T=${FMRI##*.}
	if [[ "$FMRI_H" == "$FMRI" ]] ; then
		printf "WARNING: Wrong fmri format: %s\n" "$FMRI" >&2
		continue
	fi
	FMRI_T=$((FMRI_T + 1))
	printf "%s.%s noincorporate\n" "$FMRI_H" "$FMRI_T" >> history

	[[ -n "$OV" ]] && OV="${OV/ and /, } and " && OV_PLURAL="s"
	OV="$OV$o"
done
if [[ -f history ]] ; then
	LC_ALL=C sort -u history > history.new
	mv history.new history
	git add history

	awk '$NF == "noincorporate" {printf("WARNING: Unincorporated package: %s\n", $1)}' < history >&2
fi


# Cleanup before we try to publish to make sure there are no leftovers from
# previous steps
$GMAKE clobber > /dev/null 2>&1

# Publish packages and create pkg5 file
$GMAKE publish > /dev/null 2>&1 || printf "ERROR: 'gmake publish' failed!\n" >&2
git add pkg5 2>/dev/null


PYTHON_VERSIONS=$($GMAKE print-value-PYTHON_VERSIONS)
PYTHON_TEST_BOOTSTRAP=$($GMAKE print-value-PYTHON_TEST_BOOTSTRAP)


# Run tests to make sure they pass and to create result snapshots
TESTED_VERSIONS=
for v in $PYTHON_VERSIONS ; do
	# Check the test environment
	if ! $GMAKE PYTHON_VERSIONS=$v test-env-check > /dev/null 2>&1 ; then
		if [[ "$PYTHON_TEST_BOOTSTRAP" == "yes" ]] ; then
			printf "WARNING: Test environment for %s is not ready yet (bootstrap)\n" "$v" >&2
		else
			printf "ERROR: 'gmake test-env-check' failed for %s!\n" "$v" >&2
		fi
		continue
	fi

	# Run the test
	! $GMAKE PYTHON_VERSIONS=$v test > /dev/null 2>&1 && printf "ERROR: Testing failed for %s!\n" "$v" >&2 && continue

	# If there is no snapshot produced the component likely does not support tests
	COMPONENT_TEST_SNAPSHOT=$($GMAKE PYTHON_VERSION=$v print-value-COMPONENT_TEST_SNAPSHOT)
	[[ ! -f "$COMPONENT_TEST_SNAPSHOT" ]] && printf "WARNING: Testing unsupported for %s\n" "$v" >&2 && continue

	# Empty result snapshot is suspicious
	[[ -s "$COMPONENT_TEST_SNAPSHOT" ]] || printf "WARNING: Empty test results for %s\n" "$v" >&2

	TESTED_VERSIONS="$TESTED_VERSIONS $v"
done

# Save result snapshots and detect USE_COMMON_TEST_MASTER value
TEST_MASTERS=
for common_results in yes no ; do
	for v in $TESTED_VERSIONS ; do
		COMPONENT_TEST_SNAPSHOT=$($GMAKE PYTHON_VERSION=$v print-value-COMPONENT_TEST_SNAPSHOT)
		COMPONENT_TEST_MASTER=$($GMAKE PYTHON_VERSION=$v USE_COMMON_TEST_MASTER=$common_results print-value-COMPONENT_TEST_MASTER)

		if [[ -f "$COMPONENT_TEST_MASTER" ]] ; then
			# Switch to 'USE_COMMON_TEST_MASTER = no' if test results differ
			if ! diff "$COMPONENT_TEST_SNAPSHOT" "$COMPONENT_TEST_MASTER" > /dev/null ; then
				printf "WARNING: Test results differ so switch to 'USE_COMMON_TEST_MASTER = no'\n" >&2
				rm -f $TEST_MASTERS
				TEST_MASTERS=
				continue 2
			fi
		else
			mkdir -p $(dirname "$COMPONENT_TEST_MASTER")
			cp -p "$COMPONENT_TEST_SNAPSHOT" "$COMPONENT_TEST_MASTER"
			TEST_MASTERS="$TEST_MASTERS $COMPONENT_TEST_MASTER"
		fi
	done
	break
done
[[ -n "$TEST_MASTERS" ]] && git add $TEST_MASTERS

# Run tests again to confirm the results are reproducible
for v in $TESTED_VERSIONS ; do
	$GMAKE PYTHON_VERSIONS=$v USE_COMMON_TEST_MASTER=$common_results test > /dev/null 2>&1 || printf "ERROR: Testing for %s is not reproducible!\n" "$v" >&2
done

# Remove USE_COMMON_TEST_MASTER from Makefile if it should be set to (default) 'yes'
if [[ "$common_results" == "yes" ]] ; then
	sed -i -e '/^USE_COMMON_TEST_MASTER/d' Makefile
	git add Makefile
fi


# Create .gitignore
COMPONENT_SRC=$($GMAKE print-value-COMPONENT_SRC)
printf '/%s/\n' "$COMPONENT_SRC" > .gitignore
git add .gitignore


# Construct the commit message
MSG=
if ((NEW)) ; then
	MSG="Add $PROJECT Python project"
else
	[[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && MSG="change version format"
	[[ "$PREV_HVER" != "$VERSION" ]] && MSG="update to $VERSION"

	REBUILDMSG=

	if [[ "$($GMAKE print-value-SINGLE_PYTHON_VERSION)" == "no" ]] ; then
		NV=
		for v in $PYTHON_VERSIONS ; do
			PYV=${v//.}
			pkg list -avH "$COMPONENT_FMRI-$PYV" 2>/dev/null | egrep -q -v '(o|r)$' && continue
			[[ -n "$NV" ]] && NV="$NV and "
			NV="$NV$v"
		done
		[[ -n "$NV" ]] && REBUILDMSG="rebuild for Python $NV"
	fi

	if [[ -n "$OV" ]] ; then
		[[ -n "$REBUILDMSG" ]] && REBUILDMSG="$REBUILDMSG and "
		REBUILDMSG="${REBUILDMSG}obsolete package$OV_PLURAL for Python $OV"
	fi

	if [[ -n "$REBUILDMSG" ]] ; then
		[[ -n "$MSG" ]] && MSG="$MSG; "
		MSG="$MSG$REBUILDMSG"
	fi
	[[ -z "$MSG" ]] && MSG="rebuild"

	MSG="$DIRECTORY: $MSG"
fi

# Commit the results
! git commit -m "$MSG" > /dev/null 2>&1 && printf "FATAL: 'git commit' failed!\n" >&2 && exit 1