param(
    [Parameter(Mandatory = $true)] [string] $MtciDirectory,
    [Parameter(Mandatory = $true)] [double] $West,
    [Parameter(Mandatory = $true)] [double] $South,
    [Parameter(Mandatory = $true)] [double] $East,
    [Parameter(Mandatory = $true)] [double] $North,
    [Parameter(Mandatory = $true)] [string] $ReportFile,
    [string] $OutputMtciDirectory
)

$ErrorActionPreference = "Stop"
$projectRoot = Split-Path -Parent $PSScriptRoot
$applicationDir = Join-Path $projectRoot "application"
$env:GRADLE_USER_HOME = Join-Path $projectRoot ".gradle-user"

$gradleArguments = @(
    '-p', $applicationDir,
    '--project-cache-dir', (Join-Path $projectRoot ".gradle-project-cache"),
    '-Pmaptools.buildRoot=../build-windows',
    ':core:analyzeMtciSelection',
    "-PmtciDirectory=$MtciDirectory",
    "-PselectionWest=$West",
    "-PselectionSouth=$South",
    "-PselectionEast=$East",
    "-PselectionNorth=$North",
    "-PselectionReport=$ReportFile"
)
if ($OutputMtciDirectory) {
    $gradleArguments += "-PselectionOutputMtci=$OutputMtciDirectory"
}

& (Join-Path $applicationDir "gradlew.bat") @gradleArguments
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
