# -*- cperl -*-

=head1 Configuration script for automated installation of TrEd dependencies 

This file contains configuration for the ./install script in this
directory.  It lists modules to install. Each module is specified by
its filename without version number and extension. The files are
expected to be found in the same directory as this file and the
install script.

This configuration defines dependencies for TrEd.

To install the modules, run ./install in this directory.

See ./install --help for more information.

=head1 AUTHOR

Petr Pajas <pajas@matfyz.cz>

Copyright (c) 2005 Petr Pajas, All rights reserved.

=cut

#     XML-LibXML-Common-

sub package_dir { 'packages' }

sub perl_modules {
     # Test-Harness-
     # IPC-Run3-
     # Probe-Perl-
     # Test-Script-
  qw(
     ExtUtils-CBuilder-
     ExtUtils-ParseXS-
     Test-Simple-
     Perl-OSType-
     version-
     Module-Metadata-
     Version-Requirements-
     CPAN-Meta-YAML-
     JSON-PP-
     Parse-CPAN-Meta-
     File-Temp-
     CPAN-Meta-
     Module-Build-
     UNIVERSAL-DOES-
     File-Which-
     Text-Iconv-
     Tie-IxHash-
     Tk-
     Syntax-Highlight-Perl-
     Tk-CodeText-
     Compress-Raw-Zlib-
     Compress-Raw-Bzip2-
     IO-Compress-
     XML-NamespaceSupport-
     XML-SAX-Base-
     XML-SAX-
     XML-LibXML-
     XML-LibXSLT-
     XML-CompactTree-
     XML-CompactTree-XS-
     XML-NodeFilter-
     XML-LibXML-Iterator-
     XML-Filter-BufferText-
     XML-SAX-Writer-
     XML-Writer-
     Text-Balanced-
     Parse-RecDescent-
     Graph-
     Graph-ChuLiuEdmonds-
     Graph-Kruskal-
     PerlIO-gzip-
     Archive-Zip-
     Tk-MatchEntry-
     IO-String-
     Class-Inspector-
     File-ShareDir-
     Treex-PML-
     IO-Zlib-
   )

}

#      XML-JHXML-
#
# Will not distribute XML::JHXML with Unix/Linux TrEd anymore due to compilation issues
#

sub package_real_module {
  (
  )
}

sub c_modules {
  qw(libxml2- libxslt-)
}

sub version_from {
  (
    'IO-Compress-' => 'IO::Compress::Base',
  )
}

sub ignore_module_build_fail {
  (
    'XML-JHXML-' => 1,
    'Tk-MatchEntry-' => 1,
  )
}

sub force_build {
  (
   (($^O eq 'darwin') ? ('Tk-' => 1) : ()),
   'XML-LibXML-Common-' => ['libxml2-'],
   'XML-LibXML-' => { 'libxml2-' =>
		      sub {
                        $_[0] eq `$^X -MXML::LibXML -e "print XML::LibXML::LIBXML_DOTTED_VERSION"` ? 0 : 1
		      },
		    },
   'XML-LibXSLT-' => { 'libxslt-' =>
		      sub {
                        $_[0] eq `$^X -MXML::LibXSLT -e "print XML::LibXSLT::LIBXSLT_DOTTED_VERSION"` ? 0 : 1
		      },
		    },
#   'libxslt-' => '--with-libxml-prefix="'.$C_PREFIX.'"'
  )
}

sub module_build_flags {
  if ($^O eq 'darwin') {
    my ($sdk) =
      map { $_->[0] }
      sort { $b->[1]<=>$a->[1] or $b->[2]<=>$a->[2] }
      map { /MacOSX(\d+)\.(\d+)/ ? [$_,$1,$2] : [$_,0,0] }
      grep { -d $_ }
      glob("/Developer/SDKs/MacOSX*.sdk");
    if ($sdk) {
      $extra_opts=" X11INC=$sdk/usr/X11R6/include";
    }
  }
  (
   'Tk-' => 'XFT=1'.$extra_opts,
   'XML-LibXML-' => 'SKIP_SAX_INSTALL=1',
#   'libxslt-' => '--with-libxml-prefix="'.$C_PREFIX.'"'
  )
}

sub module_build_input {
  (
   'XML-SAX-' => 'echo Y', # answer a XML::SAX::Base Makefile.PL question
  )
}

sub patches {
  (
# No longer needed: cannot be apllied to Tk 804.029
#    # Fix Tk on newest Xlib (MouseWheel binding)

#    'Tk-' => ['patches/tk-mousewheel.patch', 'patches/Tk-clipboard.patch'],

   # Don't fail when can't modify global site Perl SAX configuration

   'XML-LibXML-' => 'patches/XML-LibXML-Makefile.patch',
   'XML-LibXSLT-' => 'patches/XML-LibXSLT-Makefile.patch',
  )
}

