@echo off
setlocal EnableExtensions

set "APP_DIR=%~dp0app"
set "DATA_DIR=%LOCALAPPDATA%\Pinterest AI Operator"
set "LOG_DIR=%DATA_DIR%\Logs"
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"

cd /d "%APP_DIR%"
set "NEXT_DIST_DIR=.next"
set "PINTEREST_AUTO_DATA_DIR=%DATA_DIR%"
set "PORT=%~1"
if "%PORT%"=="" set "PORT=3000"
set "NODE_EXE=%~dp0runtime\node\node.exe"
if not exist "%NODE_EXE%" set "NODE_EXE=node"

if exist "%DATA_DIR%\.env.local" (
  for /f "usebackq tokens=1,* delims==" %%A in ("%DATA_DIR%\.env.local") do (
    if not "%%A"=="" set "%%A=%%B"
  )
)

"%NODE_EXE%" node_modules\next\dist\bin\next start >> "%LOG_DIR%\pinterest-auto.log" 2>&1
