@echo off

REM ---------------------------------------------------------------------------
REM  Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
REM
REM  Licensed under the Apache License, Version 2.0 (the "License");
REM  you may not use this file except in compliance with the License.
REM  You may obtain a copy of the License at
REM
REM      http://www.apache.org/licenses/LICENSE-2.0  
REM
REM  Unless required by applicable law or agreed to in writing, software
REM  distributed under the License is distributed on an "AS IS" BASIS,
REM  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM  See the License for the specific language governing permissions and
REM  limitations under the License.

rem ---------------------------------------------------------------------------
rem Main Script for the WSO2 Web Services Application Server v1.1
rem
rem Environment Variable Prequisites
rem
rem   WSO2IS_HOME   Home of WSO2 Identity Solution installation. 
rem		    If not set I will  try to figure it out.
rem                 
rem   JAVA_HOME       Must point at your Java Development Kit installation.
rem
rem   JAVA_OPTS       (Optional) Java runtime options used when the commands
rem                   is executed. 
rem ---------------------------------------------------------------------------

rem ----- if JAVA_HOME is not set we're not happy ------------------------------
:checkJava

if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
goto checkServer

:noJavaHome
echo "You must set the JAVA_HOME variable before running WSO2 WSAS."
goto end

rem ----- Only set WSO2IS_HOME if not already set ----------------------------
:checkServer

pushd..
if "%WSO2IS_HOME%"=="" set WSO2IS_HOME=%CD%
popd

rem find WSO2IS_HOME if it does not exist due to either an invalid value passed
rem by the user or the %0 problem on Windows 9x
rem if not exist "%WSO2IS_HOME%\bin\version.txt" goto noServerHome

set AXIS2_HOME=%WSO2IS_HOME%
goto updateClasspath

:noServerHome
echo %WSO2IS_HOME% is set incorrectly or WSO2 WSAS could not be located. Please set WSO2IS_HOME.
goto end

rem ----- update classpath -----------------------------------------------------
:updateClasspath

setlocal EnableDelayedExpansion
set WSO2IS_CLASSPATH=%WSO2IS_HOME%\conf
FOR %%C in (%WSO2IS_HOME%\lib\*.jar) DO set WSO2IS_CLASSPATH=!WSO2IS_CLASSPATH!;./lib/%%~nC%%~xC

set WSO2IS_CLASSPATH="%JAVA_HOME%\lib\tools.jar";%WSO2IS_CLASSPATH%;%CLASSPATH%

rem ----- Process the input command -------------------------------------------

rem Slurp the command line arguments. This loop allows for an unlimited number
rem of arguments (up to the command line limit, anyway).

set CMD=%*

:setupArgs
if ""%1""=="""" goto doneStart

if ""%1""==""help""     goto getHelp
if ""%1""==""-help""    goto getHelp
if ""%1""==""--help""   goto getHelp
if ""%1""==""?""        goto getHelp
if ""%1""==""-?""       goto getHelp

shift
goto setupArgs

rem ----- getHelp --------------------------------------------------------------
:getHelp
  echo Usage: wso2wsas.bat ( commands ... )
  echo commands:
  echo   --start        Start WSO2 WSAS as a background process
  echo   --run          Run WSO2 WSAS
  echo   --restart      Restart WSO2 WSAS
  echo   --stop         Stop WSO2 WSAS
  echo   --debug        Start WSO2 WSAS in remote debugging mode. You need to specify the debugging port after this argument.
  echo   --version      What version of WSO2 WSAS are you running?
goto end

rem ----- doneStart ------------------------------------------------------------
rem This label provides a place for the argument list loop to break out
rem and for NT handling to skip to.

:doneStart
if "%OS%"=="Windows_NT" @setlocal
if "%OS%"=="WINNT" @setlocal

rem ----------------- Execute The Requested Command ----------------------------

:runServer
set WSO2IS_CLASSPATH=%WSO2IS_HOME%\conf;%WSO2IS_CLASSPATH%

cd %WSO2IS_HOME%
echo  Using WSO2IS_HOME:   %WSO2IS_HOME%
echo  Using JAVA_HOME:       %JAVA_HOME%

set JAVA_ENDORSED = "%WSO2IS_HOME%\lib\endorsed";"%JAVA_HOME%\jre\lib\endorsed";"%JAVA_HOME%\lib\endorsed"

if ""%1""==""-start""   goto startNewPrompt
if ""%1""==""--start""   goto startNewPrompt
if ""%1""==""start""   goto startNewPrompt

"%WSO2IS_HOME%\lib\wso2wsas\bin\wso2wsas" %CMD%

:end
endlocal

:startNewPrompt
start "Identity Application Server" "%WSO2IS_HOME%\lib\wso2wsas\bin\wso2wsas" %CMD%
endlocal


