# extconf.rb : Configure script for Ruby/PGPLOT
#
#   Copyright (c) 2000,2001 Masahiro TANAKA <masa@ir.isas.ac.jp>
#
#   This program is free software.
#   You can distribute/modify this program
#   under the same terms as Ruby itself.
#   NO WARRANTY.
#
# usage: ruby extconf.rb [configure options]

require "mkmf"

#$DEBUG = true

# configure options:
#  --with-x11-dir=path
#  --with-x11-include=path
#  --with-x11-lib=path
dir_config("x11")

# configure options:
#  --with-pgplot-dir=path
#  --with-pgplot-include=path
#  --with-pgplot-lib=path
dir_config("pgplot")

# Otherwise you can also specify:
#  --with-opt-dir=path
#  --with-opt-include=path
#  --with-opt-lib=path

# Check PGPLOT Header
exit unless have_header("cpgplot.h")

# Check FORTRAN Libraries
#
# SUN WorkShop FORTRAN 77 compiler ver5.0
# configure options: --with-sunws
if with_config("sunws")
  $libs = "-lM77 -lsunmath "+$libs
  exit unless find_library("F77", "f77_init", "/opt/SUNWspro/lib")
  $defs.push "-DSPARC_FORTRAN"
#
# GNU FORTRAN 77 (default)
elsif have_library("g2c")
  $CFLAGS = "-Wall "+$CFLAGS
  $defs.push "-DGNU_FORTRAN"
else
  exit
end

# Check X11 Library
exit unless have_library("X11", "XOpenDisplay")

# Check PGPLOT Library
$libs = append_library($libs, "pgplot")
exit unless find_library( "cpgplot", "cpgbeg", "/usr/lib", 
			  "/usr/local/lib", "/usr/local/pgplot" )

$objs = %w(rb_pgplot.o kwarg.o)

# Generate Makefile
create_makefile("pgplot")



# Check types and create narray_config.h...

def have_type(type, header=nil)
  printf "checking for %s... ", type
  STDOUT.flush
  src = <<"SRC"
#include <ruby.h>
SRC
  unless header.nil?
  src << <<"SRC"
#include <#{header}>
SRC
  end
  r = try_link(src + <<"SRC")
  int main() { return 0; }
  int t() { #{type} a; return 0; }
SRC
  unless r
    print "no\n"
    return false
  end
  $defs.push(format("-DHAVE_%s", type.upcase))
  print "yes\n"
  return true
end

def create_conf_h(file)
  print "creating #{file}\n"
  hfile = open(file, "w")
  for line in $defs
    line =~ /^-D(.*)/
    hfile.printf "#define %s 1\n", $1
  end
  hfile.close
end

$defs=[]
if have_header("sys/types.h")
  header = "sys/types.h"
else
  header = nil
end
have_type("u_int8_t", header)
have_type("int16_t", header)
have_type("int32_t", header)
create_conf_h("narray_config.h")
