@echo off

SET distr=%1
@rem путь установки
if "%distr%"=="" goto params
pushd %distr%
For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="physical_port" set physical_port=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port" set virtual_port=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port_out" set virtual_port_out=%%B
)

if "%virtual_port_out%"=="" (
    For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
        IF "%%A"=="pos_port" set virtual_port_out=%%B
    )
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port_baudrate" set virtual_port_baudrate=%%B
)
if "%virtual_port_baudrate%"=="" (set virtual_port_baudrate=115200)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="physical_port_baudrate" set physical_port_baudrate=%%B
)
if "%physical_port_baudrate%"=="" (set physical_port_baudrate=57600)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="fr_connection_type" set fr_connection_type=%%B
)
if "%fr_connection_type%"=="" (set fr_connection_type=COM)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="log_level" set log_level=%%B
)
if "%log_level%"=="" (set log_level=ERROR)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="start_agent" set start_agent=%%B
)
if "%start_agent%"=="" (set start_agent=true)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="fr_serial_number" set fr_serial_number=%%B
)


@rem Считывание значений параметров второго фискального регистратора, если таковой был прописан в ComProxy.ini
For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="physical_port2" set physical_port2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port2" set virtual_port2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port_out2" set virtual_port_out2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="virtual_port_baudrate2" set virtual_port_baudrate2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="physical_port_baudrate2" set physical_port_baudrate2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="fr_connection_type2" set fr_connection_type2=%%B
)

For /F "tokens=1* delims==" %%A IN (ComProxy.ini) DO (
    IF "%%A"=="fr_serial_number2" set fr_serial_number2=%%B
)


echo stopping ComProxy service
ComProxySrv.exe stop ComProxy >nul 2>&1
@echo off > gui_stop.tsk
ping -n 13 localhost  >nul 2>&1
ComProxySrv.exe delete ComProxy >nul 2>&1
ping -n 5 localhost  >nul 2>&1

rd /Q /S jre >nul 2>&1
rd /Q /S libs >nul 2>&1
rd /Q /S libx32 >nul 2>&1
rd /Q /S libx64 >nul 2>&1
rd /Q /S logs >nul 2>&1
rd /Q /S libxarmhf >nul 2>&1
rd /Q /S ppp >nul 2>&1
rd /Q /S tasks >nul 2>&1
rd /Q /S oldVersion >nul 2>&1

ping -n 5 localhost  >nul 2>&1
del /F /S /Q /A "%distr%\ComProxy.ini" >nul 2>&1
del /F /S /Q /A "%distr%\ComProxyAgent.jar" >nul 2>&1
del /F /S /Q /A "%distr%\ComProxyUpdater.jar" >nul 2>&1
del /F /S /Q /A "%distr%\ComProxy.jar" >nul 2>&1
del /F /S /Q /A "%distr%\ComProxySrv.exe" >nul 2>&1
del /F /S /Q /A "%distr%\KSL.exe" >nul 2>&1
del /F /S /Q /A "%distr%\StartAgent.cmd" >nul 2>&1
del /F /S /Q /A "%distr%\firmware_new.bin" >nul 2>&1
del /F /S /Q /A "%distr%\ComProxy_new.jar" >nul 2>&1
del /F /S /Q /A "%distr%\gui_stop.tsk" >nul 2>&1
del /F /S /Q /A "%distr%\AgentService.jar" >nul 2>&1
del /F /S /Q /A "%distr%\AgentService.ini" >nul 2>&1
del /F /S /Q /A "%distr%\uninstallService.cmd" >nul 2>&1
popd
C:\Windows\system32\reg delete "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Hardware Profiles\Current\System\CurrentControlSet\Enum\COM0COM" /f >nul 2>&1
For /F "Tokens=2 Delims=[]" %%i In ('ver') Do (
   For /F "Tokens=2,3 Delims=. " %%a In ("%%i") Do Set version=%%a.%%b
)
mkdir %distr%
echo %CD%
if %version%==5.1 (
    set path=%CD%
	xcopy32 CPA.jar %distr% /I
	xcopy32 CPU.jar %distr% /I
	xcopy32 CP.jar %distr% /I
	xcopy32 CP.ini %distr% /I
	xcopy32 STA.cmd %distr% /I
	xcopy32 unst.cmd %distr% /I
	xcopy32 CPsrv.exe %distr% /I
	xcopy32 KSL.exe %distr% /I
	xcopy32 AS.jar %distr% /I
	xcopy32 AS.ini %distr% /I
) else (
	xcopy CPA.jar %distr% /I
	xcopy CPU.jar %distr% /I
	xcopy CP.jar %distr% /I
	xcopy CP.ini %distr% /I
	xcopy STA.cmd %distr% /I
	xcopy unst.cmd %distr% /I
	xcopy CPsrv.exe %distr% /I
	xcopy KSL.exe %distr% /I
	xcopy AS.jar %distr% /I
	xcopy AS.ini %distr% /I
)
rename %distr%\CPA.jar ComProxyAgent.jar
rename %distr%\CPU.jar ComProxyUpdater.jar
rename %distr%\CP.jar ComProxy.jar
rename %distr%\CP.ini ComProxy.ini
rename %distr%\STA.cmd StartAgent.cmd
rename %distr%\unst.cmd uninstallService.cmd
rename %distr%\CPsrv.exe ComProxySrv.exe
rename %distr%\AS.jar AgentService.jar
rename %distr%\AS.ini AgentService.ini
echo start unzip

