. ./exiter.ps1

try {
  $BOSH_INSTALL_TARGET = Resolve-Path $env:BOSH_INSTALL_TARGET
  $COMPILE_ENV = Join-Path -Path "${BOSH_INSTALL_TARGET}" -ChildPath "bosh"
  $PACKAGE_NAME = Split-Path -Path "${env:BOSH_INSTALL_TARGET}" -Leaf
} catch {
  Write-Host "$_.Exception.Message"
  Exit 1
}

$path=(Get-ChildItem "go1.27*.windows-amd64.zip").FullName

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
  param([string]$zipfile, [string]$outpath)
  [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

try {
  Write-Host "unzipping $path $BOSH_INSTALL_TARGET"
  Unzip "$path" "${BOSH_INSTALL_TARGET}"
} catch {
  Write-Host "$_.Exception.Message"
  Exit 1
}

try {
  Write-Host "Copying compile.ps1 and runtime.ps1 to ${COMPILE_ENV}"
  New-Item "${COMPILE_ENV}" -type directory

  $CompileEnvPath = (Get-ChildItem -Path "compile.env.windows").FullName
  (Get-Content -Path $CompileEnvPath).replace('${PACKAGE_NAME}', "${PACKAGE_NAME}") | Set-Content -Path "${COMPILE_ENV}\compile.ps1"
  $RuntimeEnvPath = (Get-ChildItem -Path "runtime.env.windows").FullName
  (Get-Content -Path $RuntimeEnvPath).replace('${PACKAGE_NAME}', "${PACKAGE_NAME}") | Set-Content -Path "${COMPILE_ENV}\runtime.ps1"
} catch {
  Write-Host "$_.Exception.Message"
  Exit 1
}

Exit 0
