@echo off
setlocal enabledelayedexpansion


set "Mode=%~1"
set "Count=%~2"

if [%Mode%] == [] goto Usage
if [%Count%] == [] goto Usage

if /i [%Mode%] == [remove] (
    set "Mode=remove"
) else (
    set "Mode=install"
)

if /i [%~3] == [true] (
    set "UsePortsClass=1"
) else if /i [%~3] == [yes] (
    set "UsePortsClass=1"
) else if [%~3] == [1] (
    set "UsePortsClass=1"
) else (
    set "UsePortsClass=0"
)

set "Dir=C:\Program Files (x86)\com0com"
set "Exe=setupc.exe"
set /a Start=10
set /a End=Count-1
set /a Spread=20

pushd %cd%
cd /d "%Dir%"

@REM for /l %%i in (0,1,249) do setupc --no-update install - -
for /l %%i in (0,1,%End%) do (
    if /i [%Mode%] == [install] (
        set /a In=%%i+Start
        set /a Out=In+Spread
        if [%UsePortsClass%] == [1] (
            set "Arg1=PortName=COM#"
        ) else (
            set "Arg1=PortName=COM!In!"
        )
        set "Arg2=PortName=COM!Out!"
        @REM set "Args=--no-update install %%i !Arg1! !Arg2!"
        set "Args=--no-update install !Arg1! !Arg2!"
        "!Exe!" !Args!
    ) else (
        set "Args=remove %%i"
        "!Exe!" !Args!
    )
)

if /i [%Mode%] == [install] (
    "%Exe%" %Mode%

    @REM if [%UsePortsClass%] == [1] (
    @REM     for /l %%i in (0,1,%End%) do (
    @REM         set /a I=End-%%i
    @REM         set /a N=Start+I
    @REM         "!Exe!" change CNCA!I! RealPortName=COM!N!
    @REM     )
    @REM )
)

popd
goto Cleanup


:Usage
echo Usage: %~n0 mode count [use_ports_class]


:Cleanup
