@echo off

:: В первой строке меняем текущий диск, во второй - замена на диск и путь из которых запущен командный файл(необходимо при запуске от имени администратора)
 %~d0
 cd /D "%~p0"

:: 1)Переменные
  
 set MainKSRSDir=d:\ksrs-terminal
 set DepKSRSQt=..\Dependencies\Windows\Qt\Qt485
 set TermKSRSUnits=..\Terminal\Modules
 set TermLIRADrv=..\Terminal\Drv\Windows
 set windows=%SystemRoot%
 set vc90Debug=Microsoft.VC90.DebugCRT
 set dirMain=%cd%
 set LiraDrv32=%SystemRoot%\system32\drivers\ftdibus.sys
 ::set LiraDrv64=%SystemRoot%\syswow64\

 set DepKSRSPhp=..\Dependencies\Windows\php-win-5-4-11.zip
 set DepKSRSSnmp=..\Dependencies\Windows\net-snmp-win-5-7-2.zip
 set arch7z="c:\Program Files (x86)\7-Zip\7z.exe"
 set Key=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

 set PathPhp=%Path%c:\php
 set PathSnmp=%Path%c:\usr\bin
 set PathPhpSnmp=%Path%c:\php;c:\usr\bin
 set php1=0
 set agent=c:\usr\bin

::2) Проверка/установка php, snmp, регистрация службы

echo "Verifying the installation Php, net-snmp.. "

for /f "tokens=*" %%i in ('Reg query "%Key%" /v Path') do echo %%i | findstr /i c:\php > NUL
if %errorlevel% == 0 (echo php - OK) else (%arch7z% x -r -y %DepKSRSPhp% -oc:\ & setx Path "%PathPhp%" /M & set php1=1)

for /f "tokens=*" %%i in ('Reg query "%Key%" /v Path') do echo %%i | findstr /i %agent% > NUL
if %errorlevel% == 0 (echo net-snmp - OK) else (%arch7z% x -r -y %DepKSRSSnmp% -oc:\ & (if %php1% == 1 (setx Path "%PathPhpSnmp%" /M) else (setx Path "%PathSnmp%" /M)))

%agent%\snmpd.exe -register -quiet -Ln


::3)проверка драйверов РП

 echo "Lira drivers checking.. "
if exist %LiraDrv32% (echo OK) else (goto no_drv) 

::4) проверка директории, если нет создаем

 echo "create main directory (%MainKSRSDir%).. "
 if exist %MainKSRSDir% (echo OK) else (md %MainKSRSDir% && echo OK)

 cd /d %MainKSRSDir%
 echo stop "krs-terminal-server" service.. 
 krs-terminal-server --stop
 if %errorlevel% == 0 (echo OK) 
 
 echo "stop "snmp-agent" service.."
 snmp-agent --stop
 if %errorlevel% == 0 (echo OK) 

 echo "stopping KARSTerminal.. "
 taskkill /im KARSTerminal.exe
 if %errorlevel% == 0 (echo OK) else (echo process not running)
 
 cd /d %dirMain%
 
:: 5)дополнительные пакеты - qt, microsoft.vc90

echo "copy Qt file.. "
 xcopy %DepKSRSQt% /e/y %MainKSRSDir% /q || xcopy %DepKSRSQt% /e/y %Windows% /q 
 if %errorlevel% == 0 (echo OK) else (echo fail) 
 
 echo "copy %vc90Debug% file.. "
 xcopy %TermKSRSUnits%\%vc90Debug% /e/y %MainKSRSDir%\%vc90Debug%\ /q
 if %errorlevel% == 0 (echo OK) else (echo fail) 

:: 6) копируем в корень Терминала все из папки Terminal

 echo "copy Terminal file.. "
 xcopy %TermKSRSUnits%\Windows /e/y %MainKSRSDir%\ /q
 if %errorlevel% == 0 (echo OK) else (echo fail ) 

 echo "checking configuration file.."
 if exist %MainKSRSDir%\config\KARSTerminal.conf (echo OK) else (xcopy %TermKSRSUnits%\config /e/y %MainKSRSDir%\config\ /q && echo "create configuration file OK")

:: 7)Блок сервисов

 cd /d %MainKSRSDir%
 krs-terminal-server --install

 if %errorlevel% == 0 (echo "krs-terminal-server started")

 snmp-agent --start
 if %errorlevel% == 0 (echo "snmp-agent started")

 start KARSTerminal.exe 
 if %errorlevel% == 0 (echo "KARSTerminal started")

 echo "Setup terminal software completed successfully!"
 echo -------------------------------------------------

goto end

:no_drv
echo "Lira drivers not exist!"
echo "To be continue installation, you must install the drivers!" 
echo -----------------------------------------------------------

:end
pause