@echo off

rem ********************************************************************************
rem *
rem * uxspi.bat
rem *
rem * This file, which is copied to and run from the destination server, 
rem * updates device drivers and firmare through UpdateXpress System Pack
rem * during the final phase of the OS installation.
rem * 
rem *
rem * This batch file is called by from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.
rem *
rem *
rem ********************************************************************************
:UXSP
SET UXSP=%HOMEDRIVE%\uxsp
SET UXLOG=%UXSP%\uxsplog.txt
SET UXVAR=%UXSP%\uxspvars.bat
SET UXFIND=%UXSP%\uxspi.vbs
SET UXTMP=%UXSP%\uxtemp.bat
SET UXMAP=U:

echo "Updating device drivers and firmware through IBM UXSP. Please wait..." >>%UXLOG%
echo "Updating device drivers and firmware through IBM UXSP. Please wait..." 
%HOMEDRIVE%
cd %UXSP%
echo "calling %UXVAR%.."    >>%UXLOG%
call %UXVAR% >>%UXLOG%
echo "variables set."       >>%UXLOG%
rem * Sleep for 10 seconds
echo wscript.sleep 10000 > sleep.vbs
cscript /Nologo sleep.vbs
net use /delete %UXMAP% 1>nul 2>nul
echo Creating uxsp_d.reg registry entry for UXSP >>%UXLOG% 
echo REGEDIT4 									>  uxsp_d.reg
echo. 										>> uxsp_d.reg
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] 	>> uxsp_d.reg
echo "IBM SGSTK UXSPI"=- 			        			>> uxsp_d.reg
echo.	
echo "Unet use %UXMAP% %UXSPPATH% /USER:%LOGON% %PWD% " >>%UXLOG%
echo "Unet use %UXMAP% %UXSPPATH% /USER:%LOGON% %PWD% " 		        						>> uxsp_d.reg
net use %UXMAP% %UXSPPATH% /USER:%LOGON% %PWD% 1>nul 2>nul
if errorlevel 1 goto ERROR
echo "Searching setup*.exe in %UXMAP%..."               >>%UXLOG% 
call cscript %UXFIND% %UXMAP% %UXTMP%                   >>%UXLOG%
if errorlevel 1 goto ERROR
call %UXTMP%                                            >>%UXLOG%
if errorlevel 1 goto ERROR
echo "Found UXSPCMD=%UXSPCMD%"                          >> %UXLOG%
echo "Running UpdateXpress System Pack Installer..."    >> %UXLOG%
echo "Executing: %UXMAP%\%UXSPCMD% update -u -l %UXMAP%">> %UXLOG%  
if "%UXSPCMD%" == "" goto ERROR
rem * Sleep for 10 seconds
echo "Launching IBM UXSP in 10 seconds..."
cscript /Nologo sleep.vbs
call %UXMAP%\%UXSPCMD% update -u -l %UXMAP%             
if errorlevel 1 goto RTRY1
goto UXEND
:RTRY1
echo "Retrying IBM UXSP in 10 seconds..."
cscript /Nologo sleep.vbs
echo "Executing: %UXMAP%\%UXSPCMD% update -u -l %UXMAP%" >>%UXLOG%
call %UXMAP%\%UXSPCMD% update -u -l %UXMAP%         
if errorlevel 1 goto ERROR_UX
goto UXEND

:ERROR
echo "Toolkit batch script file uxspi.bat failed, check %UXLOG%"   >> %UXLOG%
rem * if uxsp.reg exist this is the first run, leave registry entry
if exist uxsp.reg goto UXSP_E
rem * else this is the second run, remove the registry entry
regedit /S uxsp_d.reg
goto UXSP_E
:REBOOT
del uxsp.reg 1>nul 2>nul
rem * Try a reboot
echo Set OperatingSytemSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true") > rbt_tk.vbs
echo for each OperatingSytem in OperatingSytemSet >> rbt_tk.vbs
echo      OperatingSytem.Reboot() >> rbt_tk.vbs
echo next  >> rbt_tk.vbs
cscript /Nologo rbt_tk.vbs
rem * If this failed finish script
goto UXSP_E


:ERROR_UX
echo "UpdateXpress System Pack Installer %UXMAP%\%UXSPCMD% encountered an error"   >> %UXLOG%
rem * if uxsp.reg exist this is the first run, leave registry entry
if exist uxsp.reg goto REBOOT2
rem * else this is the second run, remove the registry entry
regedit /S uxsp_d.reg
goto FINISH
:REBOOT2
del uxsp.reg 1>nul 2>nul
rem * Try a reboot
echo Set OperatingSytemSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true") > rbt_tk.vbs
echo for each OperatingSytem in OperatingSytemSet >> rbt_tk.vbs
echo      OperatingSytem.Reboot() >> rbt_tk.vbs
echo next  >> rbt_tk.vbs
cscript /Nologo rbt_tk.vbs
rem * If this failed finish script
goto FINISH

:UXEND
echo "UpdateXpress System Pack Installer %UXMAP%\%UXSPCMD% finish succesfully" >> %UXLOG%
rem * Remove registry entry
regedit /S uxsp_d.reg
goto FINISH

:FINISH
echo "Check C:\IBM_Support\uxsp.log on the target for more information" >> %UXLOG%
goto UXSP_E

:UXSP_E
del uxsp.reg 1>nul 2>nul
echo "deleting shared drive %UXMAP% ..."                >>%UXLOG%
net use /delete %UXMAP%


