#!/bin/sh





# Check for Help command #

if [ $# -ge 1 ]; then
    case "$1" in
        "-?" | "-h" | "--help" | "-help" | "help" )
            echo
            echo "Usage: atlas-mvn [options]"
            echo
            echo "Execute arbitrary Maven commands using the version of Maven bundled with the Atlassian Plugin SDK."
            echo
                                                exit;;
    esac
fi

# Determine the location of the script #

# resolve symbolic links
PRG="${0}"

while [ -h "${PRG}" ] ; do
  ls=`ls -ld "${PRG}"`
  link=`expr "${ls}" : '.*-> \(.*\)$'`
  if expr "${link}" : '/.*' > /dev/null; then
    PRG="${link}"
  else
    PRG=`dirname "${PRG}"`/"${link}"
  fi
done

PRGDIR=`dirname "${PRG}"`

# Identify Maven location relative to script #

ATLAS_HOME=`cd "${PRGDIR}" && pwd -P`
ATLAS_HOME=`dirname "${ATLAS_HOME}"`
export ATLAS_HOME
ATLAS_VERSION="9.9.1"
export ATLAS_VERSION
AMPS_PLUGIN_VERSION="9.9.1"
export AMPS_PLUGIN_VERSION
M2_HOME="${ATLAS_HOME}"/apache-maven-3.9.8
MAVEN_EXECUTABLE="${M2_HOME}"/bin/mvn

if [ -n "${ATLAS_MVN}" ]; then
    MAVEN_EXECUTABLE="${ATLAS_MVN}"
fi



# Check that target executable exists
if [ ! -x "${MAVEN_EXECUTABLE}" ]; then
  echo "Cannot find ${MAVEN_EXECUTABLE}"
  echo "This file is needed to run this program"
  exit 1
fi

# Transform Parameters into Maven Parameters #

export MAVEN_OPTS="-Xms512M -Xmx768M $ATLAS_OPTS"
MVN_PARAMS="-gs ${M2_HOME}/conf/settings.xml"

# get the AMPS Version to use from the POM if the POM exists #

NEW_PLUGIN_REGEX="(bamboo|bitbucket|confluence|crowd|fecru|jira|refapp|amps)-maven-plugin"
OLD_PLUGIN_REGEX="maven-(bamboo|confluence|crowd|fecru|jira|stash|refapp|amps)-plugin"
PLUGIN_RESOLVE_REGEX="com\.atlassian\.maven\.plugins:.*((${NEW_PLUGIN_REGEX})|(${OLD_PLUGIN_REGEX}))"
RESOLVED_AMPS_PLUGINS=""

while [ $# -gt 0 ]
do
    case "$1" in
                        *)
                        MVN_PARAMS="${MVN_PARAMS} ${1}"
            shift 1;;     esac
done

case $AMPS_PLUGIN_VERSION in
    4.*|5.*|6.*|7.*)
        MVN_PLUGIN="maven-amps-dispatcher-plugin"
        break
        ;;
    *)
        MVN_PLUGIN="amps-dispatcher-maven-plugin"
        ;;
esac

# Check for conflicts between AMPS version and plugin artifact ID in plugin pom file #
#                                                                                    #
# AMPS Version | Artifact Name       | Error                                         #
# -------------+---------------------+-------------------------                      #
#  >= 8.0      | <PROD>-maven-plugin | No error                                      #
#  < 8.0       | maven-<PROD>-plugin | No error                                      #
#  < 8.0       | <PROD>-maven-plugin | Error (*)                                     #
#  >= 8.0      | maven-<PROD>-plugin | Error                                         #
#                                                                                    #
#  (*) except bitbucket-maven-plugin                                                 #

printErrorsAndExit ()
{
    echo "[ERROR] Invalid of Atlassian maven plugin(s) detected for AMPS Version $2: $1"
    echo "[ERROR] Try using <amps.version>$3</amps.version> in the project pom.xml"
    echo "[ERROR] Visit https://ecosystem.atlassian.net/browse/ATLASSDK-147 for more information"
    exit 1
}


export MVN_PLUGIN
MVN_COMMAND=""

# Execute Maven #

echo "Executing: ${MAVEN_EXECUTABLE} ${MVN_COMMAND} ${MVN_PARAMS}"
sh -c "${MAVEN_EXECUTABLE} ${MVN_COMMAND} ${MVN_PARAMS}"
