@echo off
REM
REM Check if python and pip is available (in path)
REM Checks for required modules
REM Finally starts the plot server.
REM 
set CURR_DIR=%~d0%~p0

set PLOT_PORT=
if "%1" NEQ "" (
  set PLOT_PORT=--port %1
)

setlocal


echo Checking if Python3 is available...
where python 2> nul > nul
set status=%ERRORLEVEL%
if %status% neq 0 (
  echo.
  echo *** Python 3.x executable could not be found
  echo *** Make sure that Python 3.x is installed and is in the PATH
  echo.
  pause
  goto exit
)

echo Checking if Pip is available...
where pip 2> nul > nul
set status=%ERRORLEVEL%
if %status% neq 0 (
  echo.
  echo *** Pip 3.x executable could not be found
  echo *** Make sure that Python 3.x is installed and is in the PATH
  echo.
  pause
  goto exit
)

goto modules

:checkModule
pip show %1 2> nul >nul
set status=%ERRORLEVEL%
if %status% neq 0 (
  echo.
    echo *** Module %1 is missing
    echo *** Run pip install -r requirements.txt
    echo.
    pause
)
exit /b %status%

:modules
echo Checking for required Python modules...
:: Check the required modules
for %%m in (pyzmq, pyside2, pyqtgraph) do (
  REM echo Module %%m
  call :checkModule %%m

)

:done

echo Starting server...
call python "%CURR_DIR%\plot_main.py" %PLOT_PORT%
set status=%ERRORLEVEL%
if %status% neq 0 (
  echo.
    echo.
    echo *** Error running the plot server
    echo.
    pause
)


goto exit

:exit
endlocal & set START_PLOT_SERVER=%status%
exit /b %START_PLOT_SERVER%
