#!/usr/bin/env bash

set -eu
set -o pipefail

install_to_site_packages=False
if [ "${which_python:-}" = "" ]
then
	which_python=""
fi

function usage
{
	retval="$1"
	(
	echo "Usage: $0"
	echo "   --install-to-site-packages"
	echo "   --use-installation-python"
	echo "   --python /custom/path/to/python/bin/python"
	echo "   --help"
	echo
	echo "You must give one of --use-installation-python or --python"
	) 1>&2
	exit "$retval"
}

while [ "$#" -ge 1 ]
do
	case "$1" in	
		--install-to-site-packages)
			install_to_site_packages=True
			;;
		--use-installation-python)
			if [ -e install-variables/python-file ]
			then
				which_python=$(set -eu; cat install-variables/python-file)
			else
				echo "$0: You must run ./configure before using --use-installation-python" 1>&2
				exit 1
			fi
			;;
		--python)
			which_python="$2"
			shift
			;;
		--help|-h)
			usage 0
			;;
		*)
			echo "$0: Unrecognized option: $1" 1>&2
			usage 1
			;;
	esac
	shift
done

python_version=$("$which_python" --version 2>&1 | head -1 | sed 's/^Python \([0-9]\+\.[0-9]\+\)\..*$/\1/')
echo "Using Python version $python_version"

case "$which_python" in
	"")
		echo "$0: No Python specified - please give --python or --use-installation-python" 1>&2
		usage 1
		;;
	*/bin/*)
        # shellcheck disable=SC2001
		python_prefix_dir=$(set -eu; set -o pipefail > /dev/null 2>&1 || true; echo "$which_python" | sed 's#/bin/[^/]*$##')
		;;
	*)
		echo "$0: Sorry, I do not know how to derive a python prefix directory from $which_python" 1>&2
		exit 1
		;;
esac

python_include_dir=$(set -eu;
	set -o pipefail > /dev/null 2>&1 || true;
	find \
		"$python_prefix_dir/include" \
		-type d \
		-name "python$python_version*" \
		-print | \
	grep -E '^.*/include/python[0-9]*\.[0-9]*m?$')

rm -f rolling_checksum_pyx_mod.c
rm -f rolling_checksum_pyx_mod.o
rm -f rolling_checksum_pyx_mod.so

# Create the .c from the .pyx
"$which_python" -m cython rolling_checksum_pyx_mod.pyx

# Create the .o from the .c
gcc \
	-pthread \
	-fno-strict-aliasing \
	-DNDEBUG \
	-g \
	-fwrapv \
	-O2 \
	-Wall \
	-Wstrict-prototypes \
	-fPIC \
	-I "$python_include_dir" \
	-c rolling_checksum_pyx_mod.c

# Create the .so from the .o
gcc \
	-pthread \
	-shared \
	-Wl,-O1 \
	-Wl,'-Bsymbolic-functions' \
	rolling_checksum_pyx_mod.o \
	-o rolling_checksum_pyx_mod.so

if [ "$install_to_site_packages" = True ]
then
	# This assumes the libdir hierarchy has no filenames with newlines in them - probably a pretty safe assumption
	site_packages_dir=$(set -eu;
		set -o pipefail > /dev/null 2>&1 || true;
        if [ -d "$python_prefix_dir/lib/python$python_version" ]
        then
            echo "$python_prefix_dir/lib/python$python_version"
        else
            find "$python_prefix_dir/lib" -type d -name site-packages -print | \
                head -1
        fi)

	if [ "$site_packages_dir" = "" ]
	then
		echo "$0: Sorry, I did not find a site-packages directory under $which_python's lib directory" 1>&2
		exit 1
	fi

	cp rolling_checksum_pyx_mod.so "$site_packages_dir"/.
fi