#!/usr/bin/env ruby

if $0 == __FILE__
  file = ARGV.shift
  ary = File.basename(file).split(".")
  outfile = ary[0] + ".rb"
  flag = -1
  File.open(outfile, "w") do |f|
    f.puts("#!/usr/bin/env ruby")
    f.puts("require\(\"gsl\"\)")
    f.puts("require\(\"../gsl_test2.rb\"\)")
    f.puts("require\(\"./gsl_test_sf.rb\"\)")
    f.puts("include GSL::Test")
    f.puts("include Math")
    f.puts("include GSL::Test::Sf")
    f.puts("s = 0")
    f.puts("m = GSL::MODE_DEFAULT")
    f.puts("mode = GSL::MODE_DEFAULT")
    f.puts("GSL::set_error_handler_off")
    File.foreach(file) do |line|
      line.chomp!
      flag = -1
      if /^\s*TEST_SF\(/ =~ line
        if /\);\s*$/ =~ line
          testsf = line
          flag = 0
        else
          save = line
          flag = 1
        end
      else
        if flag == 1
          testsf = save + line
          flag = 0
        else
          flag = -1
        end
      end

      if /(x = \d+)/ =~ line
        f.puts($1)
      end
      if flag == 0

        testsf.gsub!("gsl_sf_", "\"GSL::Sf::")
        testsf.gsub!("GSL_", "GSL::")
        testsf.gsub!(/,\s*\(/, "\", \"\(")
        testsf.gsub!(/,\s*\&r\),/, ")\",")
        testsf.gsub!(";", "")
        testsf.gsub!(/,\s*m\)/, ", GSL::MODE_DEFAULT)")
        testsf.gsub!("exp", "eepp")
        /(\"\(.+\)\")/ =~ testsf
        a = $`
        c = $'
        b =  $1.gsub("x", "\#{x}")
        b.gsub!("mode", "\#{mode}")
        testsf = a + b + c
        testsf.gsub!("eepp", "exp")
#        p testsf
        f.puts(testsf)
      end
    end
  end
end
