#!/bin/sh

#
# Global S2I variable setup
#
S2I_DESTINATION=${S2I_DESTINATION:-/tmp}
S2I_SOURCE_DIR="${S2I_DESTINATION}/src"
S2I_ARTIFACTS_DIR="${S2I_DESTINATION}/artifacts"
DEPLOYMENTS_DIR="/deployments"

if [ $RELEASE ]; then
  APP_VERSION=$RELEASE
else
  APP_VERSION=1.0.0.Alpha1
fi

if [ $MAVEN_SERVER ]; then
  REPO=$MAVEN_SERVER
else
  REPO=http://repository.jboss.org/nexus
fi

if [ -f "${S2I_SOURCE_DIR}/configuration/settings.xml" ]; then
  if [ -n "$MAVEN_MIRROR_URL" ]; then
    xml="<repository>\
           <id>mirror-maven</id>\
           <url>$MAVEN_MIRROR_URL</url>\
         </repository>"
    sed -i "s|<!-- ### repository ### -->|$xml|" ${S2I_SOURCE_DIR}/configuration/settings.xml

     xml="<pluginRepository>\
            <id>mirror-plugin-maven</id>\
            <url>$MAVEN_MIRROR_URL</url>\
          </pluginRepository>"
     sed -i "s|<!-- ### pluginrepository ### -->|$xml|" ${S2I_SOURCE_DIR}/configuration/settings.xml
  fi
  maven_env_args="${maven_env_args} -s ${S2I_SOURCE_DIR}/configuration/settings.xml"
  echo "Using custom maven settings from ${S2I_SOURCE_DIR}/configuration/settings.xml"
  cp ${S2I_SOURCE_DIR}/configuration/settings.xml $HOME/.m2/settings.xml
fi
echo "URL of the WildFly Swarm Uber jar : $REPO/service/local/artifact/maven/redirect?r=public&g=org.obsidiantoaster&a=generator-backend&v=$APP_VERSION&c=swarm"

curl -o /deployments/generator-swarm.jar -L "$REPO/service/local/artifact/maven/redirect?r=public&g=org.obsidiantoaster&a=generator-backend&v=$APP_VERSION&c=swarm"
