@ECHO OFF
rem
rem
rem DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
rem
rem Copyright 2008 Sun Microsystems, Inc. All rights reserved.
rem
rem The contents of this file are subject to the terms of either the GNU
rem General Public License Version 2 only ("GPL") or the Common Development
rem and Distribution License("CDDL") (collectively, the "License").  You
rem may not use this file except in compliance with the License. You can obtain
rem a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.htm
rem or updatetool/LICENSE.txt.  See the License for the specific
rem language governing permissions and limitations under the License.
rem
rem When distributing the software, include this License Header Notice in each
rem file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
rem Sun designates this particular file as subject to the "Classpath" exception
rem as provided by Sun in the GPL Version 2 section of the License file that
rem accompanied this code.  If applicable, add the following below the License
rem Header, with the fields enclosed by brackets [] replaced by your own
rem identifying information: "Portions Copyrighted [year]
rem [name of copyright owner]"
rem
rem Contributor(s):
rem
rem If you wish your version of this file to be governed by only the CDDL or
rem only the GPL Version 2, indicate your decision by adding "[Contributor]
rem elects to include this software in this distribution under the [CDDL or GPL
rem Version 2] license."  If you don't indicate a single choice of license, a
rem recipient has the option to distribute your version of this file under
rem either the CDDL, the GPL Version 2 or to extend the choice of license to
rem its licensees as provided above.  However, if you add GPL Version 2 code
rem and therefore, elected the GPL Version 2 license, then the option applies
rem only if the new code is made subject to such option by the copyright
rem holder.
rem
setlocal
set MY_HOME=%~dp0

rem We want to set MY_NAME to the name of this file. %~0 may include 
rem the portion of the path used to invoke the command, for example
rem ".\foo.bat". We need to trim the leading stuff to get just "foo.bat".
set MY_PATH=%~0
:loop2
for /f "tokens=1 delims=\" %%A in ("%MY_PATH%") do set MY_NAME=%%A
for /f "tokens=1* delims=\" %%A in ("%MY_PATH%") do set MY_PATH=%%B
if defined MY_PATH goto loop2
rem MY_NAME may be "foo" or "foo.bat" depending on what the user typed.
rem normalize it to "foo.bat"
if %MY_NAME%==updatetool set MY_NAME=updatetool.bat
if %MY_NAME%==pkg set MY_NAME=pkg.bat
rem MY_NAME is now set correctly

rem Location of bootstrap jar file relative to INSTALL_HOME
set BOOTSTRAPJAR=updatetool\lib\ucbootstrap.jar
set BOOTSTRAPPROPS=%TEMP%\uc%RANDOM%.props

rem Go find Java
set MY_JAVA_HOME=""
set JDK_KEY=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
set JRE_KEY=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment

rem Get Java runtime location from the registry. We try 1.5 first, then 1.6
(FOR /F "tokens=3* delims=	" %%A IN ('reg query "%JDK_KEY%\1.5" /v JavaHome') DO SET MY_JAVA_HOME=%%A) 2>nul
(if not exist "%MY_JAVA_HOME%\bin" FOR /F "tokens=3* delims=	" %%A IN ('reg query "%JRE_KEY%\1.5" /v JavaHome') DO SET MY_JAVA_HOME=%%A) 2>nul
(if not exist "%MY_JAVA_HOME%\bin" FOR /F "tokens=3* delims=	" %%A IN ('reg query "%JDK_KEY%\1.6" /v JavaHome') DO SET MY_JAVA_HOME=%%A) 2>nul
(if not exist "%MY_JAVA_HOME%\bin" FOR /F "tokens=3* delims=	" %%A IN ('reg query "%JRE_KEY%\1.6" /v JavaHome') DO SET MY_JAVA_HOME=%%A) 2>nul

rem Could not get from registry. See if JAVA_HOME is set
if not exist "%MY_JAVA_HOME%\bin" set MY_JAVA_HOME=%JAVA_HOME%

rem Can't find a Java runtime. Error.
if not exist "%MY_JAVA_HOME%\bin" goto nojavaerror

rem Go get proxy information from registry
(FOR /F "tokens=3* delims=	" %%A IN ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer') DO SET MY_PROXY_SERVER=%%A ) 2>nul
(FOR /F "tokens=3* delims=	" %%A IN ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable') DO SET MY_PROXY_ENABLE=%%A ) 2>nul

echo.
echo The software needed for this command (%MY_NAME%) is not installed.
echo Press any key to install the necessary software, or Ctrl-C to quit.
echo After software installation is completed you may re-run this command.
echo.
pause
echo.

rem ucbootstrap takes it's input as a java property file, so we must
rem write props out to a temporary java props file.
echo # Update Center Bootstrap properties  > %BOOTSTRAPPROPS%
echo install.pkg=true>> %BOOTSTRAPPROPS%
if %MY_PROXY_ENABLE%==0x1 echo proxy.URL=http://%MY_PROXY_SERVER%>> %BOOTSTRAPPROPS%
if %MY_NAME%==updatetool.bat echo install.updatetool=true>> %BOOTSTRAPPROPS%

rem Need to save the image path. This is pretty goofy.  
rem Java property files use \ as an escape character. Therefore we need to 
rem convert all \'s in the path to the escaped version ("\\").  On Unix there 
rem would be a dozen ways to do this. In a bat script all I could figure out 
rem is this. The first "for" breaks out the head of the path. We echo that to 
rem the property file. The second "for" returns the remainder of the path.
rem We then loop and process it. We continue until there is no remainder 
rem of the path. Since there is no option to echo to suppress the trailing 
rem newline, each part of the path is on its own line. Therefore we must
rem  add the line  continuation character at the end of each line. 
rem That's why there are three back slashes at the end of each line. 
rem Confused? Me too. 
set MY_IMAGE_PATH=%MY_HOME%\..
echo image.path=\>> %BOOTSTRAPPROPS%
:loop1
for /f "tokens=1 delims=\" %%A in ("%MY_IMAGE_PATH%") do set PATHPART=%%A
echo %PATHPART%\\\>> %BOOTSTRAPPROPS%
for /f "tokens=1* delims=\" %%A in ("%MY_IMAGE_PATH%") do set MY_IMAGE_PATH=%%B
if defined MY_IMAGE_PATH goto loop1

echo. >> %BOOTSTRAPPROPS%

rem Remove Unix bootstub scripts
(if %MY_NAME%==updatetool.bat del /F "%MY_HOME%\updatetool") 2>nul
(if %MY_NAME%==pkg.bat del /F "%MY_HOME%\pkg") 2>nul

rem echo %BOOTSTRAPPROPS%
rem echo "%MY_JAVA_HOME%"

rem Finally, run ucbootstrap!
"%MY_JAVA_HOME%\bin\java" -jar "%MY_HOME%\..\%BOOTSTRAPJAR%" %BOOTSTRAPPROPS%

rem Since the above bootstrap replaces this bat file, it looks like no command below
rem here ever gets run. Oh well, for now that means we leave a cruft temp file

rem Clean up temp file
del %BOOTSTRAPPROPS%

goto end

:nojavaerror
echo Failed to locate a Java runtime.
echo Please install Java SE 5 or Java SE 6.
echo If you already have one of those installed then set the JAVA_HOME 
echo environment variable to the Java SE install location.
goto end

:end
endlocal
