param([switch]$DatabaseOnly, [int]$DatabasePort = 55432, [int]$Port = 3000, [string]$PostgresBin = 'C:\Program Files\PostgreSQL\17\bin') $ErrorActionPreference = 'Stop' $repo = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..')) $portableBin = Join-Path $repo 'target/postgresql/pgsql/bin' if (!$PSBoundParameters.ContainsKey('PostgresBin') -and (Test-Path (Join-Path $portableBin 'postgres.exe'))) { $PostgresBin = $portableBin } if (!(Test-Path (Join-Path $PostgresBin '../share/postgres.bki'))) { throw 'A complete PostgreSQL server distribution is required. Pass -PostgresBin PATH; client-only installations lack share/postgres.bki.' } $localDir = Join-Path $repo 'target/local' $dataDir = Join-Path $localDir 'postgres' New-Item -ItemType Directory -Force $localDir | Out-Null if (!(Test-Path (Join-Path $dataDir 'PG_VERSION'))) { & (Join-Path $PostgresBin 'initdb.exe') -D $dataDir -U meguri --auth=trust --encoding=UTF8 --locale=C if ($LASTEXITCODE) { throw 'initdb failed' } } & (Join-Path $PostgresBin 'pg_ctl.exe') -D $dataDir status *> $null if ($LASTEXITCODE) { $pgArgs = @('-D',('"'+$dataDir+'"'),'-l',('"'+(Join-Path $localDir 'postgres.log')+'"'),'-o',('"-h 127.0.0.1 -p '+$DatabasePort+'"'),'-w','start') $pgStart = Start-Process -FilePath (Join-Path $PostgresBin 'pg_ctl.exe') -ArgumentList $pgArgs -WindowStyle Hidden -PassThru $pgStart.WaitForExit() if ($pgStart.ExitCode) { throw 'PostgreSQL did not start; inspect target/local/postgres.log' } } foreach ($dbName in @('meguri','meguri_workspace_test')) { $exists = & (Join-Path $PostgresBin 'psql.exe') -h 127.0.0.1 -p $DatabasePort -U meguri -d postgres -Atc "SELECT 1 FROM pg_database WHERE datname='$dbName'" if ($exists -ne '1') { & (Join-Path $PostgresBin 'createdb.exe') -h 127.0.0.1 -p $DatabasePort -U meguri $dbName if ($LASTEXITCODE) { throw 'Could not create development database' } } } @{ postgresBin=$PostgresBin; databasePort=$DatabasePort; port=$Port } | ConvertTo-Json | Set-Content (Join-Path $localDir 'settings.json') $env:DATABASE_URL = "postgresql://meguri@127.0.0.1:$DatabasePort/meguri" $env:MEGURI_TEST_DATABASE_URL = "postgresql://meguri@127.0.0.1:$DatabasePort/meguri_workspace_test" if ($DatabaseOnly) { Write-Output "Database ready. Test URL: $env:MEGURI_TEST_DATABASE_URL"; return } $pidFile = Join-Path $localDir 'app.pid' $appExecutable = Join-Path $localDir 'meguri-dev.exe' if (Test-Path $pidFile) { $old = Get-Process -Id ([int](Get-Content $pidFile)) -ErrorAction SilentlyContinue if ($old -and $old.Path -eq $appExecutable) { throw 'Meguri is already running. Run scripts/stop-dev.ps1 first.' } } Push-Location $repo try { cargo build --locked; if ($LASTEXITCODE) { throw 'Build failed' } } finally { Pop-Location } $env:MEGURI_BIND = "127.0.0.1:$Port" $env:MEGURI_PUBLIC_URL = "http://127.0.0.1:$Port" $env:MEGURI_ALLOW_SIGNUP = 'true' $env:MEGURI_SEED_DEMO = 'true' $env:MEGURI_ROLE = 'all' Copy-Item -LiteralPath (Join-Path $repo 'target/debug/meguri.exe') -Destination $appExecutable $app = Start-Process -FilePath $appExecutable -WorkingDirectory $repo -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $localDir 'app.log') -RedirectStandardError (Join-Path $localDir 'app-error.log') $app.Id | Set-Content $pidFile Write-Output "Meguri starting at $env:MEGURI_PUBLIC_URL — demo: alice / meguri-demo-only"