@echo off
setlocal
cd /d %~dp0

:START_SERVICES
rem setting up the environment
set "CURRENT_DIR=%~dp0"
set "CURRENT_DIR=%CURRENT_DIR:~0,-1%"
for %%I in ("%CURRENT_DIR%\..") do set "ROOT_DIR=%%~fI"
set "SRC_DIR=%ROOT_DIR%\SRC"
set "XMAIL_VIEWER_DIR=%SRC_DIR%\MaiMLViewerLocalRun\xmail-viewer"

rem Neo4j and Node.js setup
set "NEO4J_INSTALL_DIR=%SRC_DIR%\MaiMLViewerLocalRun\neo4j"
set "NEO4J_VERSION=4.4.42"
set "NODE_DIR=C:\Program Files\nodejs\"
set "PATH=%PATH%;%NODE_DIR%;C:\Users\%USERNAME%\AppData\Roaming\npm"
set "MAIML_TMP_DIR=%XMAIL_VIEWER_DIR%\models\tmp"
echo Starting MaiML Viewer...
call "%XMAIL_VIEWER_DIR%\viewervenv\Scripts\activate.bat"

rem Start Neo4j
echo Starting Neo4j...
cd /d "%NEO4J_INSTALL_DIR%\neo4j-community-%NEO4J_VERSION%\bin"
start /b neo4j console > nul 2>&1

rem Start Node.js
echo Starting Node.js...
cd /d "%XMAIL_VIEWER_DIR%"
start /b node bin\www > nul 2>&1

timeout /t 10 /nobreak > nul
echo Viewer Started.

