#!/bin/sh
# Copyright (c) 2013-2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Written by Mikolaj Izdebski <mizdebsk@redhat.com>

# Setup sane shell environment
\unset IFS
\unalias -a
unset -f command
path="${PATH}"
export PATH="$(command -p getconf PATH):${PATH}"
unset LANG CDPATH
export LC_ALL=POSIX
set -e

# Determine installation directory of XMvn
if [ -z "${M2_HOME}" ]; then
    case "${0}" in
        *[\\/]*)
            export M2_HOME=$(dirname "${0}")/..
            ;;
        *)
            IFS=:
            for dir in $path; do
                if [ -z "${dir}" ]; then
                    dir=.
                fi
                if [ -r "${dir}/${0}" ]; then
                    export M2_HOME="${dir}"/..
                    unset IFS
                    break
                fi
            done

            echo "$0: Unable to find XMvn installation directory." >&2
            exit 1
            ;;
    esac
fi

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