#!/bin/sh

# the purpose of this script is to speed-up the support
# of additional variants. it is not very pretty, but
# does the job quite well. please read the script in
# its entirety before executing it.

set -eu

trap usage 1 EXIT

py_refdir=$(pwd)
py_hoppla=$(basename "$0")
py_differ=

usage()
{
	printf '%s: %s %s\n' "$py_hoppla" \
		'please read this script' \
		'and understand what it does.' >&2
	exit 1
}

if ! [ -z ${4:-} ]; then
	printf '%s: too many arguments.\n' "$py_hoppla" >&2
	exit 1
fi

if ! [ -z ${3:-} ]; then
	if [ "$3" != '--diff-only' ] && [ "$3" != '-d' ]; then
		printf '%s: %s %s\n' "$py_hoppla" \
			'invalid third argument' \
			'(must be --diff-only or -d).' >&2
		exit 1
	else
		py_differ=yes
	fi
fi

py_base_ver="$1"
py_curr_ver="$2"

PY_SOURCE_PREFIX=${PY_SOURCE_PREFIX:-}

if [ -z $PY_SOURCE_PREFIX ]; then
	PY_SOURCE_PREFIX=$py_refdir
fi

for py_ver in "$py_base_ver" "$py_curr_ver"; do
	case "$py_ver" in
		[0-9].[0-9].[0-9] )
			if ! [ -d "$PY_SOURCE_PREFIX"/Python-$py_ver ]; then
				printf '%s: %s: %s\n' "$py_hoppla" \
					"$PY_SOURCE_PREFIX"/Python-$py_ver \
					'the directory does not exist.' >&2
					exit 1
			fi
			;;
		* )
			printf '%s: %s %s\n' "$py_hoppla" \
				">>$py_ver<<" 'is not a valid python version.' >&2
			exit 1
	esac
done

for py_ver in $py_base_ver $py_curr_ver; do
	if [ -z $py_differ ]; then
		mkdir $py_refdir/$py_ver.build
		cd    $py_refdir/$py_ver.build

		printf '%s: configure.\n' $py_ver

		"$PY_SOURCE_PREFIX"/Python-$py_ver/configure \
			--enable-ipv6 > /dev/null

		printf '%s: make.\n' $py_ver

		make V=2 >pylog
	else
		cd $py_refdir/$py_ver.build
	fi

	py_dot=$(printf '%s' $py_ver | sed -e 's/\.[^.]*$//g')
	py_abi=$(printf '%s' $py_dot | sed -e 's/\.//g')

	grep -e ' -c ' pylog | grep -v -e 'build/temp.' -e '/install' \
			| sed -e 's@Python-'$py_ver/'@Python-pyver/@g' \
			      -e 's@-Wstrict-prototypes@@g' \
			      -e 's@-Werror=implicit-function-declaration@@g' \
			      -e 's@-Wno-unused-result@@g' \
			      -e 's@-Wsign-compare@@g' \
			      -e 's@-DNDEBUGe@@g' \
			      -e 's@-DPy_BUILD_CORE_BUILTIN@@g' \
			      -e 's@-DPy_BUILD_CORE@@g' \
			      -e 's@-g -fwrapv -O3 -Wall@@g' \
			      -e 's@-std=c99 -Wextra@@g' \
			      -e 's@-Wno-missing-field-initializers@@g' \
			      -e 's@-Wno-unused-result@@g' \
			      -e 's@-Wno-unused-parameter@@g' \
			      -e 's@-Wno-implicit-fallthrough@@g' \
			      -e 's/  */ /g' \
			      -e 's/[ \t]*$//' \
		| sort > cccore.log

	grep -e ' -c ' pylog | grep -e 'build/temp.' \
			| sed -e 's@Python-'$py_ver/'@Python-pyver/@g' \
			      -e 's@'/$py_ver.build'@/pyver.build@g' \
			      -e 's@'$py_dot/'@pydot/@g' \
			      -e 's@-Wstrict-prototypes@@g' \
			      -e 's@-Werror=implicit-function-declaration@@g' \
			      -e 's@-Wno-unused-result@@g' \
			      -e 's@-Wsign-compare@@g' \
			      -e 's@-DNDEBUGe@@g' \
			      -e 's@-DBLAKE2_USE_SSE=1@@g' \
			      -e 's@-g -fwrapv -O3 -Wall@@g' \
			      -e 's@-std=c99 -Wextra@@g' \
			      -e 's@-Wno-missing-field-initializers@@g' \
			      -e 's@-Wno-unused-result@@g' \
			      -e 's@-Wno-unused-parameter@@g' \
			      -e 's@-Wno-implicit-fallthrough@@g' \
			      -e 's/  */ /g' \
			      -e 's/[ \t]*$//' \
		| sort > ccext.log

	grep -e ' -shared ' pylog | grep -e '.cpython-' \
			| sed -e 's@Python-'$py_ver/'@Python-pyver/@g' \
			      -e 's@.cpython-'$py_abi'@.cpython-pyabi.@g' \
			      -e 's@'-$py_dot'/@-pydot/@g' \
		| sort > pyext.log

	find . -name '*.cpython-*' \
			| sed -e 's@.cpython-'$py_abi'@.cpython-pyabi.@g' \
			      -e 's@'-$py_dot'/@-pydot/@g' \
		| sort > pyext.lst

	find "$PY_SOURCE_PREFIX"/Python-$py_ver/Include -maxdepth 1 -name '*.h' \
			| sed -e 's@Python-'$py_ver/'@Python-pyver/@g' \
		| sort > pyhdr.lst
done

diffdir=$py_base_ver-vs-$py_curr_ver
logfiles="cccore.log ccext.log pyext.log pyext.lst pyhdr.lst"
genfiles="pyconfig.h Modules/config.c"

cd $py_refdir
mkdir -p $diffdir

for f in $logfiles $genfiles; do
	diffname=$(basename $f | sed -e 's/\.[^.]*$//g')

	diff -u \
			$py_refdir/$py_base_ver.build/$f \
			$py_refdir/$py_curr_ver.build/$f \
		> $diffdir/$diffname.diff || true
done

trap '' 0
echo yay.

exit 0
