@echo off

REM
REM UN-REMARK the desired  speed below:
REM
REM set DESIREDSPEED="Auto "
REM set DESIREDSPEED="10Mbps/Half "
REM set DESIREDSPEED="10Mbps/Full "
REM set DESIREDSPEED="100Mbps/Half "
REM set DESIREDSPEED="100Mbps/Full "
set DESIREDSPEED="Auto-negotiate "

set RET=0
set RETURN=0
set ADDLOG=addLogRecord %0

call :LOG INFO "Setting all Network Interface speeds...."
FOR /F "usebackq tokens=1,2,3*" %%i IN (`nicSpeed`) DO call :ALLNICS %%i %%j %%k %%l
goto :EOF

:ALLNICS
        set SPEEDSET=NO
        set NICINDEX=%1
        set NICMAC=%2
        FOR /F "usebackq tokens=1,2*" %%i IN (`nicSpeed -i %NICINDEX%`) DO call :SETSPEED %NICINDEX% %%i %%j
        if %SPEEDSET% == "NO" (
          call :LOG  WARNING "NotSupported for NIC %NICINDEX%"
        )
goto :DONE

:SETSPEED
        REM echo %*
        set NICINDEX=%1
        set SPEEDINDEX=%2
        set NICSPEED=%3 %4

        if "%NICSPEED%" == %DESIREDSPEED% (
          call nicSpeed -i %NICINDEX% -s %2
          set RET=%ERRORLEVEL%
          if /i %RET% GTR 0 (
              call :LOG  ERROR "Failed to set speed for %*"
          ) else (
            set SPEEDSET=YES
            call :LOG  INFO "Speed set succesfully for NIC%*"
          )

        )
        if /i %RET% GTR %RETURN%  (
          set RETURN=%RET%
        )
goto :EOF

:LOG
call %ADDLOG% %*
echo %~2 %3
goto :EOF

:DONE
exit /B %RETURN%