rem For setup run command: setup DEBS_PATH XXXX
rem where DEBS_PATH - install path for software
rem       XXXX - 4 letter SFR code of PL
rem Take user for run services "DE BlackSpider delivery agent" and 
rem "DE BlackSpider load agent" from SFR KR 

@echo off

call environment.bat

chcp 1251
title Setup Client DE Black Spider Starter Kit
set DEBSCU_CODE=%1
set DEBS_PATH=%2 %3 %4 %5 %6 %7 %8 %9

IF NOT EXIST "%DEBS_PATH%" mkdir "%DEBS_PATH%"

PUSHD "%DEBS_PATH%"
IF NOT EXIST LOG mkdir LOG
for /F "tokens=*" %%I in ('cd') do set DEBS_PATH=%%~fsI
POPD

rem Find DE-BS previous installation path 
utils\reg query "HKLM\Software\Digital Empire\BlackSpider Client" /S| %SystemRoot%\system32\find "%DEBS_PATH%" > "%DEBS_PATH%\log\reg.out"

rem --Extract paths of installed KU's and add space to end of paths:

for /F "tokens=3*" %%I in ('type "%DEBS_PATH%\log\reg.out"') do echo %%I >> "%DEBS_PATH%\log\reg2.out"

IF EXIST %DEBS_PATH%\log\reg2.out for /F "skip=2 tokens=*" %%I in ('%SystemRoot%\system32\find "%DEBS_PATH% " "%DEBS_PATH%\log\reg2.out"') do set DEBS_PATH_PREVIOUS=%%I

IF DEFINED DEBS_PATH_PREVIOUS  GOTO :L2


rem utils\reg add "HKLM\Software\Digital Empire\BlackSpider Client\Version=3.0"
utils\reg add "HKLM\Software\Digital Empire\BlackSpider Client\%DEBSCU_CODE%=%DEBS_PATH%"


copy bank %DEBS_PATH%\bank

xcopy ARM %DEBS_PATH%\arm\ /E /V /C /Y
xcopy AGENTS %DEBS_PATH%\AGENTS\ /E /V /C /Y
xcopy LOG %DEBS_PATH%\LOG\ /E /V /C /Y
xcopy DOC %DEBS_PATH%\DOC\ /E /V /C /Y

rem .bat and .ini for delivery agent

cscript //nologo crtbat.vbs /p:"%DEBS_PATH%" /r:"%DEBSCU_CODE%" /s:"%MQSERVER_CTU%" /t:"DLVRF"

echo ; DE-BS Agent Delivery configuration file>%DEBS_PATH%\bank
echo ; Information about MQ Objects>>%DEBS_PATH%\bank
echo [MQSeries Objects]>>%DEBS_PATH%\bank
echo QMgrName = %MQMGR_CTU%>>%DEBS_PATH%\bank
echo QName = %DEBSCU_CODE%_TGT>>%DEBS_PATH%\bank
echo WaitMQInterval = 1000>>%DEBS_PATH%\bank
echo InputExclusiveON = N>>%DEBS_PATH%\bank
echo [Options]>>%DEBS_PATH%\bank
echo DelayInterval = 30000>>%DEBS_PATH%\bank

echo CreateFileMode = NEWVERS >> %DEBS_PATH%\bank

echo ; New  file name  to be created if file alredy exists and mode eq "NEWVERS"
echo NewVersFile = %DEBSCU_CODE%%%d.xml>>%DEBS_PATH%\bank

copy /Y %DEBS_PATH%\bank %DEBS_PATH%\agents\dlvrf.ini

rem .bat and .ini for load agent

cscript //nologo crtbat.vbs /p:"%DEBS_PATH%" /r:"%DEBSCU_CODE%" /s:"%MQSERVER_CTU%" /t:"LOADF"

