<commit_before><#
.SYNOPSIS
    Packages a Vagrant installer from a substrate package.

.DESCRIPTION
    Packages a Vagrant installer from a substrate package.

    This script requires administrative privileges.

    You can run this script from an old-style cmd.exe prompt using the
    following:

      powershell.exe -ExecutionPolicy Unrestricted -NoLogo -NoProfile -Command "& '.\package.ps1'"

.PARAMETER SubstratePath
    Path to the substrate zip file.

.PARAMETER VagrantRevision
    The commit revision of Vagrant to install.
#>
param(
    [Parameter(Mandatory=$true)]
    [string]$SubstratePath,

    [Parameter(Mandatory=$true)]
    [string]$VagrantRevision
)

# Exit if there are any exceptions
$ErrorActionPreference = "Stop"

# Put this in a variable to make things easy later
$UpgradeCode = "1a672674-6722-4e3a-9061-8f539a8b0ed6"

# Get the directory to this script
$Dir = Split-Path $script:MyInvocation.MyCommand.Path

# Lookup the WiX binaries, these will error if they're not on the Path
$WixHeat   = Get-Command heat | Select-Object -ExpandProperty Definition
$WixCandle = Get-Command candle | Select-Object -ExpandProperty Definition
$WixLight  = Get-Command light | Select-Object -ExpandProperty Definition

#--------------------------------------------------------------------
# Helper Functions
#--------------------------------------------------------------------
function Expand-ZipFile($file, $destination) {
    $shell = New-Object -ComObject "Shell.Application"
    $zip = $shell.NameSpace($file)
    foreach($item in $zip.items()) {
        $shell.Namespace($destination).copyhere($item)
    }
}

#--------------------------------------------------------------------
# Extract Substrate
#--------------------------------------------------------------------
# We need the full path to the file
$SubstratePath = Resolve-Path $SubstratePath

# We need to create a temporary configuration directory
$SubstrateTmpDir = [System.IO.Path]::GetTempPath()
$SubstrateTmpDir = [System.IO.Path]::Combine(
    $SubstrateTmpDir, [System.IO.Path]::GetRandomFileName())
[System.IO.Directory]::CreateDirectory($SubstrateTmpDir) | Out-Null
Write-Host "Substrate temp dir: $($SubstrateTmpDir)"

# Unzip
Write-Host "Expanding substrate..."
Expand-ZipFile -file $SubstratePath -destination $SubstrateTmpDir

# Set the full path to the substrate
$SubstrateDir = "$($SubstrateTmpDir)"

#--------------------------------------------------------------------
# Install Vagrant
#--------------------------------------------------------------------
$VagrantTmpDir = [System.IO.Path]::GetTempPath()
$VagrantTmpDir = [System.IO.Path]::Combine(
    $VagrantTmpDir, [System.IO.Path]::GetRandomFileName())
[System.IO.Directory]::CreateDirectory($VagrantTmpDir) | Out-Null
Write-Host "Vagrant temp dir: $($VagrantTmpDir)"

$VagrantSourceURL = "https://github.com/mitchellh/vagrant/archive/$($VagrantRevision).zip"
$VagrantDest      = "$($VagrantTmpDir)\vagrant.zip"

# Download
Write-Host "Downloading Vagrant: $($VagrantRevision)"
$client = New-Object System.Net.WebClient
$client.DownloadFile($VagrantSourceURL, $VagrantDest)

# Unzip
Write-Host "Unzipping Vagrant"
Expand-ZipFile -file $VagrantDest -destination $VagrantTmpDir

# Set the full path to where Vagrant is
$VagrantSourceDir = "$($VagrantTmpDir)\vagrant-$($VagrantRevision)"

# Build gem
Write-Host "Building Vagrant Gem"
Push-Location $VagrantSourceDir
&"$($SubstrateDir)\embedded\bin\gem.bat" build vagrant.gemspec
Copy-Item vagrant-*.gem -Destination vagrant.gem
Pop-Location

# Determine the version
$VagrantVersionFile = Join-Path $VagrantSourceDir version.xt
if (-Not (Test-Path $VagrantVersionFile)) {
    "0.1.0" | Out-File -FilePath $VagrantVersionFile
}
$VagrantVersion=$((Get-Content $VagrantVersionFile) -creplace '\.[^0-9]+$')
Write-Host "Vagrant version: $VagrantVersion"

# Install gem. We do this in a sub-shell so we don't have to worry
# about restoring environmental variables.
$env:SubstrateDir     = $SubstrateDir
$env:VagrantSourceDir = $VagrantSourceDir
powershell {
    $ErrorActionPreference = "Stop"

    Set-Location $env:VagrantSourceDir
    $EmbeddedDir  = "$($env:SubstrateDir)\embedded"
    $env:GEM_PATH = "$($EmbeddedDir)\gems"
    $env:GEM_HOME = $env:GEM_PATH
    $env:GEMRC    = "$($EmbeddedDir)\etc\gemrc"
    $env:CPPFLAGS = "-I$($EmbeddedDir)\include"
    $env:LDFLAGS  = "-L$($EmbeddedDir)\lib"
    $env:Path     ="$($EmbeddedDir)\bin;$($env:Path)"
    &"$($EmbeddedDir)\bin\gem.bat" install vagrant.gem --no-ri --no-rdoc
}
Remove-Item Env:SubstrateDir
Remove-Item Env:VagrantSourceDir

#--------------------------------------------------------------------<commit_msg>package: fix version.txt path for Windows<commit_after><#
.SYNOPSIS
    Packages a Vagrant installer from a substrate package.

