#!/bin/sh
localDir=`dirname "$0"`/..
if [ -n "${JAVA_HOME}"  ] ; then
   if [ -x "${JAVA_HOME}/bin/java" ] ; then
       JAVACMD="${JAVA_HOME}/bin/java"
   else
       echo "** ERREUR: impossible d'executer java sous ${JAVA_HOME}"
       exit 1
   fi
else
   JAVACMD=`which java 2> /dev/null `
   if [ -z "${JAVACMD}" ] ; then
       JAVACMD=java
   fi
fi

# Verification de la version de java (1.5.x) :
javaVersion=`${JAVACMD} -version 2>&1 | grep "java version" | grep -e "1\.[56]"`
if [ -z "${javaVersion}" ] ; then
    echo "** ERREUR: La version de java n'est pas 1.5.x ou 1.6.x"
    exit 1
fi

# Positionnement des options java :
JAVA_OPTIONS="${JAVA_OPTIONS} -Xms64m -Xmx1024m"
JAVA_OPTIONS="${JAVA_OPTIONS} -DBATCH_LOG4J_FOLDER=${localDir}/logs"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dlog4j.conf.file=${localDir}/conf/log4j.xml"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dpool.conf.file=${localDir}/conf/mysql.properties"
JAVA_OPTIONS="${JAVA_OPTIONS} -Dxemelios.resources.location=${localDir}/resources/"

JAVA_CP=
for f in `find ${localDir}/lib -name "*.jar"` ; do JAVA_CP=${JAVA_CP}:$f ; done

# Pour le debogage a distance :
#JAVA_OPTIONS="${JAVA_OPTIONS} -Xdebug"
#JAVA_OPTIONS="${JAVA_OPTIONS} -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y"

# Execution du batch :
${JAVACMD} ${JAVA_OPTIONS} -cp ${JAVA_CP} fr.gouv.finances.dgfip.xemelios.batch.BatchRunner ${localDir}/conf/batch-conf.xml $@
