@echo off

REM Solution: dca100020
REM DATE:     2022-06-07

cd %~dp0
set "IIS_RESET=%systemroot%\system32\iisreset.exe"
set "PATCH_LOG_FILE=patch.log"
set "PATCH_ERRLOG_FILE=patch_error.log"
IF EXIST "%PATCH_LOG_FILE%" DEL "%PATCH_LOG_FILE%"
IF EXIST "%PATCH_ERRLOG_FILE%" DEL "%PATCH_ERRLOG_FILE%"


REM Detect if (local)Administrator
OPENFILES >NUL 2>%PATCH_ERRLOG_FILE%
IF ERRORLEVEL 1 (
	echo ERROR: Logged-on user does not have administrative privilege.
	echo Run the CMD Shell as Administrator
	echo *** NOT COMPLETED ***
	PAUSE
	EXIT /B 1
)

REM IF NOT EXIST "%IIS_RESET%" (
REM 	echo ERROR: IIS server NOT exist!!
REM 	echo *** NOT COMPLETED ***
REM 	PAUSE
REM 	EXIT /B 1
REM )

echo Installing PATCHES: >%CD%\%PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

::Stop IIS server, so that dlls can be overwritten
echo Stop IIS Server...
iisreset /stop

::Start copying files

call:install_object BackupDCA34_dca100020,About.htm,web
call:install_object BackupDCA34_dca100020,UsersLoggedIn.htm,web\OnlineHelp\enduser
call:install_object BackupDCA34_dca100020,filesperapp.asp,web\Applications
call:install_object BackupDCA34_dca100020,SearchCriteria.asp,web\Search
call:install_object BackupDCA34_dca100020,ExpMO1.asp,web\ExportImportMappingRules
call:install_object BackupDCA34_dca100020,ExportObjDocumentation.asp,web\ExportObjDocumentation
call:install_object BackupDCA34_dca100020,GetDetails.asp,web\ExportImportMappingRules
call:install_object BackupDCA34_dca100020,MPRFDetails.asp,web\MigrationProfiles
call:install_object BackupDCA34_dca100020,ModuinContents.asp,web
call:install_object BackupDCA34_dca100020,RootMenu.asp,web
call:install_object BackupDCA34_dca100020,UsersLoggedin.asp,web\Users
call:install_object BackupDCA34_dca100020,simulate.asp,web\ConversionObjects
call:install_object BackupDCA34_dca100020,Exception.asp,web
call:install_object BackupDCA34_dca100020,expPatch.asp,web\ExportImportPatches
call:install_object BackupDCA34_dca100020,SCA22UserMgt.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20HandleMigProf.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20XMLGenerator.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20DDCdll.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20IDDLOADER.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20VerifyMapRules.dll,bin
call:install_object BackupDCA34_dca100020,BBCA20Convdll.dll,shared
call:install_object BackupDCA34_dca100020,BBCA20ScriptGenerator.dll,shared
call:install_object BackupDCA34_dca100020,enrich_db_with_xml.pyc,bin
call:install_object BackupDCA34_dca100020,ddimport_baan.pyc,py\Lib\dcapackage
call:install_object BackupDCA34_dca100020,sharedlibrary.pyc,py\Lib\dcapackage
call:install_object BackupDCA34_dca100020,SlmClient.exe,bin
call:install_object BackupDCA34_dca100020,iBCAUtilities20.exe,bin
call:install_object BackupDCA34_dca100020,BBCA20DBOptions.exe,bin
call:install_object BackupDCA34_dca100020,BBCA20Scheduler.exe,bin
call:install_object BackupDCA34_dca100020,bbca20recoding.exe,bin
call:install_object BackupDCA34_dca100020,BBCA20UserManager.exe,bin
call:install_object BackupDCA34_dca100020,SCA_Debugger.exe,bin






echo Start IIS Server...
iisreset /start  >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

call :check_empty "%PATCH_ERRLOG_FILE%"

Pause
goto:eof

:install_object
REM Par1: SolutionBackupName
REM Par2: Object
REM Par3: ObjectPath

set des=%~1
set object=%~2
set objectext=%~x2
set objectpath=%~3

echo Installing PATCH: %object% >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

REM unRegister first in case a .dll
IF "%objectext%"==".dll" regsvr32 /u /s "..\%objectpath%\%object%"  >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

IF NOT EXIST "..\%des%" md "..\%des%"   >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%
IF NOT EXIST "..\%des%\%objectpath%" md "..\%des%\%objectpath%"  >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

move "..\%objectpath%\%object%"  "..\%des%\%objectpath%\."  >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%
IF ERRORLEVEL 1 (
	echo WARNING: File was not found or could not be moved. >> %PATCH_LOG_FILE%
	echo move "..\%objectpath%\%object%" to "..\%des%\%objectpath%\."  >> %PATCH_LOG_FILE%
)

copy "%objectpath%\%object%" "..\%objectpath%\." >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

REM Register in case a .dll
IF "%objectext%"==".dll" regsvr32 /s "..\%objectpath%\%object%"  >> %PATCH_LOG_FILE% 2>%PATCH_ERRLOG_FILE%

REM Install py main
IF "%object%"=="py37_main.exe" (
  del /Q "..\%objectpath%\python*.*"  >nul
  del /Q "..\%objectpath%\vcruntime*.*" >nul
  del /S /Q "..\%objectpath%\DLLs\*.*" >nul
  del /S /Q "..\%objectpath%\Lib\*.*" >nul
  call "..\%objectpath%\py37_main.exe" -y >nul
  echo Y|del "..\%objectpath%\py37_main.exe"
)

goto:eof


:check_empty
if %~z1 == 0 (
echo Patch complete. System ready to use...
) ELSE (
echo Patch Errors found see: %~dp0%PATCH_ERRLOG_FILE%
)

goto:eof

:eof

