@Echo Off
Rem ---------------------------------------------------------------------------
Rem JOnAS: Java(TM) Open Application Server
Rem Copyright (C) 1999 Bull S.A.
Rem Contact: jonas-team@objectweb.org
Rem 
Rem This library is free software; you can redistribute it and/or
Rem modify it under the terms of the GNU Lesser General Public
Rem License as published by the Free Software Foundation; either
Rem version 2.1 of the License, or any later version.
Rem 
Rem This library is distributed in the hope that it will be useful,
Rem but WITHOUT ANY WARRANTY; without even the implied warranty of
Rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Rem Lesser General Public License for more details.
Rem 
Rem You should have received a copy of the GNU Lesser General Public
Rem License along with this library; if not, write to the Free Software
Rem Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
Rem USA
Rem
Rem Initial developer(s): Philippe Durieux
Rem Contributor(s): Miroslav Halas 
Rem                 Adriana Danes :
Rem                     - Change /config en /conf
Rem                     - Use JONAS_BASE
Rem ---------------------------------------------------------------------------
Rem $Id: jonas.bat,v 1.16.2.2 2003/12/17 08:44:55 durieuxp Exp $
Rem ---------------------------------------------------------------------------

Rem Keep variables local to this script
setlocal

Rem ---------------------------------------------
Rem set environment
Rem ---------------------------------------------
if [%JONAS_ROOT%]==[] goto setroot
call %JONAS_ROOT%\bin\nt\setenv.bat
call %JONAS_ROOT%\bin\nt\config_env.bat
set JONAS_LIB=%JONAS_ROOT%\lib

Rem include jonas classes
Set CLASSPATH=%JONAS_ROOT%\lib\common\ow_jonas_bootstrap.jar;%CLASSPATH%


Rem ---------------------------------------------
Rem set JAVA_OPTS 
Rem ---------------------------------------------
Rem JAVA_HOME must be  set since config_env.bat requires it for tools.jar
if [%JAVA_HOME%]==[] goto setjava

Rem JAVA_OPTS may be already partially initialized
set JAVA_OPTS=%JAVA_OPTS% -Dinstall.root=%JONAS_ROOT% 
set JAVA_OPTS=%JAVA_OPTS% -Djonas.base=%JONAS_BASE% 
set JAVA_OPTS=%JAVA_OPTS% -Djava.security.policy=%JONAS_ROOT%\conf\java.policy 
set JAVA_OPTS=%JAVA_OPTS% -Djonas.classpath=%XTRA_CLASSPATH% 
set JAVA_OPTS=%JAVA_OPTS% -Djonas.default.classloader=true 
set JAVA_OPTS=%JAVA_OPTS% -Djavax.rmi.CORBA.PortableRemoteObjectClass=org.objectweb.carol.rmi.multi.MultiPRODelegate 
set JAVA_OPTS=%JAVA_OPTS% -Djava.naming.factory.initial=org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory 
set JAVA_OPTS=%JAVA_OPTS% -Djava.security.auth.login.config=%JONAS_BASE%\conf\jaas.config
Rem ---------------------------------------------
Rem Get args
Rem ---------------------------------------------

if [%1]==[]   goto no_arg

set ARGS=
set MODE=
set JONASNAME=
set NAME_OPT=
set ANNOTATE=
set WHERE=background

:loop_on_args
if [%1]==[start]   goto start_arg
if [%1]==[stop]    goto stop_arg
if [%1]==[admin]   goto admin_arg
if [%1]==[version] goto version_arg
if [%1]==[check]   goto check_arg
if [%1]==[-fg]     goto fg_arg
if [%1]==[-bg]     goto bg_arg
if [%1]==[-win]    goto win_arg
if [%1]==[-n]      goto n_arg
if [%1]==[-cfgsvc] goto cfgsvc_arg
set ARGS=%ARGS% %1
goto next_arg

:cfgsvc_arg 
shift 
if [%1]==[] goto cfgsvc_usage 
set WHERE=cfgsvc 
set WRAPPER_CONF=%1 
goto start_arg 

:start_arg
set MODE=start
set CLASS_TO_RUN=org.objectweb.jonas.server.Server
echo JONAS_BASE is set to %JONAS_BASE%
goto next_arg

:stop_arg
set MODE=stop
set CLASS_TO_RUN=org.objectweb.jonas.adm.JonasAdmin -s
goto next_arg

:admin_arg
set MODE=admin
set CLASS_TO_RUN=org.objectweb.jonas.adm.JonasAdmin
echo JONAS_BASE is set to %JONAS_BASE%
goto next_arg

