#!/bin/sh -e
EXTERNAL="/afs/crc.nd.edu/group/ccl/software"

if [ ! -d "${EXTERNAL}" ]
then
    cat <<EOF
This configure script is tailored to compile CCTools at Notre Dame with all of
the optional packages enabled. It may be of little use outside Notre Dame.
EOF
    PLATFORM=nosuchplatform
else
    # Our external packages are installed in $EXTERNAL/PLATFORM/packagename/cctools-dep
    PLATFORM=$(${EXTERNAL}/cclplatform.sh)
    echo "platform is ${PLATFORM}"
fi

detect_packages()
{
        # cleanup GLOBUS_LOCATION
	unset GLOBUS_LOCATION
	export GLOBUS_LOCATION

	echo "$0: Detecting packages ..."

	PACKAGES_CONFIG=""
        # perl and python taken from host
	for package in fuse irods mysql python3 globus swig xrootd cvmfs uuid
	do
	packagepath=$EXTERNAL/${PLATFORM}/$package/cctools-dep

	if [ -d $packagepath ]
		then
			echo "$package -> $packagepath"
			PACKAGES_CONFIG="$PACKAGES_CONFIG --with-$package-path $packagepath"
	else
			echo "$package -> NOTFOUND"
		fi
	done

	echo ""
}


BRANCH="$CCTOOLS_BRANCH"
SOURCE="from source"

GIT_DIR="$(dirname "$0")/.git"
export GIT_DIR
if [ -d "$GIT_DIR" ] && which git > /dev/null 2> /dev/null; then
	# `git archive` (man gitattributes(5)) fills this in, otherwise we do it manually...
	COMMIT='$Format:%H$'
	if [ "$(expr substr "$COMMIT" 1 1)" = '$' ]; then
		COMMIT="$(git rev-parse HEAD)"
	fi

	DATE='$Format:%ci$'
	if [ "${DATE:0:1}" = '$' ]; then
		DATE="$(git log -1 --pretty=format:%ci HEAD)"
	fi

	DIRTY=
	if [ -n "$(git status --porcelain)" ]; then
		echo "Working copy dirty..." >&2
		DIRTY='-DIRTY'
	fi

	if [ -z "$BRANCH" ]; then
		BRANCH='$Format:%d$'
		if [ "$(expr substr "$BRANCH" 1 1)" = '$' ]; then
			BRANCH="$(git rev-parse --abbrev-ref HEAD)"
		else
			# example BRANCH:
			#     "(HEAD, origin/heads/master, ccl/heads/master, master, refs/backup/master)"
			# or on newer Git:
			#     "(HEAD -> version-commit, tag: history/version-commit-006, afs/tags/history/version-commit-006, afs/backup/version-commit)"

			# find the first instance of a (lowercase) branch name
			echo "This commit is referenced by: $BRANCH" >&2
			BRANCH="$(echo " ${BRANCH}," | tr -d '\n()' | perl -pe 's|.*? ([^ A-Z/]+),.*|\1|')"
			if [ -z "$BRANCH" ]; then
				echo "Could not interpret branch name." >&2
				BRANCH=HEAD
			fi
		fi
	fi

	SOURCE="[${BRANCH}:$(expr substr "$COMMIT" 1 8)${DIRTY}]"
fi


detect_packages
./configure --strict --build-label "${SOURCE}" --build-date "${DATE}" --tcp-low-port 9000 --sge-parameter '-pe smp $cores' $PACKAGES_CONFIG "$@"


# vim: set sts=4 sw=4 ts=8 expandtab ft=sh:
