#!/usr/bin/env ruby

$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))

require 'fileutils'
require 'railsbench/benchmark_specs'

ARGV.each do |file_name|
  backup_file_name = file_name + '~'
  FileUtils.mv(file_name, backup_file_name)
  File.open(backup_file_name) do |infile|
    bench_name = Hash.new{ |hash,key| hash[key] = key }
    File.open(file_name, "w") do |outfile|
      infile.each_line do |line|
        case line
        when /-bm=([^\s]+)/
          # $stderr.puts "-bm=#{$1}"
          outfile.puts(line)
          benchmarks = BenchmarkSpec.load($1)
          benchmarks.each{|spec| bench_name[spec.uri] = spec.name}
        when /^(.*)(\s+[\d\.]+\s+[\d\.]+\s+[\d\.]+\s+\(\s*[\d\.]+\s*\))$/
          l = $1.length
          # $stderr.printf "%-#{l}s%s\n", $1.strip, $2
          outfile.printf "%-#{l}s%s\n", bench_name[$1.strip], $2
        else
          outfile.puts(line)
        end
      end
    end
  end
end

__END__

#  Copyright (C) 2007, 2008  Stefan Kaes
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
