:: Licensed Materials - Property of IBM
:: BI and PM: Mobile
:: (C) Copyright IBM Corp. 2007, 2013
:: US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

@echo off
rem ---------------------------------------------------------------------------
rem Shell script (Windows) to invoke jre and launch buildSettings tool.
rem
rem J_HOME is the location of the Java RE installation; if JAVA_HOME is
rem defined, use it.  Otherwise default to the local distribution.
rem J_OPTS are the Java options to be passed into the JAVA VM (optional)
rem ---------------------------------------------------------------------------
rem If the argument -java:{env | local} is present as first argument then the required JRE will be
rem loaded: -java:env will force the script to look for JRE using JAVA_HOME environment variable.
rem If not found then it will end by an error. The argument -java:local will force it to look
rem for JRE within the local distribution. If not found then it will end by an error.
rem Otherwise, the script will default by looking for a JRE using the JAVA_HOME environment
rem variable, then if cannot find one, it will try to use the local distribution.

rem Causes all changes made to environment variables to be local to this script
rem up to the point where endlocal is encountered.
setlocal

rem Change directories to where this script lives
CD /D %~dp0

rem ----- Verify and Set Required Environment Variables -----------------------
rem check if we need to run the local jre
rem Shift the first argument: -java:local
if "%1" == "-java:local" (shift & goto useLocal)

rem Try first the use of JAVA_HOME
goto useEnvVar

:useEnvVarOnly
rem JAVA_HOME should be defined first.
if "%JAVA_HOME%" == "" goto JavaHomeError
set J_HOME=%JAVA_HOME%
echo Looking for JRE in: %J_HOME%\bin... >&2
if exist "%J_HOME%\bin\java.exe" goto gotJava
set J_HOME=%JAVA_HOME%\jre
echo Looking for JRE in: %J_HOME%\jre\bin... >&2
if exist "%J_HOME%\bin\java.exe" goto gotJava


:JavaHomeError
echo You have specified the argument '-java:env'. >&2
echo However, the environment variable JAVA_HOME is not defined or incorrectly defined. >&2
echo Please define JAVA_HOME first before using the argument '-java:env'. >&2
goto finish


:useLocal
rem Using the local distribution either because -java:local was
rem specified or because the JAVA_HOME environment variable
rem is not defined or if the JAVA_HOME location doesn't have a JRE.
set J_HOME=jre\7.0
echo Looking for JRE in: %J_HOME%\bin... >&2
if exist "%J_HOME%\bin\java.exe" goto gotJava

rem Did not find a JRE in the local location, display error and exit.
goto error

:useEnvVar
rem Shift the first argument: -java:env
if "%1" == "-java:env" (shift & goto useEnvVarOnly)
rem Using the distribution referred by JAVA_HOME because
rem -java:local was NOT specified and the JAVA_HOME environment
rem variable is defined.
set J_HOME=%JAVA_HOME%
echo Looking for JRE in: %J_HOME%\bin... >&2
if exist "%J_HOME%\bin\java.exe" goto gotJava
set J_HOME=%JAVA_HOME%\jre
echo Looking for JRE in: %J_HOME%\bin... >&2
if exist "%J_HOME%\bin\java.exe" goto gotJava

rem Did not find a JRE in the environment variable's location.  If the
rem -java:env argument was not specified, try the local distribution.
if not "%1" == "-java:env" goto useLocal

:error
echo Could not find a JRE. Cannot run buildSettings.
goto finish

:gotJava

rem ----- Prepare Appropriate Java Execution Commands -------------------------


set _RUNJAVA="%J_HOME%\bin\java"


rem -----  Disabling DirectDraw. ----------------------------------------------
rem The next command checks if the DirectDraw will be disabled.
rem Disabling this DirectDraw might reduce the performance of the program that renders the image.
rem It is recommended to disable the DirectDraw in the case the ConfigTool and some desktop
rem icons flicker/flashes when started.
if "%1" == "-noddraw" set DD_OPTS=-Dsun.java2d.noddraw=true
if "%1" == "-noDDraw" set DD_OPTS=-Dsun.java2d.noddraw=true
if not "%DD_OPTS%" == "" echo Disabling DirectDraw...
set J_OPTS=%DD_OPTS% %J_OPTS%
rem ----- Set Up The Runtime Classpath ----------------------------------------
set CP=mobadmin.jar

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

%_RUNJAVA% -cp %CP% %J_OPTS% com.cognos.mobile.consoleclient.MobAdmin %*

:finish
endlocal