#!/bin/sh
#
# Copyright (c) 2014-2016 Linagora
#
# This program/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 (at your
# option) any later version.
#
# This program/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 program/library; If not, see http://www.gnu.org/licenses/
# for the GNU Lesser General Public License version 2.1.
#
#############################################################################
#
#                   Petals Registry Overlay Bootstrap Script
#
# Environment variables:
#
#  JAVA_HOME         Directory containing the JRE. Use the java executable
#                    found in $PATH if not defined.
#
#  JAVA_OPTS         Options for the JVM. Try "java -h" to get informations
#                    about the available options for your environment
#
#############################################################################

ABSOLUTE_PATH_SCRIPT=`readlink -f "$0"`
DIRNAME=`dirname "$ABSOLUTE_PATH_SCRIPT"`

# Set the path to the java executable
JAVA="$JAVA_HOME/bin/java"
[ -x "$JAVA" ] || JAVA="`which java`" || {
	echo "ERROR: java executable not found" >&2
	exit 1
}

PETALS_LIB_DIR="/usr/share/petals-registry"

# Set the path to petals bootstrap jar
PETALS_BOOTSTRAP_JAR_NAME="petals-registry-overlay.jar"
PETALS_JAR="$PETALS_LIB_DIR/$PETALS_BOOTSTRAP_JAR_NAME"
[ -f "$PETALS_JAR" ] || {
	echo "ERROR: $PETALS_BOOTSTRAP_JAR_NAME: JAR archive not found" >&2
	exit 1
}


# Adapt pathes if architecture is Cygwin
case "`uname`" in
	CYGWIN*)
		JAVA="`cygpath --unix "$JAVA"`"
		PETALS_LIB_DIR="`cygpath --dos "$PETALS_LIB_DIR"`"
		PETALS_JAR="`cygpath --unix "$PETALS_JAR"`"
		;;
esac

# Petals options
PETALS_JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.ow2.petals.registry_overlay.util.PetalsLogManager"

# Petals arguments
PETALS_ARGS=""
HELP_FLAG=""
MEMBER_PROPERTIES_URL=""
while getopts ':Hc:' OPT; do
   case "$OPT" in
   H) HELP_FLAG="yes"
      PETALS_ARGS="$PETALS_ARGS -h"
      ;;
   c) MEMBER_PROPERTIES_URL="$OPTARG"
      PETALS_ARGS="$PETALS_ARGS -c $MEMBER_PROPERTIES_URL"
      ;;
   *) PETALS_ARGS="$PETALS_ARGS $OPT $OPTARG"
      ;;
   esac
done

`echo "$@" | grep -qE "stop$"`
[ $? -ne 0 ] || {
      PETALS_ARGS="$PETALS_ARGS stop"
}

[ ! -z "$HELP_FLAG" ] || {
   # Retrieve the environment configuration
   if [ -z "$MEMBER_PROPERTIES_URL" ]; then
      echo "Error: You must specify a member configuration URL using '-c <url>'."
      exit 1
   fi
   LOCAL_MEMBER_PROPERTIES="/tmp/petals-registry-local-server.$$.properties"
   `curl -s $MEMBER_PROPERTIES_URL > $LOCAL_MEMBER_PROPERTIES`
   if [ $? -ne 0 ]; then
      echo "Error retrieving the local member configuration: $MEMBER_PROPERTIES_URL"
      exit 1;
   fi
   ENV_CFG_FILE=`sed "/^\#/d" $LOCAL_MEMBER_PROPERTIES | grep "petals.registry.overlay.member.environment.config.file" | cut -d "=" -f2- | sed "s/^[[:space:]]*//;s/[[:space:]]*$//"`
   rm -f $LOCAL_MEMBER_PROPERTIES >&2
   if [ -z "$ENV_CFG_FILE" ]; then
      . /etc/petals-registry/default-env.sh
   else
      case "$ENV_CFG_FILE" in
         /*) # Absolute environment configuration file
             . $ENV_CFG_FILE
             ;;
         *) # Relative environment configuration file
             FILE_SCHEME=`echo $MEMBER_PROPERTIES_URL | sed -e "s/^file:\/\/\(.*\)/\1/"`
             if [ -z $FILE_SCHEME ]; then
                 echo "Relative environment configuration file is authorized only for local url (ie. file://...)"
                 exit 1;
             else
                 . `dirname $FILE_SCHEME`/$ENV_CFG_FILE
             fi
             ;;
      esac
   fi
}

# Start
exec "$JAVA" -cp $PETALS_CLASSPATH:$PETALS_JAR $PETALS_JAVA_OPTS org.ow2.petals.registry_overlay.Main $PETALS_ARGS
