$ErrorActionPreference = "Stop"

$root = Split-Path -Parent $PSScriptRoot
$maven = Join-Path $root ".tools/apache-maven-3.9.9/bin/mvn.cmd"
$appDir = Join-Path $root "examples/java-demo-app"
$jarPath = Join-Path $appDir "target/java-demo-app-0.0.1-SNAPSHOT.jar"
$jmxJar = Join-Path $root ".tools/jmx_prometheus_javaagent-0.18.0.jar"
$jmxConfig = Join-Path $appDir "jmx-exporter.yaml"

if (-not (Test-Path $maven)) {
  throw "Maven not found at $maven"
}

if (-not (Test-Path $jmxJar)) {
  throw "JMX exporter jar not found at $jmxJar"
}

& $maven -f (Join-Path $appDir "pom.xml") package -DskipTests

$process = Start-Process -FilePath "java" `
  -ArgumentList "-javaagent:$jmxJar=19404:$jmxConfig", "-jar", $jarPath `
  -WorkingDirectory $appDir `
  -PassThru

$process.Id