7z.exe x jre8.zip -r -o%distr%
7z.exe x libs.zip -r -o%distr%

cd /d %distr%
mkdir %distr%\logs
@rem установка параметров физического порта в ini
echo physical_port=%physical_port%>> ComProxy.ini
echo physical_port_baudrate=%physical_port_baudrate%>> ComProxy.ini

@rem установка параметров виртуальных портов в ini
echo virtual_port=%virtual_port%>> ComProxy.ini
echo virtual_port_out=%virtual_port_out%>> ComProxy.ini
echo virtual_port_baudrate=%virtual_port_baudrate%>> ComProxy.ini

@rem установка типа соединений в ini
echo fr_connection_type=%fr_connection_type%>> ComProxy.ini

@rem установка доп. параметров в ini
echo log_level=%log_level%>> ComProxy.ini
echo start_agent=%start_agent%>> ComProxy.ini
echo fr_serial_number=%fr_serial_number%>> ComProxy.ini

@rem установка параметров второго фискального регистратора
if not "%physical_port2%" == "" (
    echo physical_port2=%physical_port2%>> ComProxy.ini
    echo physical_port_baudrate2=%physical_port_baudrate2%>> ComProxy.ini
    echo virtual_port2=%virtual_port2%>> ComProxy.ini
    echo virtual_port_out2=%virtual_port_out2%>> ComProxy.ini
    echo virtual_port_baudrate2=%virtual_port_baudrate2%>> ComProxy.ini
    echo fr_connection_type2=%fr_connection_type2%>> ComProxy.ini
    echo fr_serial_number2=%fr_serial_number2%>> ComProxy.ini
) else (
    echo Param physical_port2 is empty - second FR is absent
)

@rem включен запуск агента
Set pathCmd=%cd%\StartAgent.cmd
Set data=start /b %cd%\jre\bin\javaw -Xmx200m -Dfile.encoding=CP1251 -jar %cd%\ComProxyAgent.jar
echo %data% >> StartAgent.cmd
Reg Add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "StartAgent" /t REG_SZ /d "%pathCmd%" /f
ComProxySrv.exe install ComProxy --Install="%distr%\ComProxySrv.exe" --Jvm="%distr%\jre\bin\client\jvm.dll" --JvmOptions=-Xmx16M ++JvmOptions=-XX:PermSize=16M ++JvmOptions=-XX:MaxPermSize=16M --Startup=auto --LogPath="%distr%\logs" --Classpath="%distr%\ComProxy.jar" --StartMode jvm --StartClass org.comproxy.Main --StartMethod start --StopMode jvm --StopClass  org.comproxy.Main --StopMethod stop
echo ************************************************************
echo.
echo                   Update completed!
echo.
echo                     Enjoy ComProxy.
echo.
echo ************************************************************
ComProxySrv.exe start ComProxy
%data%
goto :eof
:params
echo.
echo Use update.cmd dir_to_update
echo For example:
echo update.cmd c:\ComProxy
echo.
