# libguestfs Ruby bindings -*- ruby -*-
# @configure_input@
# Copyright (C) 2009 Red Hat Inc.
#
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.

require 'rake/clean'
require 'rake/testtask'

# Used to be rake/rdoctask.  Now it's rdoc/task.
# http://stackoverflow.com/questions/2460891/how-do-i-rescue-from-a-require-no-such-file-to-load-in-ruby
begin
  require 'rdoc/task'
rescue LoadError
  require 'rake/rdoctask'
end

# Used to be rake/gempackagetask.  Now it's rubygems/package_task.  Also
# we need to use the appropriate class name below.
begin
  require 'rubygems/package_task'
  gempackagetask='Gem::PackageTask'
rescue
  require 'rake/gempackagetask'
  gempackagetask='Rake::GemPackageTask'
end

PKG_NAME='@PACKAGE_NAME@'
PKG_VERSION='@PACKAGE_VERSION@'

DLEXT=RbConfig::CONFIG['DLEXT']

EXT_CONF='@abs_builddir@/ext/guestfs/extconf.rb'
MAKEFILE='@builddir@/ext/guestfs/Makefile'
GUESTFS_MODULE="@builddir@/ext/guestfs/_guestfs.#{DLEXT}"
GUESTFS_SRCS=[ '@srcdir@/ext/guestfs/handle.c',
               '@srcdir@/ext/guestfs/module.c',
               '@srcdir@/ext/guestfs/actions.h',
               '@srcdir@/ext/guestfs/actions-0.c',
               '@srcdir@/ext/guestfs/actions-1.c',
               '@srcdir@/ext/guestfs/actions-2.c',
               '@srcdir@/ext/guestfs/actions-3.c',
               '@srcdir@/ext/guestfs/actions-4.c',
               '@srcdir@/ext/guestfs/actions-5.c',
               '@srcdir@/ext/guestfs/actions-6.c' ]

CLEAN.include [ "@builddir@/ext/**/*.o", GUESTFS_MODULE,
                "@builddir@/ext/**/depend" ]

CLOBBER.include [ "@builddir@/config.save", "@builddir@/ext/**/mkmf.log",
                  MAKEFILE ]

# Build locally

file MAKEFILE => EXT_CONF do |t|
     unless sh "top_srcdir=$(pwd)/@top_srcdir@; top_builddir=$(pwd)/@top_builddir@; export ARCHFLAGS=\"-arch $(uname -m)\"; mkdir -p @builddir@/ext/guestfs; cd @builddir@/ext/guestfs; @RUBY@ #{EXT_CONF} --with-_guestfs-include=$top_srcdir/common/utils:$top_srcdir/lib:$top_builddir --with-_guestfs-lib=$top_builddir/lib/.libs"
         $stderr.puts "Failed to run extconf"
         break
     end
end
file GUESTFS_MODULE => [ MAKEFILE ] + GUESTFS_SRCS do |t|
    Dir::chdir("@builddir@/ext/guestfs") do
         unless sh "make"
             $stderr.puts "make failed"
             break
         end
     end
end
desc "Build the native library"
task :build => GUESTFS_MODULE

Rake::TestTask.new(:test) do |t|
    t.test_files = FileList['t/tc_*.rb']
    t.libs = [ 'lib', 'ext/guestfs' ]
end
task :test => :build

RDOC_FILES = FileList[
    "@srcdir@/README.rdoc",
    "@srcdir@/lib/**/*.rb",
    "@srcdir@/ext/**/*.[ch]"
]

Rake::RDocTask.new do |rd|
    rd.main = "@srcdir@/README.rdoc"
    rd.rdoc_dir = "doc/site/api"
    rd.rdoc_files.include(RDOC_FILES)
end

Rake::RDocTask.new(:ri) do |rd|
    rd.main = "@srcdir@/README.rdoc"
    rd.rdoc_dir = "doc/ri"
    rd.options << "--ri-system"
    rd.rdoc_files.include(RDOC_FILES)
end

# Package tasks

PKG_FILES = FileList[
  "Rakefile", "COPYING", "README", "NEWS", "@srcdir@/README.rdoc",
  "lib/**/*.rb",
  "ext/**/*.[ch]", "ext/**/MANIFEST", "ext/**/extconf.rb",
  "t/**/*",
  "spec/**/*"
]

DIST_FILES = FileList[
  "pkg/*.src.rpm",  "pkg/*.gem",  "pkg/*.zip", "pkg/*.tgz"
]

SPEC = Gem::Specification.new do |s|
    s.name = PKG_NAME
    s.version = PKG_VERSION
    s.email = "rjones@redhat.com"
    s.homepage = "http://libguestfs.org/"
    s.summary = "Ruby bindings for libguestfs"
    s.files = PKG_FILES
    s.autorequire = "guestfs"
    s.required_ruby_version = '>= 1.8.1'
    s.extensions = "ext/guestfs/extconf.rb"
    s.description = <<EOF
Ruby bindings for libguestfs.
EOF
end

eval(gempackagetask).new(SPEC) do |pkg|
    pkg.need_tar = true
    pkg.need_zip = true
end
