# The install hook removes the old default gemspecs at deploy time on each machine.
# Remove this hook when updating to a new version of Ruby that includes the fixed gems.
$rubyScript = @'
require "rbconfig"

GEMS_TO_REMOVE = %w{erb zlib}.freeze

default_gemspec_dir = File.join(
  RbConfig::CONFIG["rubylibprefix"],
  "gems",
  RbConfig::CONFIG["ruby_version"],
  "specifications",
  "default"
)

unless File.directory?(default_gemspec_dir)
  puts "  Default gemspec directory not found: #{default_gemspec_dir}"
  exit 0
end

GEMS_TO_REMOVE.each do |gem_name|
  gemspecs = Dir.glob(File.join(default_gemspec_dir, "#{gem_name}-*.gemspec"))
  if gemspecs.empty?
    puts "  No default gemspec found for #{gem_name}"
    next
  end
  gemspecs.each do |gemspec|
    puts "  Removing default gemspec: #{gemspec}"
    File.delete(gemspec)
    puts "  Successfully removed #{File.basename(gemspec)}"
  end
end
'@

$tempFile = [System.IO.Path]::GetTempFileName() + ".rb"
$rubyScript | Set-Content -Path $tempFile -Encoding UTF8
ruby $tempFile
$exitCode = $LASTEXITCODE
Remove-Item $tempFile -ErrorAction SilentlyContinue
exit $exitCode
