# Config =====================================================================

# beatoraja: https://mocha-repository.info/download.php (外部リンク)
[string] $beatoraja = 'D:\bms\beatoraja0.8.6-modernchic\beatoraja.jar'

# OpenJDK (Amazon Corretto): https://aws.amazon.com/jp/corretto/ (外部リンク)
# OpenJDK (Liberica Full JDK): https://bell-sw.com/ (外部リンク)
[string] $java = 'C:\jdk-21.0.1\bin\java.exe'

# JavaFX SDK: https://gluonhq.com/products/javafx/ (外部リンク)
[string] $javafx = 'C:\javafx-sdk-21.0.1\lib'

# 編集中の一時譜面を 9 ボタンとして認識させるか否か? $TRUE or $FALSE
[bool] $force_9buttons = $FALSE

# beatoraja 終了後に VirtualMIDISynth 等を再起動するか否か? $TRUE or $FALSE
[bool] $isRebootApps = $FALSE

# ============================================================================

$java_options = @(
    '-Dsun.java2d.opengl=true'
    '-Dawt.useSystemAAFontSettings=on'
    '-Dswing.aatext=true'
    '-Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel'
)
$env:_JAVA_OPTIONS = "'" + ($java_options -join ' ') + "'"
$is_chart = '\.(bme|bml|bms|pms)$'

$p1 = '--module-path'
$p2 = '--add-modules=javafx.controls,javafx.fxml'
$p5 = '-Xms2048m'
$p6 = '-Xmx2048m'

function Reboot-Apps {
    param (
        [String] $process_name = 'VirtualMIDISynth.exe'
    )
    # https://koreyakono.hatenadiary.jp/entry/2021/02/17/225041 (外部リンク)
    Get-WmiObject Win32_Process |
            Where-Object {$_.CommandLine -match $process_name} |
            ForEach-Object {
        Stop-Process $_.Handle
        Start-Process $_.Path
        Write-Output($_.Path)
    }
}

Push-Location -LiteralPath (Split-Path $beatoraja -Parent)

If (Test-Path -LiteralPath $args[0]) {
    $p = [System.IO.Path]::GetFullPath($args[0])
    If ($p -match $is_chart) {
        switch ($force_9buttons) {
            $TRUE {
                $q = Split-Path $p -Parent
                $pms = Join-Path $q ___bmse_temp.pms
                If (Test-Path $pms) {
                    Remove-Item -LiteralPath $pms
                }
                Copy-Item -LiteralPath $p $pms

                & $java $p1 $javafx $p2 $p5 $p6 '-jar' $beatoraja $pms $args[1] | Out-Null
# ↑ Liberica Full JDK を使用する場合は、以下のように変更しても構わない↓
                # & $java $p5 $p6 '-jar' $beatoraja $pms $args[1] | Out-Null

                Remove-Item -LiteralPath $pms
            }
            $FALSE {
                & $java $p1 $javafx $p2 $p5 $p6 '-jar' $beatoraja $p $args[1] | Out-Null
# ↑ Liberica Full JDK を使用する場合は、以下のように変更しても構わない↓
                # & $java $p5 $p6 '-jar' $beatoraja $p $args[1] | Out-Null
            }
        }
    }
}

Pop-Location

If ($isRebootApps) {
    Reboot-Apps
}

Read-Host 'Done'

[End Of File]