#! /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="perl-integrate-module"
CONF="$THIS.conf"
SNIPPET="$THIS.snippet"
APIURL="https://fastapi.metacpan.org/v1"
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] DISTRIBUTION|MODULE\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"

MODULE="$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"


# Query MetaCPAN
METACPAN_DATA=$( (printf '{"query":{"bool":{"must":[{"term":{"maturity":"released"}},' ; \
	printf '{"term":{"distribution":"%s"}}],' "$MODULE" ; \
	printf '"must_not":{"term":{"status":"backpan"}}}},' ; \
	printf '"size":1,"sort":[{"date":"desc"}],' ; \
	printf '"fields":["distribution","version","author","download_url","abstract"]}') \
		| $CURL "$APIURL/release/_search" -d @- \
		| /usr/bin/jq -r '.hits.hits[].fields')
if (($? != 0)) ; then
	printf 'FATAL: Failed to get distribution info from MetaCPAN\n' >&2
	exit 1
fi

# If we didn't found the distribution directly try to find the module
if [[ -z "$METACPAN_DATA" || "$METACPAN_DATA" == "null" ]] ; then
	DISTRIBUTION=$( (printf '{"query":{"bool":{"must":[{"term":{"maturity":"released"}},' ; \
		printf '{"term":{"module.name":"%s"}}],' "$MODULE" ; \
		printf '"must_not":{"term":{"status":"backpan"}}}},' ; \
		printf '"size":1,"fields":["distribution"]}') \
			| $CURL "$APIURL/module/_search" -d @- \
			| /usr/bin/jq -r '.hits.hits[].fields.distribution')
	if (($? != 0)) || [[ -z "$DISTRIBUTION" || "$DISTRIBUTION" == "null" ]] ; then
		printf 'FATAL: Failed to find distribution for %s at MetaCPAN\n' "$MODULE" >&2
		exit 1
	fi
	METACPAN_DATA=$( (printf '{"query":{"bool":{"must":[{"term":{"maturity":"released"}},' ; \
		printf '{"term":{"distribution":"%s"}}],' "$DISTRIBUTION" ; \
		printf '"must_not":{"term":{"status":"backpan"}}}},' ; \
		printf '"size":1,"sort":[{"date":"desc"}],' ; \
		printf '"fields":["distribution","version","author","download_url","abstract"]}') \
			| $CURL "$APIURL/release/_search" -d @- \
			| /usr/bin/jq -r '.hits.hits[].fields')
	if (($? != 0)) || [[ -z "$METACPAN_DATA" || "$METACPAN_DATA" == "null" ]] ; then
		printf 'FATAL: Failed to get distribution info from MetaCPAN\n' >&2
		exit 1
	fi
fi

# Get distribution
DISTRIBUTION=$(printf '%s' "$METACPAN_DATA" | /usr/bin/jq -r '.distribution')
if (($? != 0)) || [[ -z "$DISTRIBUTION" || "$DISTRIBUTION" == "null" ]] ; then
	printf 'FATAL: Failed to confirm or detect distribution\n' >&2
	exit 1
fi
if [[ "$DISTRIBUTION" != "$MODULE" ]] then
	printf 'WARNING: Using distribution %s for module %s\n' "$DISTRIBUTION" "$MODULE" >&2
fi


# Prepare the directory
[[ -z "$DIRECTORY" ]] && DIRECTORY="perl/$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=
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"

# Find the latest version
LATEST_VERSION=$(printf '%s' "$METACPAN_DATA" | /usr/bin/jq -r '.version')
if (($? != 0)) || [[ -z "$LATEST_VERSION" || "$LATEST_VERSION" == "null" ]] ; then
	printf 'FATAL: Failed to detect the latest version\n' >&2
	exit 1
fi

# Use the latest version if version was not provided
if [[ -z "$VERSION" ]] ; then
	VERSION=$LATEST_VERSION
elif [[ "$VERSION" != "$LATEST_VERSION" ]] ; then
	# MetaCPAN data are no longer valid because we need different version
	METACPAN_DATA=
fi


