@echo off
setlocal enabledelayedexpansion

REM --- Proxy settings (if needed) ---
REM If you are behind a proxy, uncomment(Remove 'REM') and edit the following lines:
REM set HTTP_PROXY=http://your.proxy.server:port
REM set HTTPS_PROXY=http://your.proxy.server:port
REM pip config set global.proxy http://your.proxy.server:port

cd /d %~dp0

REM Get the directory of the script
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 Install Node.js
echo Installing Node.js...
winget install OpenJS.NodeJS.LTS --silent --accept-package-agreements --accept-source-agreements
set "NODE_DIR=C:\Program Files\nodejs"
set "NPM_DIR=%USERPROFILE%\AppData\Roaming\npm"

REM Safely update user PATH environment variable
for /f "tokens=* usebackq" %%a in (`reg query "HKCU\Environment" /v PATH 2^>nul`) do (
    set "CURRENT_PATH=%%a"
)
for /f "tokens=3*" %%a in ("%CURRENT_PATH%") do (
    set "USER_PATH=%%a %%b"
)
set "NEW_PATH=!USER_PATH!"
echo !USER_PATH! | find /i "%NODE_DIR%" >nul || set "NEW_PATH=!NEW_PATH!;%NODE_DIR%"
echo !USER_PATH! | find /i "%NPM_DIR%" >nul || set "NEW_PATH=!NEW_PATH!;%NPM_DIR%"
setx PATH "!NEW_PATH!"

timeout /t 10
echo Updated PATH: !NEW_PATH!

REM Check node installation
node -v

REM Install Node modules
echo %XMAIL_VIEWER_DIR%
cd /d "%XMAIL_VIEWER_DIR%"
call npm install

echo Setup Python environment...
REM Create a virtual environment
python -m venv "%XMAIL_VIEWER_DIR%\viewervenv"

REM Activate the virtual environment
call "%XMAIL_VIEWER_DIR%\viewervenv\Scripts\activate.bat"
python -m pip install --upgrade pip setuptools wheel

REM Install Python packages
echo Installing dependencies...
python -m pip install neo4j==4.4 
python -m pip install cryptography
python -m pip install lxml
python -m pip install signxml
call deactivate.bat

REM Install JDK 11
echo === Installing JDK 11...
winget install -e --id EclipseAdoptium.Temurin.11.JDK

REM Set JAVA_HOME and update PATH
set "JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-11.0.12.7-hotspot"
for /f "tokens=* usebackq" %%a in (`reg query "HKCU\Environment" /v PATH 2^>nul`) do (
    set "CURRENT_PATH=%%a"
)
for /f "tokens=3*" %%a in ("%CURRENT_PATH%") do (
    set "USER_PATH=%%a %%b"
)
set "NEW_PATH=!USER_PATH!"
echo !USER_PATH! | find /i "%JAVA_HOME%\bin" >nul || set "NEW_PATH=!NEW_PATH!;%JAVA_HOME%\bin"
setx PATH "!NEW_PATH!"

REM Install Neo4j
echo Installing Neo4j...
set NEO4J_VERSION=4.4.42
set NEO4J_ZIP=neo4j-community-%NEO4J_VERSION%-windows.zip
set NEO4J_URL=https://dist.neo4j.org/%NEO4J_ZIP%

REM Folder to install Neo4j
set NEO4J_INSTALL_DIR=%SRC_DIR%\MaiMLViewerLocalRun\neo4j

REM Download zip
echo Downloading Neo4j...
curl -k -o "%NEO4J_ZIP%" %NEO4J_URL%

REM Extract zip
echo Extracting Neo4j to %NEO4J_INSTALL_DIR%...
if exist "%NEO4J_INSTALL_DIR%" rd /s /q "%NEO4J_INSTALL_DIR%"
powershell -Command "Expand-Archive -Path '%NEO4J_ZIP%' -DestinationPath '%NEO4J_INSTALL_DIR%'"

REM Delete zip
del "%NEO4J_ZIP%"

REM Neo4j.conf
set "NEO4J_CONF_FILE=%NEO4J_INSTALL_DIR%\neo4j-community-%NEO4J_VERSION%\conf\neo4j.conf"
echo %NEO4J_CONF_FILE%

echo edit neo4j.conf
powershell -Command "((Get-Content '%NEO4J_CONF_FILE%') -replace '#dbms.security.auth_enabled=.*', 'dbms.security.auth_enabled=false') | Set-Content '%NEO4J_CONF_FILE%'"
powershell -Command "((Get-Content '%NEO4J_CONF_FILE%') -replace 'dbms.connector.bolt.enabled=.*', 'dbms.connector.bolt.enabled=true') | Set-Content '%NEO4J_CONF_FILE%'"
powershell -Command "((Get-Content '%NEO4J_CONF_FILE%') -replace 'dbms.connector.http.enabled=.*', 'dbms.connector.http.enabled=true') | Set-Content '%NEO4J_CONF_FILE%'"

echo viewerSetup is done.
pause
