#!/bin/sh




# Check for Help command #

if [ $# -ge 1 ]; then
    case "$1" in
        "-?" | "-h" | "--help" | "-help" | "help" )
            echo
            echo "Usage: atlas-create-confluence-plugin-module [options]"
            echo
            echo "Creates a Confluence Plugin Module"
            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="5.0.18"
export ATLAS_VERSION
M2_HOME="${ATLAS_HOME}"/apache-maven-3.2.1
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="-Xmx768M -XX:MaxPermSize=256M $ATLAS_OPTS"
MVN_PARAMS="-gs ${M2_HOME}/conf/settings.xml"

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

# Execute Maven #

echo "Executing: ${MAVEN_EXECUTABLE} com.atlassian.maven.plugins:maven-confluence-plugin:5.0.18:create-plugin-module ${MVN_PARAMS}"
sh -c "${MAVEN_EXECUTABLE} com.atlassian.maven.plugins:maven-confluence-plugin:5.0.18:create-plugin-module ${MVN_PARAMS}"