:version_arg
set MODE=version
set CLASS_TO_RUN=org.objectweb.jonas_lib.version.Version
goto next_arg

:check_arg
set MODE=check
set CLASS_TO_RUN=org.objectweb.jonas.tools.CheckEnv
echo JONAS_BASE is set to %JONAS_BASE%
goto next_arg

:fg_arg
set WHERE=foreground
goto next_arg

:bg_arg
set WHERE=background
goto next_arg

:win_arg
set WHERE=window
goto next_arg

:n_arg
shift
set JONASNAME=%1
set NAME_OPT=-n %JONASNAME%
set JAVA_OPTS=%JAVA_OPTS% -Djonas.name=%JONASNAME%
goto next_arg

:next_arg
shift
if not [%1]==[] goto loop_on_args

if [%MODE%]==[] goto no_mode

Rem ---------------------------------------------
Rem Remove RMI Annotation
Rem ---------------------------------------------
set JAVA_OPTS=%JAVA_OPTS% -Djava.rmi.server.RMIClassLoaderSpi=org.objectweb.jonas.server.RemoteClassLoaderSpi

Rem ---------------------------------------------
Rem Set tomcat/jetty base directory
Rem ---------------------------------------------
set SERVLET_OPTS=
if not [%TOMCAT_BASE%]==[] set SERVLET_OPTS=%SERVLET_OPTS% -Dtomcat.base=%TOMCAT_BASE%
if not [%CATALINA_HOME%]==[] set SERVLET_OPTS=%SERVLET_OPTS% -Dcatalina.home=%CATALINA_HOME%
if not [%CATALINA_BASE%]==[] set SERVLET_OPTS=%SERVLET_OPTS% -Dcatalina.base=%CATALINA_BASE%
if not [%JETTY_HOME%]==[] set SERVLET_OPTS=-Djetty.home=%JETTY_HOME%

Rem ---------------------------------------------
Rem Run java command
Rem ---------------------------------------------
set BOOT=org.objectweb.jonas.server.Bootstrap
if not [%MODE%]==[start] goto admin
if %WHERE%==cfgsvc goto cfgsvc_mode
if %WHERE%==foreground %JAVA% %JAVA_OPTS% %SERVLET_OPTS% %BOOT% %CLASS_TO_RUN%
if %WHERE%==background start /B %JAVA% %JAVA_OPTS% %SERVLET_OPTS% %BOOT% %CLASS_TO_RUN%
set WINDOW_TITLE="%HOSTNAME%:%JONASNAME%"
if %WINDOW_TITLE%==":" set WINDOW_TITLE="JOnAS Server"
if %WHERE%==window start %WINDOW_TITLE% %JAVA% %JAVA_OPTS% %SERVLET_OPTS% %BOOT% %CLASS_TO_RUN%
goto :EOF

:cfgsvc_mode 
rem called by ANT create_win32service 
set JAVA_CMD=%JAVA% -classpath %JONAS_ROOT%/lib/jonas.jar org.objectweb.jonas.tools.GenerateWrapperConf 
%JAVA_CMD% -d ; -i 2 wrapper.java.classpath %CLASSPATH% >%WRAPPER_CONF% 
%JAVA_CMD% wrapper.java.additional %JAVA_OPTS% %SERVLET_OPTS% >>%WRAPPER_CONF% 
echo wrapper.app.parameter.10=%JONASNAME% >>%WRAPPER_CONF% 
echo wrapper.ntservice.name=JOnAS_%JONASNAME% >>%WRAPPER_CONF% 
echo wrapper.ntservice.displayname=JOnAS (%JONASNAME%) >>%WRAPPER_CONF% 
goto :EOF 
  
:admin
%JAVA% %JAVA_OPTS% %SERVLET_OPTS% %BOOT% %CLASS_TO_RUN% %NAME_OPT% %ARGS%
goto :EOF


:setjava
echo JAVA_HOME not set.
goto :EOF

:no_mode
echo "No startup mode specified, specify start | stop | admin | version | check"
goto usage

:no_arg
echo No arguments specified.
goto usage

:usage
echo "jonas start | stop | admin | version | check | -fg | -bg | -win | -n | -cfgsvc | -cnhost | -cnport"
REM TODO: Add here explanation for the arguments
goto :EOF

:setroot
echo JONAS_ROOT not set.
goto :EOF

:cfgsvc_usage 
echo -cfgsvc option requires filename for target wrapper.conf file  
goto :EOF
