#!/bin/sh
#
# $FreeBSD: ports/devel/maven-wrapper/files/mvn.sh.in,v 1.3 2012/11/17 05:55:59 svnexp Exp $

set -e

if [ -f "/usr/local/etc/maven/setenv.sh" ] ; then
	. "/usr/local/etc/maven/setenv.sh"
fi

if [ -z "${M2_HOME}" ] ; then
	INSTANCES_DIRECTORY="/usr/local/etc/maven-wrapper/instances.d"
	INSTANCE="`/usr/bin/find -s "${INSTANCES_DIRECTORY}" -type f -mindepth 1 -maxdepth 1 | /usr/bin/tail -n 1`"

	if [ -z "${INSTANCE}" ] ; then
		echo "could not find a Maven instance! (${INSTANCES_DIRECTORY})" >&2
		exit 1
	fi

	M2_HOME="`/bin/cat "${INSTANCE}"`"

	if [ ! -d "${M2_HOME}" ] ; then
		echo "not a directory: ${M2_HOME}" >&2
		exit 2
	fi
fi

export M2_HOME
exec "${M2_HOME}/bin/mvn" "$@"
