#!/bin/sh
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 2012 Bull S.A.S.
# Contact: jonas-team@ow2.org
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
# Initial developer(s): Mohammed Boukada
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# ---------------------------------------------
# Generate an addon of an application using
# PackagingManager JOnAS service
# ---------------------------------------------

echo "[INFO] ------------------------------------------------------------------------"
echo "[INFO] Application addon generation ... "
echo "[INFO] ------------------------------------------------------------------------"

# ---------------------------------------------
# Packaging maven plugin artifact
# ---------------------------------------------
groupId=org.ow2.jonas.tools.maven;
artifactId=maven-packaging-plugin;
version=5.3.0-M7-SNAPSHOT;
goal=generate-application-addon;

# ---------------------------------------------
# Command usage message
# ---------------------------------------------
USAGE="\n[INFO] Usage: $0 -app urlCloudApplication -out urlOutputDir"
USAGE=$USAGE"\n""[INFO] Options:"
USAGE=$USAGE"\n""[INFO]   -tid 	\t tenant identifier value"
USAGE=$USAGE"\n""[INFO]   -env	\t url of environment-template.xml"
USAGE=$USAGE"\n""[INFO]   -map 	\t url of deployment.xml (mapping topology)\n"

# ---------------------------------------------
# Check argument
# ---------------------------------------------
checkArg() {
startChar=`echo $2 | cut -c1`
if [ -z $2 ] || [ $startChar = "-" ]
then
	echo "[ERROR] Bad argument '$2' for '$1'"
	echo $USAGE
	exit 1
fi
}

# ---------------------------------------------
# Get arguments
# ---------------------------------------------
URL_CLOUD_APP=
URL_ENV_TEMP=
URL_MAP_TOP=
TENANT_ID=
OUTPUT_DIR=
while [ "$#" -gt 0 ]
  do case "$1" in
	-app)
	  PARAMS="$PARAMS $1"
      	  shift
      	  URL_CLOUD_APP=$1
	  checkArg '-app' $1 
	  shift
          ;;
	-env)
	  PARAMS="$PARAMS $1"
      	  shift
      	  URL_ENV_TEMP=$1
	  checkArg '-env' $1
	  shift
          ;;
	-map)
	  PARAMS="$PARAMS $1"
      	  shift
      	  URL_MAP_TOP=$1
	  checkArg '-map' $1
	  shift
          ;;
	-tid)
	  PARAMS="$PARAMS $1"
      	  shift
      	  TENANT_ID=$1
	  checkArg '-tid' $1
	  echo "[INFO] Tenant id : $TENANT_ID"
	  shift
          ;;
	-out)
	  PARAMS="$PARAMS $1"
      	  shift
      	  OUTPUT_DIR=$1
	  checkArg '-out' $1
	  shift
          ;;
	*)
	  echo "[ERROR] Unrecognized command : '$*'"
	  echo $USAGE
   	  exit 1
	  ;;
  esac
done



# ---------------------------------------------
# Check script args
# ---------------------------------------------
if [ -z $URL_CLOUD_APP ]
then
	echo "[ERROR] Missing argument '-app urlCloudApplication'"
	echo $USAGE
	exit 1
else 
	echo "[INFO] Cloud application url : $URL_CLOUD_APP"
fi

if [ -z $OUTPUT_DIR ]
then
	echo "[ERROR] Missing argument '-out urlOutputDirectory'"
	echo $USAGE
	exit 1
else
	echo "[INFO] Output directory : $OUTPUT_DIR"
fi

if [ ! -z $URL_ENV_TEMP ] && [ -z $URL_MAP_TOP ] 
then
	echo "[WARNING] -env and -map arguments must be set together"
	echo "[WARNING] '-env' argument is ignored"
	echo $USAGE
	unset $URL_ENV_TEMP
elif [ -z $URL_ENV_TEMP ] && [ ! -z $URL_MAP_TOP ]
then
	echo "[WARNING] -env and -map arguments must be set together"
	echo "[WARNING] '-map' argument is ignored"
	echo $USAGE
	unset $URL_MAP_TOP
elif [ ! -z $URL_ENV_TEMP ] && [ ! -z $URL_MAP_TOP ]
then
	echo "[INFO] Environment template url : $URL_ENV_TEMP"
	echo "[INFO] Mapping topology url : $URL_MAP_TOP"
fi

# ---------------------------------------------
# Run maven plugin
# ---------------------------------------------
mvn $groupId:$artifactId:$version:$goal -DurlCloudApplication=$URL_CLOUD_APP -DoutPutDirectory=$OUTPUT_DIR -Dtenant-id=$TENANT_ID -DurlEnvironmentTemplate=$URL_ENV_TEMP -DurlMappingTopology=$URL_MAP_TOP;