. ./exiter.ps1

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

$BOSH_INSTALL_TARGET = Resolve-Path "${env:BOSH_INSTALL_TARGET}"

Push-Location ${BOSH_INSTALL_TARGET}
. "C:\var\vcap\packages\golang-1-windows\bosh\compile.ps1"
Pop-Location

New-Item -ItemType "directory" -Force "${BOSH_INSTALL_TARGET}\src"
robocopy /E . "${BOSH_INSTALL_TARGET}\src\"
if ($LASTEXITCODE -ge 8) {
    Write-Error "robocopy /E . ${BOSH_INSTALL_TARGET}\src\"
}

Push-Location "${BOSH_INSTALL_TARGET}/src/bosh-dns"
go build -o ${BOSH_INSTALL_TARGET}\bin\bosh-dns.exe "bosh-dns\dns"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error compiling: dns"
}

go build -o ${BOSH_INSTALL_TARGET}\bin\bosh-dns-nameserverconfig.exe "bosh-dns\dns\nameserverconfig"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error compiling: dns\nameserverconfig"
}

go build -o ${BOSH_INSTALL_TARGET}\bin\bosh-dns-health.exe "bosh-dns\healthcheck"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error compiling: healthcheck"
}

Pop-Location

Push-Location "${BOSH_INSTALL_TARGET}/src/debug"
go build -o ${BOSH_INSTALL_TARGET}\bin\cli.exe "debug\cli"
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error compiling: cli"
}
Pop-Location

New-Item -ItemType "directory" -Force "emptyfolder"
robocopy /PURGE "emptyfolder" "${BOSH_INSTALL_TARGET}/src"
if ($LASTEXITCODE -ge 8) {
    Write-Error "robocopy /PURGE emptyfolder ${BOSH_INSTALL_TARGET}/src"
}

robocopy /PURGE "emptyfolder" "${BOSH_INSTALL_TARGET}/pkg"
if ($LASTEXITCODE -ge 8) {
    Write-Error "robocopy /PURGE emptyfolder ${BOSH_INSTALL_TARGET}/pkg"
}

Remove-Item -Force -Recurse "${BOSH_INSTALL_TARGET}/src"
Remove-Item -Force -Recurse "${BOSH_INSTALL_TARGET}/pkg"
Remove-Item -Force -Recurse "emptyfolder"

Exit 0