.DESCRIPTION
    Packages a Vagrant installer from a substrate package.

    This script requires administrative privileges.

    You can run this script from an old-style cmd.exe prompt using the
    following:

      powershell.exe -ExecutionPolicy Unrestricted -NoLogo -NoProfile -Command "& '.\package.ps1'"

.PARAMETER SubstratePath
    Path to the substrate zip file.

.PARAMETER VagrantRevision
    The commit revision of Vagrant to install.
#>
param(
    [Parameter(Mandatory=$true)]
    [string]$SubstratePath,

    [Parameter(Mandatory=$true)]
    [string]$VagrantRevision
)

# Exit if there are any exceptions
$ErrorActionPreference = "Stop"

# Put this in a variable to make things easy later
$UpgradeCode = "1a672674-6722-4e3a-9061-8f539a8b0ed6"

# Get the directory to this script
$Dir = Split-Path $script:MyInvocation.MyCommand.Path

# Lookup the WiX binaries, these will error if they're not on the Path
$WixHeat   = Get-Command heat | Select-Object -ExpandProperty Definition
$WixCandle = Get-Command candle | Select-Object -ExpandProperty Definition
$WixLight  = Get-Command light | Select-Object -ExpandProperty Definition

#--------------------------------------------------------------------
# Helper Functions
#--------------------------------------------------------------------
function Expand-ZipFile($file, $destination) {
    $shell = New-Object -ComObject "Shell.Application"
    $zip = $shell.NameSpace($file)
    foreach($item in $zip.items()) {
        $shell.Namespace($destination).copyhere($item)
    }
}

#--------------------------------------------------------------------
# Extract Substrate
#--------------------------------------------------------------------
# We need the full path to the file
$SubstratePath = Resolve-Path $SubstratePath

# We need to create a temporary configuration directory
$SubstrateTmpDir = [System.IO.Path]::GetTempPath()
$SubstrateTmpDir = [System.IO.Path]::Combine(
    $SubstrateTmpDir, [System.IO.Path]::GetRandomFileName())
[System.IO.Directory]::CreateDirectory($SubstrateTmpDir) | Out-Null
Write-Host "Substrate temp dir: $($SubstrateTmpDir)"

# Unzip
Write-Host "Expanding substrate..."
Expand-ZipFile -file $SubstratePath -destination $SubstrateTmpDir

# Set the full path to the substrate
$SubstrateDir = "$($SubstrateTmpDir)"

#--------------------------------------------------------------------
# Install Vagrant
#--------------------------------------------------------------------
$VagrantTmpDir = [System.IO.Path]::GetTempPath()
$VagrantTmpDir = [System.IO.Path]::Combine(
    $VagrantTmpDir, [System.IO.Path]::GetRandomFileName())
[System.IO.Directory]::CreateDirectory($VagrantTmpDir) | Out-Null
Write-Host "Vagrant temp dir: $($VagrantTmpDir)"

$VagrantSourceURL = "https://github.com/mitchellh/vagrant/archive/$($VagrantRevision).zip"
$VagrantDest      = "$($VagrantTmpDir)\vagrant.zip"

# Download
Write-Host "Downloading Vagrant: $($VagrantRevision)"
$client = New-Object System.Net.WebClient
$client.DownloadFile($VagrantSourceURL, $VagrantDest)

# Unzip
Write-Host "Unzipping Vagrant"
Expand-ZipFile -file $VagrantDest -destination $VagrantTmpDir

# Set the full path to where Vagrant is
$VagrantSourceDir = "$($VagrantTmpDir)\vagrant-$($VagrantRevision)"

# Build gem
Write-Host "Building Vagrant Gem"
Push-Location $VagrantSourceDir
&"$($SubstrateDir)\embedded\bin\gem.bat" build vagrant.gemspec
Copy-Item vagrant-*.gem -Destination vagrant.gem
Pop-Location

# Determine the version
$VagrantVersionFile = Join-Path $VagrantSourceDir version.txt
if (-Not (Test-Path $VagrantVersionFile)) {
    "0.1.0" | Out-File -FilePath $VagrantVersionFile
}
$VagrantVersion=$((Get-Content $VagrantVersionFile) -creplace '\.[^0-9]+$')
Write-Host "Vagrant version: $VagrantVersion"

# Install gem. We do this in a sub-shell so we don't have to worry
# about restoring environmental variables.
$env:SubstrateDir     = $SubstrateDir
$env:VagrantSourceDir = $VagrantSourceDir
powershell {
    $ErrorActionPreference = "Stop"

    Set-Location $env:VagrantSourceDir
    $EmbeddedDir  = "$($env:SubstrateDir)\embedded"
    $env:GEM_PATH = "$($EmbeddedDir)\gems"
    $env:GEM_HOME = $env:GEM_PATH
    $env:GEMRC    = "$($EmbeddedDir)\etc\gemrc"
    $env:CPPFLAGS = "-I$($EmbeddedDir)\include"
    $env:LDFLAGS  = "-L$($EmbeddedDir)\lib"
    $env:Path     ="$($EmbeddedDir)\bin;$($env:Path)"
    &"$($EmbeddedDir)\bin\gem.bat" install vagrant.gem --no-ri --no-rdoc
}
Remove-Item Env:SubstrateDir
Remove-Item Env:VagrantSourceDir

#--------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.21586420412084978, 'fraction_numerical': 0.0064054659976513295, 'mean_word_length': 2.918025930572982, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3470908', 'n_tokens_mistral': 3001, 'n_tokens_neox': 2878, 'n_words': 665}