# Is this new module, 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-PERL_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 download_url if not already provided
if [[ -z "$DOWNLOAD_URL" ]] ; then
	# Get new MetaCPAN data if needed
	if [[ -z "$METACPAN_DATA" ]] ; then
		METACPAN_DATA=$( (printf '{"query":{"bool":{"must":[' ; \
			printf '{"term":{"maturity":"released"}},' ; \
			printf '{"term":{"distribution":"%s"}},' "$DISTRIBUTION" ; \
			printf '{"term":{"version":"%s"}}]}},' "$VERSION" ; \
			printf '"size":1,' ; \
			printf '"fields":["author","download_url","abstract"]}') \
				| $CURL "$APIURL/release/_search" -d @- \
				| /usr/bin/jq -r '.hits.hits[].fields')
		if (($? != 0)) || [[ -z "$METACPAN_DATA" || "$METACPAN_DATA" == "null" ]] ; then
			printf 'FATAL: Failed to get data from MetaCPAN\n' >&2
			exit 1
		fi
	fi

	# Get author
	AUTHOR=$(printf '%s' "$METACPAN_DATA" | /usr/bin/jq -r '.author')
	if (($? != 0)) || [[ -z "$AUTHOR" || "$AUTHOR" == "null" ]] ; then
		printf 'FATAL: Failed to get author from MetaCPAN\n' >&2
		exit 1
	fi

	# Get download_url
	DOWNLOAD_URL=$(printf '%s' "$METACPAN_DATA" | /usr/bin/jq -r '.download_url')
	if (($? != 0)) || [[ -z "$DOWNLOAD_URL" || "$DOWNLOAD_URL" == "null" ]] ; then
		printf 'WARNING: Failed to get download_url for version %s from MetaCPAN\n' "$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" != "perl/$DISTRIBUTION" ]] && GENERATE_CMD="$GENERATE_CMD -d $DIRECTORY"
