#!/usr/bin/env ruby

require "rubygems"

platform = Gem::Platform.new(RUBY_PLATFORM)
arch =
  case platform.cpu.sub(/\Auniversal\./, '')
  when /\Aarm64/ then "arm64" # Apple reports arm64e on M1 macs
  when "x86_64"  then "amd64"
  when "x86"     then "386"
  else raise "Unknown architecture: #{platform.os}"
  end

binary = "lefthook_#{platform.os}_#{arch}/lefthook"
binary = "#{binary}.exe" if platform.os == "windows"

args = $*.map { |x| x.include?(' ') ? "'" + x + "'" : x }
cmd = File.expand_path "#{File.dirname(__FILE__)}/../libexec/#{binary}"

unless File.exists?(cmd)
  raise "Invalid platform. Lefthook wasn't build for #{RUBY_PLATFORM}"
end

puts "#{cmd} #{args.join(' ')}"

pid = spawn("#{cmd} #{args.join(' ')}")
Process.wait(pid)
exit($?.exitstatus)
