<commit_before>function log($msg) {
  $timestamp = Get-Date -Format "[yyyy-MM-dd hh:mm:ss]"
  add-content "$($config['CHEF_PS_LOG'])" "$timestamp $msg"
}

function msi_url() {
  $machine_os = $config['CHEF_MACHINE_OS']
  $machine_arch = $config['CHEF_MACHINE_ARCH']
  $download_context = $config['CHEF_DOWNLOAD_CONTEXT']
  $version = $config['CHEF_VERSION']
  $msi_url = $config['CHEF_REMOTE_SOURCE_MSI_URL']

  if ($version -eq $null) { $version = '&v=latest' }

  if ($msi_url.length -gt 4) {
    $msi_url
  } else {
    $url = "https://www.chef.io/chef/download?p=windows"
    if ($machine_os -ne $null) { $url += "&pv=$($machine_os)" }
    if ($machine_arch -ne $null) { $url += "&m=$($machine_arch)" }
    if ($download_context -ne $null) { $url += "&DownloadContext=$($download_context)" }
    $url += $version
    $url
  }
}

function report_status($exitcode) {
  set-content "$($config['CHEF_PS_EXITCODE'])" "$exitcode"
}

function ps_exit() {
  Start-Sleep 1
  while (Test-Path "$($config['CHEF_PS_LOG'])") { Remove-Item "$($config['CHEF_PS_LOG'])" -Force -ErrorAction SilentlyContinue }
  exit 99
}

function cleanup() {
  Remove-Item "$($config['CHEF_LOCAL_MSI_PATH'])" -Force -ErrorAction SilentlyContinue
  Remove-Item "$($config['CHEF_CLIENT_MSI_LOG_PATH'])" -Force -ErrorAction SilentlyContinue
}

$cmd_input_variables = @("CHEF_PS_LOG", "CHEF_PS_EXITCODE", "CHEF_REMOTE_SOURCE_MSI_URL", "CHEF_LOCAL_MSI_PATH", "CHEF_http_proxy","CHEF_CLIENT_MSI_LOG_PATH","CHEF_ENVIRONMENT_OPTION","CHEF_BOOTSTRAP_DIRECTORY","CHEF_CUSTOM_INSTALL_COMMAND","CHEF_EXTRA_MSI_PARAMETERS","CHEF_MACHINE_OS","CHEF_MACHINE_ARCH","CHEF_DOWNLOAD_CONTEXT","CHEF_VERSION")
$config = @{}
$cmd_input_variables | ForEach-Object {
	$config[$_] = (get-childitem env:$_ -ErrorAction SilentlyContinue).value
}
log "`nConfig loaded from environment:$($config | Out-String -Width 150)"

log "Removing bootstrap files left by potential earlier run"
cleanup

log "Setting up Webclient"<commit_msg>Get config (env vars) from registry instead of volatile environment. This is more stable and also compatible with older Windows versions<commit_after>function log($msg) {
  $timestamp = Get-Date -Format "[yyyy-MM-dd hh:mm:ss]"
  add-content "$($config['CHEF_PS_LOG'])" "$timestamp $msg"
}

function msi_url() {
  $machine_os = $config['CHEF_MACHINE_OS']
  $machine_arch = $config['CHEF_MACHINE_ARCH']
  $download_context = $config['CHEF_DOWNLOAD_CONTEXT']
  $version = $config['CHEF_VERSION']
  $msi_url = $config['CHEF_REMOTE_SOURCE_MSI_URL']

  if ($version -eq $null) { $version = '&v=latest' }

  if ($msi_url.length -gt 4) {
    $msi_url
  } else {
    $url = "https://www.chef.io/chef/download?p=windows"
    if ($machine_os -ne $null) { $url += "&pv=$($machine_os)" }
    if ($machine_arch -ne $null) { $url += "&m=$($machine_arch)" }
    if ($download_context -ne $null) { $url += "&DownloadContext=$($download_context)" }
    $url += $version
    $url
  }
}

function report_status($exitcode) {
  set-content "$($config['CHEF_PS_EXITCODE'])" "$exitcode"
}

function ps_exit() {
  Start-Sleep 1
  while (Test-Path "$($config['CHEF_PS_LOG'])") { Remove-Item "$($config['CHEF_PS_LOG'])" -Force -ErrorAction SilentlyContinue }
  exit 99
}

function cleanup() {
  Remove-Item "$($config['CHEF_LOCAL_MSI_PATH'])" -Force -ErrorAction SilentlyContinue
  Remove-Item "$($config['CHEF_CLIENT_MSI_LOG_PATH'])" -Force -ErrorAction SilentlyContinue
}

# Windows 2008 compatible way of loading config:
$shell_variables = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'

$cmd_input_variables = @("CHEF_PS_LOG", "CHEF_PS_EXITCODE", "CHEF_REMOTE_SOURCE_MSI_URL", "CHEF_LOCAL_MSI_PATH", "CHEF_http_proxy","CHEF_CLIENT_MSI_LOG_PATH","CHEF_ENVIRONMENT_OPTION","CHEF_BOOTSTRAP_DIRECTORY","CHEF_CUSTOM_INSTALL_COMMAND","CHEF_EXTRA_MSI_PARAMETERS","CHEF_MACHINE_OS","CHEF_MACHINE_ARCH","CHEF_DOWNLOAD_CONTEXT","CHEF_VERSION")
$cmd_input_variables | ForEach-Object {
  $config[$_] = $shell_variables.$($_)
}
log "`nConfig loaded from environment:$($config | Out-String -Width 150)"

log "Removing bootstrap files left by potential earlier run"
cleanup

log "Setting up Webclient"
{'fraction_non_alphanumeric': 0.16239520958083833, 'fraction_numerical': 0.004311377245508982, 'mean_word_length': 6.138461538461539, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6255990', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1526, 'n_words': 276}