#
# $Id$
#

use 5.008;
use strict;
use warnings;
use ExtUtils::MakeMaker;
use Cwd;
use File::Spec;

# minimum required version of dependencies we need to build
our %build_reqs = (
	'perl-ExtUtils-Depends'   => '0.20',
	'perl-ExtUtils-PkgConfig' => '1.03',
	'perl-Glib'               => '1.04',
	'perl-Gtk2'               => '1.00',
	'perl-Gnome2-Canvas'      => '1.00',
	'perl-Gnome2-VFS'         => '1.00',
	'Gnome'                   => '2.0.0',
	'Bonobo'                  => '2.0.0',
);

our %PREREQ_PM = (
	'Glib'                => $build_reqs{'perl-Glib'},
	'Gtk2'                => $build_reqs{'perl-Gtk2'},
	'Gnome2::Canvas'      => $build_reqs{'perl-Gnome2-Canvas'},
	'Gnome2::VFS'         => $build_reqs{'perl-Gnome2-VFS'},
	'ExtUtils::Depends'   => $build_reqs{'perl-ExtUtils-Depends'},
	'ExtUtils::PkgConfig' => $build_reqs{'perl-ExtUtils-PkgConfig'},
);

my %meta_merge = (
        q(meta-spec)          => {
            version => '2',
            url     => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
        },
        x_deprecated => 1,
        author              =>
            ['Gnome2 Perl Team <gtk-perl-list at gnome dot org>'],
        release_status      => 'stable',
        # valid values: https://metacpan.org/module/CPAN::Meta::Spec#license
        license             => 'lgpl_2_1',
        resources => {
            license     => 'http://www.gnu.org/licenses/lgpl-2.1.html',
            homepage    => 'http://gtk2-perl.sourceforge.net',
            x_MailingList =>
                'https://mail.gnome.org/mailman/listinfo/gtk-perl-list',
            bugtracker  => {
                    web     =>
                    'http://rt.cpan.org/Public/Dist/Display.html?Name=Gnome2',
                    mailto  => 'bug-Gnome2 [at] rt.cpan.org',
            },
            repository  => {
                    url     => 'https://gitlab.gnome.org/GNOME/perl-gnome2.git',
                    type    => 'git',
                    web     => 'https://gitlab.gnome.org/GNOME/perl-gnome2',
            },
        },
        prereqs => {
          configure => {
            requires => {%PREREQ_PM}, # no direct ref for 5.14 compatibility
          },
          test => {
            recommends => {
              'Test::Number::Delta' => 1.0,
            },
          },
        },
        no_index            => {
            file => [qw(inc/MakeHelper.pm)],
        },
);


# Writing a fake Makefile ensures that CPAN will pick up the correct
# dependencies and install them.
unless (eval "use ExtUtils::Depends '$build_reqs{'perl-ExtUtils-Depends'}';"
           . "use ExtUtils::PkgConfig '$build_reqs{'perl-ExtUtils-PkgConfig'}';"
           . "use Glib '$build_reqs{'perl-Glib'}';"
           . "use Gtk2 '$build_reqs{'perl-Gtk2'}';"
           . "use Gnome2::Canvas '$build_reqs{'perl-Gnome2-Canvas'}';"
           . "use Gnome2::VFS '$build_reqs{'perl-Gnome2-VFS'}';"
	   . "use Gtk2::CodeGen;"
           . "use Glib::MakeHelper;"
           . "1") {
   warn "$@\n";
   WriteMakefile(
         PREREQ_FATAL => 1,
         PREREQ_PM    => \%PREREQ_PM,
   );
   exit 1; # not reached
}

# now let's get started on the real work...
# in theory, getting what we need to build libgnomeui should 
# also get everything we need for libgnome.
my %libgnomeui_pkgcfg = ExtUtils::PkgConfig->find ("libgnomeui-2.0 >= $build_reqs{Gnome}");
my %libbonoboui_pkgcfg = ExtUtils::PkgConfig->find("libbonoboui-2.0 >= $build_reqs{Bonobo}");

mkdir 'build', 0777;

