require 'mkmf'

$CFLAGS = "-I../include"

puts("checking ruby version ... #{RUBY_VERSION}")
if RUBY_VERSION > "1.6.8"
  $defs.push("-DRUBY_1_8_LATER")
end

begin
  print("checking gsl version ... ")
  IO.popen("gsl-config --version") do |f|
    ver = f.gets.chomp
    puts(ver)
    $defs.push("-DGSL_VERSION=\\\"#{ver}\\\"")
    if ver >= "1.4.90"
      $defs.push("-DGSL_1_4_9_LATER")
    end
    if ver < "1.4"
    $defs.push("-DGSL_CONST_OLD")
    end
    if ver >= "1.3"
      $defs.push("-DGSL_1_3_LATER")
    end
  end
rescue
  raise("Check the command \"gsl-config\" is in search path.")
end

begin
  print("checking rb-gsl version...")
  IO.popen("cat ../VERSION") do |f|
    ver = f.gets.chomp
    puts(ver)
    $defs.push("-DRUBY_GSL_VERSION=\\\"#{ver}\\\"")
  end
end

narray_config = dir_config("narray")
have_header("narray.h")

#cblas_config = dir_config("cblas")
#atlas_config = dir_config("atlas")

IO.popen("gsl-config --cflags") do |f|
  $CFLAGS += " -Wall " + f.gets.chomp
end

IO.popen("gsl-config --libs") do |f|
  $LOCAL_LIBS += " " + f.gets.chomp
end

File.open("../lib/gsl.rb", "w") do |file|
  if narray_config[0] 
    file.print("require\(\"narray\"\)\nrequire\(\"rb_gsl\"\)\ninclude GSL\n")
  else
    file.print("require\(\"rb_gsl\"\)\ninclude GSL\n")
  end
end

create_makefile("rb_gsl")
