#!/bin/sh
#set -xv
# ---------------------------------------------------------------------------
# JOnAS: Java(TM) Open Application Server
# Copyright (C) 2006 Bull S.A.S.
# Contact: jonas-team@objectweb.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): France Telecom
# Contributor(s): Guillaume Sauthier
#
# ---------------------------------------------------------------------------
# $Id$
# ---------------------------------------------------------------------------

# Requires :
# --------------------
# * env JONAS_BASE
# * env JONAS_ROOT
# * env JAVA_HOME
# * env ANT_HOME


# Colors ...
R="\033[1;31m"
G="\033[1;32m"
E="\033[0m"

# Require non existant (and non empty) JONAS_BASE
if [ "$JONAS_BASE" = "" ]; then
    printf "$R JONAS_BASE needs to be set for $0 !$E\n"
    exit 1
fi
if [ -f $JONAS_BASE/conf/jonas.properties ]; then
    printf "$R $JONAS_BASE already exists !$E\n"
    printf "$R Please set JONAS_BASE to another directory.$E\n"
    exit 2
fi

# Require JONAS_ROOT
if [ ! -d $JONAS_ROOT ]; then
    printf "$R Please set JONAS_ROOT before using $0.$E\n"
    exit 3
fi

# Require JAVA_HOME
if [ "$JAVA_HOME" = "" ] || [ ! -d $JAVA_HOME ]; then
    printf "$R Please set JAVA_HOME before using $0.$E\n"
    exit 4
fi

# Require ANT_HOME
if [ "$ANT_HOME" = "" ] || [ ! -d $ANT_HOME ]; then
    printf "$R Please set ANT_HOME before using $0.$E\n"
    exit 5
fi



# Load the default environment
# Old style (deprecated)
if [ -x $JONAS_ROOT/env.sh ]; then
    . $JONAS_ROOT/env.sh;
fi
# Modern style
if [ -x $JONAS_ROOT/conf/env.sh.include ]; then
    . $JONAS_ROOT/conf/env.sh.include;
fi

# Directory creation if needed
if [ ! -d $JONAS_BASE ]; then
    mkdir -p $JONAS_BASE
fi

# JOnAS Base creation

# -----------------------------------------------------------------------
# Debugging properties
# ANT_OPTS="-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,address=4143,suspend=y"
# export ANT_OPTS
# -----------------------------------------------------------------------

ant -file $JONAS_ROOT/build-jb.xml

# Base update with env customization
# compatibility code :
if [ -z $JONAS_ROOT/env.start.sh ]; then
    cp $JONAS_ROOT/env.*.sh $JONAS_BASE
fi
cp $JONAS_ROOT/conf/env.*.sh.include $JONAS_BASE/conf

# Update env.sh script for JAVA_HOME and ANT_HOME
# ie Fix theses values for production use
sed -e "s#@VAR_HOME_SET@#true#g" \
    -e "s#\#JAVA_HOME=@JAVA_HOME@#    JAVA_HOME=$JAVA_HOME#g"\
    -e "s#\#ANT_HOME=@ANT_HOME@#    ANT_HOME=$ANT_HOME#g"\
    -e "s#\#export #    export #g"\
    $JONAS_ROOT/conf/env.sh.include > $JONAS_BASE/conf/env.sh.include

# Final Status
printf "* ---------------------------------------------------------------\n"
printf "* New JONAS_BASE\n"
printf "*\tJONAS_BASE  : $G$JONAS_BASE$E\n"
printf "*\tJONAS_ROOT  : $G$JONAS_ROOT$E\n"
printf "*\tJAVA_HOME   : $G$JAVA_HOME$E\n"
printf "*\tANT_HOME    : $G$ANT_HOME$E\n"
printf "* ---------------------------------------------------------------\n"
printf "*\n"
printf "* You can customize ${G}JONAS_BASE/conf/env*.sh.include$E scripts to fit your needs.\n"
printf "* Use $G$JONAS_ROOT/bin/unix/jonasctl$E to start/stop/admin/check your JOnAS instance.\n"