our @xs_files = <xs/*.xs>;
our %pod_files = (
	'Gnome2.pm' => '$(INST_MAN3DIR)/Gnome2.$(MAN3EXT)',
	# enums.pod is generated by rules we add to the postamble, below.
	# it is generated straight into blib to avoid dependency problems.
	'$(INST_LIB)/$(FULLEXT)/enums.pod'
	                   => '$(INST_MAN3DIR)/Gnome2::enums.$(MAN3EXT)',
	# we will be installing a whole slew of pod files, generated directly
	# from the xs source -- but to have them installed and manified
	# properly, we have to know their names at Makefile.PL time.
	#   This is a bit of a chicken and egg problem, and this solution
	# basically means that if you create a new package name in the xs
	# code, you'll need to re-run Makefile.PL.  you already need to
	# re-run to pick up new XS files, so that's not such a big deal. 
	Glib::MakeHelper->do_pod_files (@xs_files),
);

#
# autogeneration
#
Gtk2::CodeGen->parse_maps ('gnome2perl');
Gtk2::CodeGen->write_boot;

#
# create version guards for the libs that don't provide them.
#
ExtUtils::PkgConfig->write_version_macros(
	"build/gnome2perl-versions.h",
	'libgnome-2.0' => 'LIBGNOME',
	'libgnomeui-2.0' => 'LIBGNOMEUI',
	'libbonoboui-2.0' => 'LIBBONOBOUI',
);


# now we're ready to start creating the makefile.
# we need to use ExtUtils::Depends to get relevant information out of
# the Glib extension, and to save config information for other modules which
# will chain from this one.

our $gnome2 = ExtUtils::Depends->new ('Gnome2', 'Gnome2::Canvas', 'Gnome2::VFS');
$gnome2->set_inc ($libgnomeui_pkgcfg{cflags} . " " . $libbonoboui_pkgcfg{cflags});
$gnome2->set_libs ($libgnomeui_pkgcfg{libs} . " " . $libbonoboui_pkgcfg{libs});
$gnome2->add_xs (@xs_files);
$gnome2->add_pm ('Gnome2.pm' => '$(INST_LIBDIR)/Gnome2.pm');
$gnome2->add_typemaps (map {File::Spec->catfile(cwd(), $_)} 'build/gnome2perl.typemap', 'gnome.typemap');

$gnome2->install (qw(gnome2perl.h
                     build/gnome2perl-autogen.h
                     build/gnome2perl-versions.h
                     doctypes));
$gnome2->save_config ('build/IFiles.pm');

WriteMakefile(
    NAME          => 'Gnome2',
    VERSION_FROM  => 'Gnome2.pm', # finds $VERSION
    ABSTRACT_FROM => 'Gnome2.pm', # retrieve abstract from module
    XSPROTOARG    => '-noprototypes',
    MAN3PODS      => \%pod_files,
    PREREQ_PM     => \%PREREQ_PM,
    META_MERGE    => \%meta_merge,
    $gnome2->get_makefile_vars,
);

print <<__EON__;
NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE

This module has been deprecated by the Gtk-Perl project.  This means that the
module will no longer be updated with security patches, bug fixes, or when
changes are made in the Perl ABI.  The Git repo for this module has been
archived (made read-only), it will no longer possible to submit new commits to
it.  You are more than welcome to ask about this module on the Gtk-Perl
mailing list, but our priorities going forward will be maintaining Gtk-Perl
modules that are supported and maintained upstream; this module is neither.

Since this module is licensed under the LGPL v2.1, you may also fork this
module, if you wish, but you will need to use a different name for it on CPAN,
and the Gtk-Perl team requests that you use your own resources (mailing list,
Git repos, bug trackers, etc.) to maintain your fork going forward.

* Perl URL: https://gitlab.gnome.org/GNOME/perl-gnome2
* Upstream URL: https://gitlab.gnome.org/Archive/libgnome
* Last upstream version: 2.32.1
* Last upstream release date: 2011-01-31
* Migration path for this module: No upstream replacement

NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE

__EON__

sub MY::postamble {
	return Glib::MakeHelper->postamble_clean ()
	     . Glib::MakeHelper->postamble_docs_full (
			DEPENDS => $main::gnome2,
			DOCTYPES => 'doctypes',
			COPYRIGHT_FROM => 'copyright.pod'
	       )
	     . Glib::MakeHelper->postamble_rpms (
			'GNOME' => $build_reqs{'Gnome'},
			'PERL_EXTUTILS_DEPENDS' =>
				$build_reqs{'perl-ExtUtils-Depends'},
			'PERL_EXTUTILS_PKGCONFIG' =>
				$build_reqs{'perl-ExtUtils-PkgConfig'},
			'PERL_GLIB' => $build_reqs{'perl-Glib'},
			'PERL_GTK' => $build_reqs{'perl-Gtk2'},
			'PERL_GNOME_CANVAS' => $build_reqs{'perl-Gnome2-Canvas'},
			'PERL_GNOME_VFS' => $build_reqs{'perl-Gnome2-VFS'},
		)
          . "
\$(INST_LIB)/\$(FULLEXT)/enums.pod : \$(BLIB_DONE) \$(INST_LIB)/\$(FULLEXT)/ maps podifyenums.pl
	\$(PERLRUNINST) -M\$(NAME) podifyenums.pl \$(NAME) maps > \$@
";
}