#!/usr/bin/env ruby

require 'optparse'
require 'rbconfig'

command = :run
lib = File.dirname(File.dirname(File.expand_path(__FILE__)))

opts = OptionParser.new do |opts|
  opts.banner = "home_run: Fast Date/DateTime classes for ruby"
  opts.define_head "Usage: home_run (--option | command)"
  opts.separator ""
  opts.separator "Options:"

  opts.on_tail("--install", "install to site_ruby") do
    command = :install
  end

  opts.on_tail("--uninstall", "uninstall from site_ruby") do
    command = :uninstall
  end

  opts.on_tail("--bench", "run comparative benchmarks") do
    command = :bench
  end

  opts.on_tail("--spec", "run the specs (requires mspec)") do
    command = :spec 
  end
end
if ARGV.empty?
  $stderr.puts opts.help
  exit(1)
end
opts.order!

case command
when :bench
  Dir.chdir(lib)
  ENV['RUBY'] = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
  require 'rubygems'
  require 'rake'
  load './Rakefile'
  Rake::Task[:bench_all].invoke
when :spec
  Dir.chdir(lib)
  ENV['PATH'] = [RbConfig::CONFIG['bindir'], ENV['PATH']].join(File::PATH_SEPARATOR)
  ENV['RUBY'] = File.join(RbConfig::CONFIG['ruby_install_name'])
  require 'rubygems'
  require 'rake'
  load './Rakefile'
  Rake::Task[:default].invoke
when :install
  Dir.chdir(lib)
  require 'fileutils'
  FUV = FileUtils::Verbose
  FUV.cp("lib/date.rb", RbConfig::CONFIG['sitelibdir'])
  FUV.mkdir_p(File.join(RbConfig::CONFIG['sitelibdir'], 'date'))
  FUV.cp("lib/date/format.rb", File.join(RbConfig::CONFIG['sitelibdir'], 'date'))
  if File.exists?("lib/date_ext.#{RbConfig::CONFIG['DLEXT']}")
    FUV.cp("lib/date_ext.#{RbConfig::CONFIG['DLEXT']}", RbConfig::CONFIG['sitearchdir'])
  else
    # Windows binary gem files
    if File.exists?('lib/1.8/date_ext.so')
      FUV.mkdir_p(File.join(RbConfig::CONFIG['sitearchdir'], '1.8'))
      FUV.cp("lib/1.8/date_ext.so", File.join(RbConfig::CONFIG['sitearchdir'], '1.8'))
    end
    if File.exists?('lib/1.9/date_ext.so')
      FUV.mkdir_p(File.join(RbConfig::CONFIG['sitearchdir'], '1.9'))
      FUV.cp("lib/1.9/date_ext.so", File.join(RbConfig::CONFIG['sitearchdir'], '1.9'))
    end
  end
when :uninstall
  require 'fileutils'
  FUV = FileUtils::Verbose
  FUV.rm_f(File.join(RbConfig::CONFIG['sitelibdir'], 'date.rb'))
  FUV.rm_f(File.join(RbConfig::CONFIG['sitelibdir'], 'date/format.rb'))
  FUV.rmdir(File.join(RbConfig::CONFIG['sitelibdir'], 'date')) rescue nil
  FUV.rm_f(File.join(RbConfig::CONFIG['sitearchdir'], "date_ext.#{RbConfig::CONFIG['DLEXT']}"))
  FUV.rm_f(File.join(RbConfig::CONFIG['sitearchdir'], '1.8/date_ext.so'))
  FUV.rmdir(File.join(RbConfig::CONFIG['sitearchdir'], '1.8')) rescue nil
  FUV.rm_f(File.join(RbConfig::CONFIG['sitearchdir'], '1.9/date_ext.so'))
  FUV.rmdir(File.join(RbConfig::CONFIG['sitearchdir'], '1.9')) rescue nil
else
  ENV['RUBYOPT'] = "-rdate #{ENV['RUBYOPT']}"
  ENV['RUBYLIB'] = [File.join(lib, 'lib'), File.join(lib, 'ext', 'date_ext'), ENV['RUBYLIB']].join(File::PATH_SEPARATOR)
  exec(*ARGV)
end
