# -*- ruby -*-
#
# Copyright (C) 2011-2015  Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

$LOAD_PATH.unshift("./lib")
require 'gnome2/rake/package-task'

bin_dir            = nil
include_dir        = nil
glib2_include_dir  = nil
libffi_lib_dir     = nil

build_host         = nil

package_task = GNOME2::Rake::PackageTask.new do |package|
  bin_dir            = (package.windows.absolute_binary_dir + "bin").to_s
  include_dir        = (package.windows.absolute_binary_dir + "include").to_s
  glib2_include_dir  = File.join(include_dir, "glib-2.0")
  libffi_lib_dir     = (package.windows.absolute_binary_dir + "lib").to_s

  build_host         = package.windows.build_host

  package.summary = "Ruby/GLib2 is a Ruby binding of GLib-2.x."
  package.description = "Ruby/GLib2 is a Ruby binding of GLib-2.x."
  package.dependency.gem.runtime = [["pkg-config", ">= 0"]]
  package.dependency.gem.development = [["test-unit", ">= 2"]]
  package.windows.packages = []
  package.windows.dependencies = []
  package.external_packages = [
    {
      :name => "libiconv",
      :download_site => :gnu,
      :label => "libiconv",
      :version => "1.14",
      :windows => {
        :built_file => "bin/libiconv-2.dll",
      },
    },
    {
      :name => "gettext",
      :download_site => :gnu,
      :label => "gettext-runtime",
      :version => "0.19.7",
      :base_dir_in_package => "gettext-runtime",
      :windows => {
        :built_file => "bin/libintl-8.dll",
      },
    },
    {
      :name => "libffi",
      :download_base_url => "ftp://sourceware.org/pub/libffi",
      :label => "libffi",
      :version => "3.2.1",
      :windows => {
        :built_file => "bin/libffi-6.dll",
      },
    },
    {
      :name => "pcre",
      :download_base_url => "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre",
      :label => "PCRE",
      :version => "8.38",
      :windows => {
        :configure_args => [
          "--enable-unicode-properties",
        ],
        :built_file => "bin/libpcre-1.dll",
      },
    },
    {
      :name => "glib",
      :download_site => :gnome,
      :label => "GLib",
      :version => "2.48.1",
      :compression_method => "xz",
      :windows => {
        :need_autoreconf => true,
        :patches => [
          "glib-2.48.0-add-missing-exeext.diff",
        ],
        :built_file => "bin/libglib-2.0-0.dll",
      },
    },
    {
      :name => "gmp",
      :download_base_url => "ftp://ftp.gmplib.org/pub/gmp-6.1.0",
      :label => "GNU MP",
      :version => "6.1.0",
      :compression_method => "xz",
      :windows => {
        :configure_args => [
          "--disable-static",
          "--enable-shared",
        ],
        :built_file => "bin/libgmp-10.dll",
      },
    },
    {
      :name => "nettle",
      :download_base_url => "http://www.lysator.liu.se/~nisse/archive",
      :label => "Nettle",
      :version => "3.2",
      :windows => {
        :configure_args => [],
        :built_file => "bin/libnettle-6-2.dll",
      },
    },
    {
      :name => "libtasn1",
      :download_site => :gnu,
      :label => "Libtasn1",
      :version => "4.7",
      :windows => {
        :built_file => "bin/libtasn1-6.dll",
      },
    },
    {
      :name => "libidn",
      :download_site => :gnu,
      :label => "Libidn",
      :version => "1.32",
      :windows => {
        :built_file => "bin/libidn-11.dll",
      },
    },
    {
      :name => "p11-kit",
      :download_base_url => "https://p11-glue.freedesktop.org/releases",
      :label => "p11-kit",
      :version => "0.23.2",
      :windows => {
        :built_file => "bin/libp11-kit-0.dll",
      },
    },
    {
      :name => "gnutls",
      :download_base_url => "ftp://ftp.gnutls.org/gcrypt/gnutls/v3.4",
      :label => "GnuTLS",
      :version => "3.4.10",
      :compression_method => "xz",
      :windows => {
        :configure_args => [
          "--disable-cxx",
          "--disable-tools",
          "--disable-doc",
        ],
        :use_cc_environment_variable => false,
        :patches => [
        ],
        :built_file => "bin/libgnutls-30.dll",
      },
    },
    {
      :name => "glib-networking",
      :download_site => :gnome,
      :label => "glib-networking",
      :version => "2.48.2",
      :compression_method => "xz",
      :windows => {
        :configure_args => [
          "--without-libproxy",
          "--without-gnome-proxy",
          "--without-ca-certificates",
        ],
        :built_file => "lib/gio/modules/libgiognutls.dll",
      },
    },
  ]
  package.cross_compiling do |spec|
    if /mingw|mswin/ =~ spec.platform.to_s
      spec.add_runtime_dependency("cairo", ">= 1.12.8")
    end
  end
end
package_task.define

namespace :windows do
  namespace :glib do
    pkg_config_dir = package_task.windows.absolute_binary_dir + "lib/pkgconfig"
    pc_path = pkg_config_dir + "glib-2.0.pc"
    patched_path = pkg_config_dir + "patched"
    file patched_path.to_s do
      original_pc = pc_path.read
      new_pc = original_pc.gsub(/^Cflags:/) do |matched|
        "#{matched} -I${includedir}"
      end
      pc_path.open("w") do |pc_file|
        pc_file.write(new_pc)
      end
      touch(patched_path)
    end

    desc "Add include path for libintl.h"
    task :cross => patched_path.to_s
  end

  namespace :builder do
    task :after => "windows:glib:cross"
  end
end
