require "mkmf"
require "ftools"

dir_config('narray',$sitearchdir,$sitearchdir)
if ( ! ( have_header("narray.h") && have_header("narray_config.h") ) ) then
   print <<-EOS
   ** configure error **  
   Header narray.h or narray_config.h is not found. If you have these files in 
   /narraydir/include, try the following:

   % ruby extconf.rb --with-narray-include=/narraydir/include

   EOS
   exit(-1)
end

if(xsystem("which cdclconfig"))
  cflags = `cdclconfig --cflags`.gsub(/\n/, " ")
  ldflags = `cdclconfig --ldflags`.gsub(/\n/, " ")
  ldlibs = `cdclconfig --ldlibs`.gsub(/-R.*?\s/, " ").gsub(/\n/, " ")
  print "checking DCL version... "
  dclversion = `cdclconfig --dclversion`[0..2]
  print dclversion,"\n"
  case dclversion
  when "5.2","5.3"
    # ok
  else
    raise "unsupported DCL version"
  end
  dclversionmacro = ' -DDCLVER='+dclversion.sub(/\./,'')  # -DDCL52 etc

  $CFLAGS += ' '+cflags  + dclversionmacro
  $LOCAL_LIBS += ' ' + ldflags
  $LOCAL_LIBS += ldlibs

  gtkversion = `cdclconfig --gtkversion`.chop
  if $?.class==Fixnum
    status = $?
  else
    status = $?.exitstatus
  end
  if status != 0
    if ldlibs =~ /gtk/
      gtkversion = "1"
    else
      gtkversion = "no"
    end
  end
  if gtkversion == "no" then
    File.copy("init.c.default","init.c")
  else
    print "**Infomation** compile with gtk+ (ver.#{gtkversion})\n"
    File.copy("grph1_zgpack.c.org","grph1_zgpack.c")
    File.safe_unlink("init.c")
    File.copy("init.c.gtk","init.c")
    if ldlibs =~ /-lgdk_imlib/ then
      print "**Infomation** compile with imlib_gdk\n"
    end
  end

else
  print "**Warning** cdclconfig is not found. Assume C version of DCL 5.2\n"
  gtkversion="no"
  dcllibname = "cdcl52"
  f2clibname = "f2c4dcl"
  xlibname = "X11"
  dcllibdir = "-L/usr/local/lib"
  f2clibdir = "-L/usr/local/lib"
  xlibdir = "-L/usr/local/X11R6/lib"
  f2cincdir = "-I/usr/local/include"
  $CFLAGS += f2cincdir
  if(dcllibdir != f2clibdir)
    libdir = " "+dcllibdir
  else
    libdir = " "+dcllibdir+" "+f2clibdir
  end
  $LOCAL_LIBS += " "+libdir+" "+xlibdir
  $LOCAL_LIBS += " -l"+dcllibname+" -l"+f2clibname+" -l"+xlibname+" -lsocket -lnsl"
end

if /cygwin|mingw/ =~ RUBY_PLATFORM
   have_library("narray") || raise("ERROR: narray import library is not found")
   if gtkversion == "2"
      have_library("ruby-glib2") || 
                 raise("ERROR: ruby-glib2 import library is not found")
   end
end

create_makefile("numru/dcl_raw")

######  Modify Makefile: #######
File.rename("Makefile","Makefile.orig")
oldmkfl = File.open("Makefile.orig")
newmkfl = File.open("Makefile","w")
oldmkfl.each_line{ |line|
   case(line)
   when /^distclean:/
      newmkfl.puts(line)
      newmkfl.puts("\t\t@$(RM) Makefile.orig")
      newmkfl.puts("\t\t@$(RM) init.c grph1_zgpack.c")
   else
      newmkfl.puts(line)
   end
}
newmkfl.close


syntax highlighted by Code2HTML, v. 0.9.1