#!/usr/bin/env bash

files="$@"

function create_bin_file
{
cat << EOF > "$bindir/backshift"
#!/usr/bin/env bash

if [ "\$*" = "" ]
then
	"$which_python" "$prefix"/lib/backshift/backshift
else
	"$which_python" "$prefix"/lib/backshift/backshift "\$@"
fi

EOF
}

if [ -d install-variables ]
then
	prefix=$(cat 'install-variables'/prefix-file)
	bindir=$(cat 'install-variables'/bindir-file)
	which_python=$(cat 'install-variables'/python-file)
	use_cython=$(cat 'install-variables'/use-cython-file)

	mkdir -p "$bindir"
	mkdir -p "$prefix"/lib/backshift

	# Install the pure python modules
	cp $files "$prefix"/lib/backshift

	# Optionally install the Cython
	if [ "$use_cython" = "True" ]
	then
		./build-one-rcm '--install-to-site-packages' --use-installation-python
	fi

	# Install the expiration and randomization scripts
	cp \
		backshift-expire \
		randomize \
		"$bindir"/.

	# Finally, create the frontend bash script and make it executable
	create_bin_file
	chmod 755 "$bindir/backshift"

else
	echo "$0: You must run ./configure before 'make install'" 1>&2
	exit 1
fi