echo ; DE-BS load Agent configuration file > %DEBS_PATH%\bank
echo ; Information about MQ Objects >> %DEBS_PATH%\bank
echo [MQSeries Objects] >> %DEBS_PATH%\bank
echo QMgrName = %MQMGR_CTU% >> %DEBS_PATH%\bank
echo SrcQueue = %DEBSCU_CODE%_SRC >> %DEBS_PATH%\bank
echo [Source Definition] >> %DEBS_PATH%\bank
echo LogPath = %DEBS_PATH%\log >> %DEBS_PATH%\bank
echo [Options] >> %DEBS_PATH%\bank
echo FolderWaitTime = 30000 >> %DEBS_PATH%\bank
echo MQWaitTime = 120000 >> %DEBS_PATH%\bank
echo InputExclusiveON = N >> %DEBS_PATH%\bank

copy /Y %DEBS_PATH%\bank %DEBS_PATH%\agents\loadf.ini


echo ; DE-BS  Load Status configuration file > %DEBS_PATH%\bank
echo ;  >> %DEBS_PATH%\bank
echo ; Information about MQ Objects >> %DEBS_PATH%\bank
echo [Options] >> %DEBS_PATH%\bank
echo LOAD_LOG	=%DEBS_PATH%\log\loadf.log >> %DEBS_PATH%\bank
echo DLVR_LOG	=%DEBS_PATH%\log\dlvrf.log >> %DEBS_PATH%\bank

rem copy /Y %DEBS_PATH%\bank %DEBS_PATH%\agents\bsstatus.ini
copy /Y %DEBS_PATH%\bank %windir%\bsstatus.ini


echo set ARM_PATH=%DEBS_PATH%\ARM| utils\sed s/\\/\//g > "%DEBS_PATH%\log\tempenv.bat"
call "%DEBS_PATH%\log\tempenv.bat"
del "%DEBS_PATH%\log\tempenv.bat"

echo # ARM FUS PL configuration file> %DEBS_PATH%\bank
echo [Options]>> %DEBS_PATH%\bank
echo USER = deuser>> %DEBS_PATH%\bank
echo PASSWORD = depassword>> %DEBS_PATH%\bank
echo ID = %DEBSCU_CODE%>> %DEBS_PATH%\bank
echo PATH = %ARM_PATH%>> %DEBS_PATH%\bank 

copy /Y %DEBS_PATH%\bank %DEBS_PATH%\arm\userXML.ini

del /Q %DEBS_PATH%\bank
del /Q %DEBS_PATH%\log\reg.out

agents\instsrv "DE BlackSpider delivery agent" "%DEBS_PATH%\agents\srvany.exe"
agents\instsrv "DE BlackSpider load agent" "%DEBS_PATH%\agents\srvany.exe"

rem net start "DE BlackSpider delivery agent"
rem net start "DE BlackSpider load agent"

utils\reg add "HKLM\SYSTEM\CurrentControlSet\Services\DE BlackSpider delivery agent\Parameters\Application=%DEBS_PATH%\agents\dlvrf.bat"

utils\reg add "HKLM\SYSTEM\CurrentControlSet\Services\DE BlackSpider load agent\Parameters\Application=%DEBS_PATH%\agents\loadf.bat"


cscript //nologo crtlinks.vbs /p:"%DEBS_PATH%\agents\bsstatus.exe" /r:"%DEBS_PATH%\arm\userXML.jar" /s:"%DEBS_PATH%" /t:"%DEBS_PATH%\doc\guide.htm"


rem copy "%DEBS_PATH%\agents\DE Black Spider.lnk" "%USERPROFILE%\Рабочий стол"

rem %DEBS_PATH%\agents\bsstatus.exe

wscript //nologo warning.vbs 

pause



GOTO :END


:L2

IF DEFINED DEBSCU_MQM_PREVIOUS GOTO :L3
rem cls
@echo off
@echo DE-BS Client is already installed in specified path %DEBS_PATH_PREVIOUS%. Run uninstall first. 
pause
GOTO :END


:END