sub no_test {
  'Tk-' => 2,
  'File-Which-' => 2, # tests require too many modules
  # 'XML-SAX-Writer-' => 1
}

my $platform = $^O;
if ($platform eq 'linux') {
  my $lsb = `lsb_release -i`;
  if ($lsb =~ /ubuntu|debian/i) {
    $platform='debian';
  }
}
sub _dpkg_check {
  my ($pkg)=@_;
  my $status = `LANG=C dpkg-query -s $pkg 2>/dev/null|grep Status `;
  if ($status =~ /installed/ and $status !~ /not-installed/) {
    return 1;
  } else {
    warn "\nMISSING PACKAGE: Please install the $pkg package (sudo apt-get install $pkg)\n";
    warn "To disable this check, set NO_PREREQ=1 environment variable\n";
    return 0;
  }
}
sub _pkg_check {
  my (@packages)=@_;
  my $return = 1;
  for my $package (@packages) {
    if ($platform eq 'debian') {
      $return=0 unless _dpkg_check($package->[0]);
    } else {
      warn "NOTE: Will require $package->[1]; make sure the corresponding package is installed!\n";
    }
  }
  return $return;
}
sub check_prereq {
  return 1 if $ENV{NO_PREREQ};
  (
    'Treex-PML-' => sub {
      return 1 if get_current_version('LWP::UserAgent');
      warn "NOTE: Will require libwww-perl; make sure the corresponding package is installed!\n";
      return 0;
    },
    'XML-LibXML-' => sub {
      my $zlib_present = _pkg_check( 
        ['zlib1g-dev' => 'zlib header files'],
      );
      return 1 if (will_install(C => 'libxml2-') && $zlib_present);
      return 1 if ((`xml2-config --libs --cflags 2>/dev/null` =~ /xml/) && $zlib_present);
      return (_pkg_check(
        ['libxml2-dev' => 'libxml2 header files'],
      ) && $zlib_present);
    },
    'XML-LibXSLT-' => sub {
      return 1 if will_install(C => 'libxslt-');
      return 1 if (`xml2-config --libs --cflags 2>/dev/null` =~ /xml/
		     and
		   `xslt-config --libs --cflags 2>/dev/null` =~ /xslt/);
      return _pkg_check(
	['libxml2-dev' => 'libxml2 header files'],
	['libxslt-dev' => 'libxslt header files'],
       );
    },
    'Tk-' => sub {
      return _pkg_check(
	['libx11-dev' => 'libx11 header files'],
	['libxft-dev' => 'libxft2 header files'],
	['libfontconfig1-dev' => 'libfontconfig1 header files'],
	['libpng12-dev' => 'libpng12 header files'],
       );
    }
   )
}

sub on_fetch_from_CPAN {
  fetch_latest_libxml2();
  fetch_latest_libxslt();
}

=head1 NOTE

This configuration file contains a code able to fetch the latest
libxml2 and libxslt sources from the gnome-xml project homepage.
To do that, issue:

  perl -e "require q(./install.cfg); fetch_latest_libxml2()"

and

  perl -e "require q(./install.cfg); fetch_latest_libxslt()"

=cut


sub fetch_latest_libxml2 {
  my $ftp = "ftp://xmlsoft.org/libxml2/";
  my $listing=`wget -qO- $ftp`;
  my ($latest) = $listing=~/>LATEST_LIBXML2_IS_([^>]*)</;
  print "Latest version if $latest\n";
  if ($latest ne "") {
    if (-f "libxml2-$latest.tar.gz") {
      print "We already have the latest: libxml2-$latest.tar.gz\n";
    } else {
      print "Fetching libxml2-${latest}.tar.gz\n";
      system("wget","${ftp}libxml2-${latest}.tar.gz");
    }
  } else {
    print "Couldn't determine the latest libxml2 version\n";
  }
}

sub fetch_latest_libxslt {
  my $ftp = "ftp://xmlsoft.org/libxslt/";
  my $listing=`wget -qO- $ftp`;
  my ($latest) = $listing=~/>LATEST_LIBXSLT_IS_([^>]*)</;
  print "Latest version if $latest\n";
  if ($latest ne "") {
    if (-f "libxslt-$latest.tar.gz") {
      print "We already have the latest: libxslt-$latest.tar.gz\n";
    } else {
      print "Fetching libxslt-${latest}.tar.gz\n";
      system("wget","${ftp}libxslt-${latest}.tar.gz");
    }
  } else {
    print "Couldn't determine the latest libxslt version\n";
  }
}

1;
