# PowerShell script to start JBoss EAP 8.0

# Configuration
$JBOSS_HOME = "C:\Dev2012\BUILDERS\jboss-eap-8.0"
$JAVA_HOME = "C:\Dev2012\BUILDERS\java\jdk-17.0.9"

# Function to check if JBoss is already running
function Is-JBossRunning {
    $jbossProcess = Get-CimInstance Win32_Process -Filter "Name = 'java.exe'" | 
                    Where-Object { $_.CommandLine -like "*jboss.home.dir=$JBOSS_HOME*" }
    return $null -ne $jbossProcess
}

# Check if Java exists
if (-not (Test-Path $JAVA_HOME)) {
    Write-Host "Error: Java 17 not found at $JAVA_HOME" -ForegroundColor Red
    exit 1
}

# Check if JBoss directory exists
if (-not (Test-Path $JBOSS_HOME)) {
    Write-Host "Error: JBoss directory not found at $JBOSS_HOME" -ForegroundColor Red
    exit 1
}

# Check if configuration file exists
$configFile = "$JBOSS_HOME\standalone\configuration\standalone-full-ADV360-BES-EAP_8_POSTGRES.xml"
if (-not (Test-Path $configFile)) {
    Write-Host "Error: Configuration file not found at $configFile" -ForegroundColor Red
    exit 1
}

# Set environment variables
$env:JAVA_HOME = $JAVA_HOME
$env:JBOSS_HOME = $JBOSS_HOME
$env:PATH = "$JAVA_HOME\bin;$env:PATH"
$env:NOPAUSE = "true"
# Remove this as it might affect logging
# $env:LAUNCH_JBOSS_IN_BACKGROUND = "true"

# Check if JBoss is already running
if (Is-JBossRunning) {
    Write-Host "JBoss is already running!" -ForegroundColor Yellow
    exit 0
}

Write-Host "Starting JBoss EAP 8.0..." -ForegroundColor Green
Write-Host "Using Java from: $JAVA_HOME" -ForegroundColor Yellow
Write-Host "JBoss Home: $JBOSS_HOME" -ForegroundColor Yellow
Write-Host "Using configuration: standalone-full-ADV360-BES-EAP_8_POSTGRES.xml" -ForegroundColor Yellow

# Start JBoss in standalone mode
$startScript = "$JBOSS_HOME\bin\standalone.bat"
$jvmOptions = @(
    "-DIDServer=GS",
    "-Dbtf.PathToParse=C:\Dev2012\advc0\in",
    "-Djava.awt.headless=true",
    "-DFilOptimize=true",
    "-DMTH_VALID_JB=internal",
    "-DskQueueElab=GS",
    "-DskQueueWrite=GS",
    "-DmtSession=GS",
    "-agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"
)

# Add JVM options to JAVA_OPTS
$env:JAVA_OPTS = "$($jvmOptions -join ' ')"

Write-Host "Starting JBoss with custom JVM options..." -ForegroundColor Green
Write-Host "JVM Options: $env:JAVA_OPTS" -ForegroundColor Yellow
Write-Host "Remote debugging enabled on port 8787" -ForegroundColor Cyan

# Create the command line arguments
$cmdArgs = "-c standalone-full-ADV360-BES-EAP_8_POSTGRES.xml --debug -b 0.0.0.0 -bmanagement 0.0.0.0"

# Start JBoss - Modified to use Start-Process with RedirectStandardOutput
$logFile = "$JBOSS_HOME\standalone\log\server-info.log"
$processStartInfo = @{
    FilePath = $startScript
    ArgumentList = $cmdArgs
    RedirectStandardOutput = $logFile
    RedirectStandardError = "$JBOSS_HOME\standalone\log\server_error.log"
    WorkingDirectory = "$JBOSS_HOME\bin"
    NoNewWindow = $true
    PassThru = $true
}

Write-Host "Starting JBoss and redirecting output to $logFile" -ForegroundColor Yellow
$jbossProcess = Start-Process @processStartInfo

# Wait a few seconds to check if the process started successfully
Start-Sleep -Seconds 10

if (Is-JBossRunning) {
    Write-Host "JBoss started successfully!" -ForegroundColor Green
    Write-Host "Admin console will be available at: http://localhost:9990" -ForegroundColor Yellow
    Write-Host "Remote debugging is enabled on port 8787" -ForegroundColor Cyan
    Write-Host "Check JBoss logs at: $logFile" -ForegroundColor Yellow
} else {
    Write-Host "Error: JBoss failed to start. Check the logs at: $logFile" -ForegroundColor Red
}