@echo off
pushd %~dp0
SET distr=%1
SET physical_port=%2
SET virual_port=%3
SET virual_port_out=%4
SET baud_rate=%5
@rem путь установки
if "%distr%"=="" goto params
@rem имена виртуальных портов
if "%physical_port%"=="" goto params
if "%virual_port%"=="" goto params
if "%virual_port_out%"=="" goto params
if "%baud_rate%"=="" goto params
echo %path%
echo %distr%
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%
if %version%==5.1 (
    set path=%CD%
	xcopy32 CP.jar %distr% /I
	xcopy32 CPU.jar %distr% /I
	xcopy32 CP.ini %distr% /I
	xcopy32 unst.cmd %distr% /I
	xcopy32 CPsrv.exe %distr% /I
) else (
	xcopy CP.jar %distr% /I
	xcopy CPU.jar %distr% /I
	xcopy CP.ini %distr% /I
	xcopy unst.cmd %distr% /I
	xcopy CPsrv.exe %distr% /I
)
rename %distr%\CP.jar ComProxy.jar
rename %distr%\CPU.jar ComProxyUpdater.jar
rename %distr%\CP.ini ComProxy.ini
rename %distr%\unst.cmd uninstallService.cmd
rename %distr%\CPsrv.exe ComProxySrv.exe
echo start unzip

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

if defined PROGRAMFILES(X86) (
    echo 64-bit sytem detected
    %~dp0\c0c64\setup.exe /S /D=%distr%\c0c
) else (
    echo 32-bit sytem detected
    %~dp0\c0c32\setup.exe /S /D=%distr%\c0c
)
pushd %distr%\c0c
@rem установка виртуальных портов
setupc.exe install PortName=%virual_port% PortName=%virual_port_out%
popd
cd /d %distr%
mkdir %distr%\logs

@rem установка параметров физического порта в ini
echo physical_port=%physical_port%>> ComProxy.ini
echo physical_port_baudrate=%baud_rate%>> ComProxy.ini
@rem установка параметров виртуальных портов в ini
echo virtual_port=%virual_port%>> ComProxy.ini
echo virtual_port_out=%virual_port_out%>> ComProxy.ini
echo virtual_port_baudrate=%baud_rate%>> ComProxy.ini
echo log_level=INFO>> ComProxy.ini

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 ++JvmOptions=-Dfile.encoding=UTF-8 --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                  Installation completed!
echo.
echo                     Enjoy ComProxy.
echo.
echo ************************************************************
echo              PRESS ANY KEY TO START SERVICE

ComProxySrv.exe start ComProxy
%data%
goto :eof
:params
echo.
echo Use install.cmd dir_to_install physical_port virtual_port virtual_port_out baud_rate
echo For example:
echo install.cmd c:\ComProxy COM3 COM10 COM11 57600
echo.
