@echo off

rem setlocal

set javaapp=com.dell.suu.app.SUUMain
set homedir=%~dp0
set appname=DellSUU
set appcommand=%~nx0
set OS=i386
IF NOT EXIST "%homedir%java\windows\%OS%" (
set OS=x86_64
)
set javapath=%homedir%java\windows\%OS%\bin
set suupath=%homedir%bin\windows
set logging=-Djava.util.logging.config.file=suuapp.properties
set customization_location=C:\dell\suu\customization;C:\dell\suu\customization\etc;C:\dell\suu\customization\debranded_images.jar
path=%javapath%;%suupath%;%path%
set CLASSPATH=%customization_location%;%homedir%;%homedir%etc;%homedir%etc/guiproperties;%homedir%lib/DellSUU.jar;%homedir%lib/xercesImpl.jar;%homedir%lib/xml-apis.jar;%homedir%lib/OMCF.jar;%homedir%lib/images.jar

REM Delete(remove) Property File
if exist C:\dell\suu\PMInfo.properties (
    del /f /q C:\dell\suu\PMInfo.properties
    )

REM Delete(remove) old Logo and images files including all sub-directories
if exist C:\dell\suu\customization (
    del /f /q /s C:\dell\suu\customization\*.*
    )

REM delete log file "pmsuu.log"
if exist C:\pmsuu.log (
    del /f /q C:\pmsuu.log
    )
	
REM delete error log file "pmsuu.err"
if exist C:\pmsuu.err (
    del /f /q C:\pmsuu.err
    )
	
mkdir C:\dell\suu 2>C:\pmsuu.err

REM SUUPMDataCollector 0x06 C:\dell\suu\PMInfo.properties 1>C:\pmsuu.log 2>C:\pmsuu.err


REM DISPLAY WELCOME MESSAGE
REM echo ------------------------------------------------
REM echo Welcome to the Dell OpenManage Server Update Utility.
REM echo Copyright (c) 2003-2011 Dell Inc. All Rights Reserved.
REM more +1 "%homedir%"etc\build-version.txt


if y%1_y==y_y set argsgetOS=0
if not y%1_y==y_y set argsgetOS=1

set arg1=_a1
set arg2=_a2
set arg3=_a3
if not p%1_p==p_p set arg1=%1
if not p%2_p==p_p set arg2=%2
if not p%3_p==p_p set arg3=%3

set args=%1

shift
:getargs
if x%1_x==x_x goto checkOS
set args=%args% %1
shift
goto getargs

:checkOS
pushd %homedir%

if %argsgetOS%==0 "%suupath%"\getOS.exe -suubatch
if %argsgetOS%==1 "%suupath%"\getOS.exe %args%


set isCore=%errorlevel%

if %isCore%==2 goto :showhelp
REM if %isCore%==3 goto :checklocaladmin
if %isCore%==0 goto :suu

:showhelp
java -DdebugEnabled=false -Dapp.name=%appname% -Dapp.home="%homedir%.\\" -Dapp.cmd=%appcommand% %logging% %javaapp%
goto :end

REM :checklocaladmin
REM set args=%args% -uacon

:suu
rem removed all the conditions to call run/runapp. 
rem runapp checks for networkpath then makes the call accordingly.  
goto runapp

:runapp
	"%suupath%"\NetWorkFinder.exe "%homedir%"
	set isnetworkpath=%errorlevel%

	if %isnetworkpath%==0 goto run
	if %isnetworkpath%==1 goto neterr
	if %isnetworkpath%==2 goto netrun
	if %isnetworkpath%==3 goto net_temp

:neterr
	echo Error : Not enough disk space to run SUU!
	echo You should have at least 50 MB of space available to run SUU.
	goto end

:net_temp
	goto end

:netrun
	"%suupath%"\CryptoSecureRNG.exe
	set randomNum=%errorlevel%
	if %randomNum%==0 goto temperr
	set stemp=%temp%\%randomNum%
	goto drun

:temperr
	echo ERROR: Couldn't create temporary directory for copying SUU files.
	echo Exiting the application....
	goto end

:drun
	mkdir %stemp%
	attrib +r +h %stemp%
	echo ------------------------------------------------
	echo A temporary directory has been created as %stemp%
	echo Copying SUU files into the temporary directory...

	copy /v /z "%homedir%"java\windows\%OS%\suu_jre.zip %stemp%
	copy /v /z "%homedir%"bin\Windows\suu_jar.zip %stemp%
	copy /v /z "%homedir%"bin\Windows\BundleApp.exe %stemp%
	copy /v /z "%homedir%"bin\Windows\suu_pm.zip %stemp%
	echo ------------------------------------------------
	echo Extracting files to run SUU...
	"%suupath%\unzip.exe" -q %stemp%\suu_jre.zip -d %stemp%\
	"%suupath%\unzip.exe" -q %stemp%\suu_jar.zip -d %stemp%\ 
	echo Extracting pm 
	"%suupath%\unzip.exe" -q %stemp%\suu_pm.zip -d %stemp%
	pushd %stemp%
	SUUPMDataCollector 0x06 C:\dell\suu\PMInfo.properties 1>C:\pmsuu.log 2>C:\pmsuu.err
	popd
	del %stemp%\suu_jre.zip
	del %stemp%\suu_jar.zip
	
	attrib +r +h %stemp%/*.* /s /d
	echo Preparing SUU to Launch...
	set javapath=%stemp%\java\windows\%OS%\bin
	set path=%javapath%;%path%
	set CLASSPATH=%customization_location%;%homedir%;%homedir%etc;%homedir%etc/guiproperties;%stemp%\DellSUU.jar;%stemp%\xercesImpl.jar;%stemp%\xml-apis.jar;%stemp%\OMCF.jar;%stemp%\images.jar
	%javapath%\java -DdebugEnabled=false -Dapp.name=%appname% -Dapp.home="%homedir%.\\" -Dapp.cmd=%appcommand% -Dapp.isnetworkpath=%isnetworkpath% -Dapp.bundleAppTempPath=%stemp%  %logging% %javaapp% %args% 
	set suuerrorlevel=%errorlevel%

	REM Introducing a sleep of 5 sec. Required for Win2KSp4 OS. Refer DF127421.
	ping 127.0.0.1 -n 1 -w 5000 > NUL
		
	rmdir /s /q %stemp%
	goto end

:run	
	SUUPMDataCollector 0x06 C:\dell\suu\PMInfo.properties 1>C:\pmsuu.log 2>C:\pmsuu.err
	java -DdebugEnabled=false -Dapp.name=%appname% -Dapp.home="%homedir%.\\" -Dapp.cmd=%appcommand% %logging% %javaapp% %args%
	set suuerrorlevel=%errorlevel%
	
:end
	EXIT /B %suuerrorlevel%
	popd	
	endlocal