. ./exiter.ps1
. C:\var\vcap\packages\golang-1-windows\bosh\compile.ps1
$env:GOPATH="C:\var\vcap"

$ErrorActionPreference = "Stop";
trap { $host.SetShouldExit(1) }

$BOSH_INSTALL_TARGET = Resolve-Path "${env:BOSH_INSTALL_TARGET}"
if ((Get-Item $BOSH_INSTALL_TARGET).LinkType -ne "") {
  $BOSH_INSTALL_TARGET = (Get-Item $BOSH_INSTALL_TARGET).Target
}

$pkg_name="loggregator-agent"
$pkg_path="./cmd/agent"

go.exe build -mod=vendor  -o "${BOSH_INSTALL_TARGET}\${pkg_name}.exe" "${pkg_path}"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error compiling: ${pkg_path}"
}

# Cleanup build artifacts
New-Item -ItemType directory -Path ".\emptydirectory" -Force

robocopy.exe ".\emptydirectory" "${BOSH_INSTALL_TARGET}\src" /PURGE
if ($LASTEXITCODE -ge 8) {
    Write-Error "robocopy.exe .\emptydirectory ${BOSH_INSTALL_TARGET}\src
/PURGE"
}

Remove-Item -Recurse -Path "${BOSH_INSTALL_TARGET}\src" -Force
Remove-Item -Recurse -Path ".\emptydirectory" -Force

Exit 0