GENERATE_CMD="$GENERATE_CMD $DISTRIBUTION"
(
cat $WS_TOP/transforms/copyright-template | sed -e '/^$/,$d'
cat <<EOF

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

BUILD_STYLE = makemaker
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
COMPONENT_CPAN_AUTHOR =		$AUTHOR
COMPONENT_ARCHIVE_HASH =	\\
	sha256:TODO
COMPONENT_LICENSE =		license:TODO
COMPONENT_LICENSE_FILE =	licfile: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 is not correct then we
# do not need COMPONENT_CPAN_AUTHOR.  We need COMPONENT_ARCHIVE_URL instead.
if [[ -n "$DOWNLOAD_URL" ]] ; then
	COMPONENT_ARCHIVE_URL=$($GMAKE print-value-COMPONENT_ARCHIVE_URL)
	[[ "$COMPONENT_ARCHIVE_URL" == "$DOWNLOAD_URL" ]] && DOWNLOAD_URL=
fi
[[ -n "$DOWNLOAD_URL" ]] && sed -i -e $'s|^COMPONENT_CPAN_AUTHOR.*|COMPONENT_ARCHIVE_URL =\t\t\\\\\\\n\t'"$DOWNLOAD_URL"'|' Makefile

# 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"

# Switch to modulebuild if possible
[[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/Build.PL" ]] && sed -i -e 's/makemaker/modulebuild/g' Makefile

# Get summary if not already provided
if [[ -z "$SUMMARY" ]] ; then
	# Get new MetaCPAN data if needed
	if [[ -z "$METACPAN_DATA" ]] ; then
		METACPAN_DATA=$( (printf '{"query":{"bool":{"must":[' ; \
			printf '{"term":{"maturity":"released"}},' ; \
			printf '{"term":{"distribution":"%s"}},' "$DISTRIBUTION" ; \
			printf '{"term":{"version":"%s"}}]}},' "$VERSION" ; \
			printf '"size":1,"fields":["abstract"]}') \
				| $CURL "$APIURL/release/_search" -d @- \
				| /usr/bin/jq -r '.hits.hits[].fields')
		if (($? != 0)) || [[ -z "$METACPAN_DATA" || "$METACPAN_DATA" == "null" ]] ; then
			printf 'FATAL: Failed to get data from MetaCPAN\n' >&2
			exit 1
		fi
	fi

	# Get abstract and use it as summary.  Either from MetaCPAN, or directly from sources.
	ABSTRACT=$(printf '%s' "$METACPAN_DATA" | /usr/bin/jq -r '.abstract')
	if (($? != 0)) || [[ -z "$ABSTRACT" || "$ABSTRACT" == "null" ]] ; then
		printf 'WARNING: Failed to get abstract from MetaCPAN\n' >&2
		ABSTRACT="TODO"
	fi
	if [[ "$ABSTRACT" == "TODO" ]] ; then
		if [[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/META.json" ]] ; then
			printf "WARNING: META.json missing\n" >&2
		else
			ABSTRACT=$(cat "$SOURCE_DIR$COMPONENT_SUBDIR/META.json" | /usr/bin/jq -r '.abstract')
			if (($? != 0)) || [[ -z "$ABSTRACT" || "$ABSTRACT" == "null" ]] ; then
				printf "WARNING: Failed to get abstract from META.json\n" >&2
				ABSTRACT="TODO"
			fi
		fi
	fi
	if [[ "$ABSTRACT" == "TODO" ]] ; then
		if [[ ! -f "$SOURCE_DIR$COMPONENT_SUBDIR/META.yml" ]] ; then
			printf "WARNING: META.yml missing\n" >&2
		else
			ABSTRACT=$(cat "$SOURCE_DIR$COMPONENT_SUBDIR/META.yml" | python -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))' | /usr/bin/jq -r '.abstract')
			if (($? != 0)) || [[ -z "$ABSTRACT" || "$ABSTRACT" == "null" ]] ; then
				printf "WARNING: Failed to get abstract from META.yml\n" >&2
				ABSTRACT="TODO"
			fi
		fi
	fi
	SUMMARY="$ABSTRACT"
fi
# Summary needs to be sanitized
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=
COPYRIGHT=
for f in $LICENSE_FILE LICENSE LICENCE COPYING COPYRIGHT ; do
	[[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/$f" ]] || continue
	LICFILE="$SOURCE_DIR$COMPONENT_SUBDIR/$f"

	detect_license LICENSE "$LICFILE"

	if [[ -n "$LICENSE" ]] ; then
		sed -i -e 's|licfile:TODO|'"$f"'|g' Makefile
		break
	fi

	printf "WARNING: Failed to detect license type in %s file\n" "$f" >&2

	# Since the license file does not contain any known license we will use
	# its content as Copyright notice only
	COPYRIGHT=$(<"$LICFILE")
done
if [[ -z "$LICFILE" ]] ; then
	printf "WARNING: No license file found\n" >&2
fi

if [[ -z "$LICENSE" ]] ; then
	# Since the distibution does not provide own license file (or we failed
	# to find it) we will use default Perl license with added Copyright
	# notice from this distribution

	sed -i -e '/^COMPONENT_LICENSE_FILE/d' Makefile

	# Try to find Copyright notice if we do not have one yet
	[[ -z "$COPYRIGHT" ]] && for f in README README.md ; do
		f="$SOURCE_DIR$COMPONENT_SUBDIR/$f"
		[[ -f "$f" ]] || continue

		COPYRIGHT=$(gsed -e '0,/^# LICENSE/d' -e '/^#/,$d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
		COPYRIGHT=$(gsed -e '0,/^# COPYRIGHT/d' -e '/^#/,$d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
		COPYRIGHT=$(gsed -e '0,/LICENSE/d' -e '/^REPOSITORY/,$d' -e '/^SEE/,$d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
		COPYRIGHT=$(gsed -e '0,/COPYING/d' -e '/^BUGS/,$d' -e '/^SEE/,$d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
		COPYRIGHT=$(gsed -e '0,/COPYRIGHT/d' -e '/^AUTHOR/,$d' -e '/^SEE/,$d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
		COPYRIGHT=$(gsed -e '0,/^## Copyright/d' -e '/./,$!d' "$f" 2>/dev/null)
		[[ -n "$COPYRIGHT" ]] && break
	done
	if [[ -z "$COPYRIGHT" ]] ; then
		printf "WARNING: No copyright notice found at standard locations\n" >&2
		for f in $(find "$SOURCE_DIR$COMPONENT_SUBDIR" -type f -name "*.pm" | LC_ALL=C sort | while read f ; do egrep -q "^=head1 (LICENSE|LICENCE|COPYRIGHT)" "$f" && echo "$f" ; done) ; do
			COPYRIGHT=$(sed -e '1,/^=head1 LICENSE/d' -e '/^=/,$d' "$f" 2>/dev/null)
			if [[ -n "$COPYRIGHT" ]] ; then
				printf "WARNING: Using copyright notice from %s\n" "$f" >&2
				break
			fi
			COPYRIGHT=$(sed -e '1,/^=head1 LICENCE/d' -e '/^=/,$d' "$f" 2>/dev/null)
			if [[ -n "$COPYRIGHT" ]] ; then
				printf "WARNING: Using copyright notice from %s\n" "$f" >&2
				break
			fi
			COPYRIGHT=$(sed -e '1,/^=head1 COPYRIGHT/d' -e '/^=/,$d' "$f" 2>/dev/null)
			if [[ -n "$COPYRIGHT" ]] ; then
				printf "WARNING: Using copyright notice from %s\n" "$f" >&2
				break
			fi
		done
	fi
	if [[ -z "$COPYRIGHT" ]] ; then
		printf "WARNING: No copyright notice found\n" >&2
		> "$DISTRIBUTION.license"
	else
		(printf "%s\n\n" "$COPYRIGHT" | dos2unix -ascii
		i=75 ; while ((i)) ; do printf "=" ; i=$((i-1)) ; done
		printf "\n\n") > "$DISTRIBUTION.license"
	fi

	USE_DEFAULT_PERL_LICENSE=1

	# 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 ((USE_DEFAULT_PERL_LICENSE)) ; then
		# Confirm the package is distributed under the same terms as Perl itself
		D=1
		((D)) && (printf "%s\n" "$COPYRIGHT" | grep -q -i "under the same terms as Perl itself") && D=0
		((D)) && grep -q "license *=> *'http://dev\.perl\.org/licenses/'" "$SOURCE_DIR$COMPONENT_SUBDIR/Makefile.PL" 2>/dev/null && D=0
		((D)) && grep -q "LICENSE *=> *'perl'" "$SOURCE_DIR$COMPONENT_SUBDIR/Makefile.PL" 2>/dev/null && D=0
		((D)) && [[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/META.json" && "$(/usr/bin/jq -r '.license[]' < "$SOURCE_DIR$COMPONENT_SUBDIR/META.json" 2>/dev/null)" == "perl_5" ]] && D=0
		((D)) && [[ -f "$SOURCE_DIR$COMPONENT_SUBDIR/META.yml" && "$(cat "$SOURCE_DIR$COMPONENT_SUBDIR/META.yml" \
			| python -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))' \
			| /usr/bin/jq -r '.license' 2>/dev/null)" == "perl" ]] && D=0

		((D)) && printf "ERROR: Heuristics failed to detect license type, using default Perl license\n" >&2

		# Make a copy of license so we can use it during publish
		cat "$WS_TOP/tools/perl-license" | grep -v "^#" >> "$DISTRIBUTION.license"
	fi
	git add "$DISTRIBUTION.license"

	[[ -z "$LICENSE" ]] && detect_license LICENSE "$DISTRIBUTION.license"
	[[ -z "$LICENSE" ]] && LICENSE="TODO"
fi

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


# Create manifests
if ! $GMAKE sample-manifest > /dev/null 2>&1 ; then
	printf "ERROR: 'gmake sample-manifest' failed!\n" >&2
else
	MANIFEST="$DISTRIBUTION-PERLVER.p5m"
	[[ "$($GMAKE print-value-SINGLE_PERL_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)
PERL_VERSIONS_OBSOLETING=$($GMAKE print-value-PERL_VERSIONS_OBSOLETING)
OV=
OV_PLURAL=
for o in $(echo $OBSOLETE $PERL_VERSIONS_OBSOLETING | LC_ALL=C sort -u) ; do
	PLV=${o//.}
	FMRI=$(pkg list -nvH "$COMPONENT_FMRI-$PLV" 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


PERL_VERSIONS=$($GMAKE print-value-PERL_VERSIONS)
PERL_TEST_BOOTSTRAP=$($GMAKE print-value-PERL_TEST_BOOTSTRAP)


# Run tests to make sure they pass and to create result snapshots
TESTED_VERSIONS=
for v in $PERL_VERSIONS ; do
	# Check the test environment
	if ! $GMAKE PERL_VERSIONS=$v test-env-check > /dev/null 2>&1 ; then
		if [[ "$PERL_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 PERL_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 PERL_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 PERL_VERSION=$v print-value-COMPONENT_TEST_SNAPSHOT)
		COMPONENT_TEST_MASTER=$($GMAKE PERL_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 PERL_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 $DISTRIBUTION Perl distribution"
else
	[[ "$PREV_VER" != "$COMPONENT_VERSION" ]] && MSG="change version format"
	[[ "$PREV_HVER" != "$VERSION" ]] && MSG="update to $VERSION"

	REBUILDMSG=

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

	if [[ -n "$OV" ]] ; then
		[[ -n "$REBUILDMSG" ]] && REBUILDMSG="$REBUILDMSG and "
		REBUILDMSG="${REBUILDMSG}obsolete package$OV_PLURAL for Perl $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