The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
# @(#) $Id: Makefile.PL,v 1.9 2007/10/17 14:06:22 xueron Exp $

use 5.006;
use ExtUtils::MakeMaker;
use Config;
use File::Spec;

my %config;
my $ok;
my $devnull = File::Spec->devnull();

###############################################################################
# Read settings from the commandline
# We must delete the options we're handling ourselves to keep
# MakeMaker from processing them, but the rest should be preserved so
# that we get the default MakeMaker behavior.

my $i = 0;

while ($i <= $#ARGV)
{
   my ($key, $val) = split(/=/, $ARGV[$i], 2);
   $config{$key} = $val;

   if ($key eq 'LIBS' || $key eq 'INC')
   {
      delete $ARGV[$i];
   }

   $i++;
}

###############################################################################
# Check for libscws

if ($config{LIBS} or $config{INC})
{
   print "Your settings:\n",
       "  LIBS: ", $config{LIBS}, "\n", "  INC:  ", $config{INC}, "\n";
}

print 'Checking for libscws ... ';

if (linktest($config{LIBS}, $config{INC}))
{
   $ok = 1;
   print "ok (scws apparently in libc)\n";
} else {
   if ($config{LIBS} !~ /-lscws/)
   {
      $config{LIBS} .= ' -lscws';
   }

   if ($config{LIBS} !~ /-lm/) 
   {
      $config{LIBS} .= ' -lm';
   }

   if (linktest($config{LIBS}, $config{INC}))
   {
      $ok = 1;
      print "ok (checked -lscws and -lm)\n";
   }
}

if ($ok)
{
   print <<EOT;

NOTE: If you have multiple libscws implementations installed, you might
      want to make sure that I've found the one you want to use.
      If necessary, you can explicitly specify paths like this:

      $^X Makefile.PL LIBS='-L/path/to/lib' INC='-I/path/to/include'

EOT
}
else
{
   print "fail\n";

   print "Failed to find libscws, please check your settings and re-run as:\n";
   print "$^X Makefile.PL LIBS='-L/path/to/lib' INC='-I/path/to/include'\n";
   exit 1;
}

###############################################################################
# Write the makefile

WriteMakefile(
	      'NAME'              => 'Text::Scws',
	      'VERSION_FROM'      => 'Scws.pm', # finds $VERSION
	      'PREREQ_PM'         => {}, # e.g., Module::Name => 1.1
	      ($] >= 5.005 ?    ## Add these new keywords supported since 5.005
	       (ABSTRACT_FROM => 'Scws.pm', # retrieve abstract from module
		AUTHOR        => 'Xueron Nee <xueron@xueron.com>') : ()),
	      'LIBS'              => $config{LIBS},
	      'DEFINE'            => "@DEFINE",
	      'INC'               => $config{INC},
	      'dist'              => {COMPRESS => 'gzip', SUFFIX => 'gz'},
	     );

###############################################################################

sub linktest
{
   my $libs = shift;
   my $incs = shift;

   my $file = 'linktest';
   my $obj_ext = $Config{_o};

   my $prog = <<EOT;
#include <scws.h>
main()
{
  scws_t s;
  s = scws_new();
  scws_free(s);
  printf("test ok!\\n");
}
EOT

   my $compile;

   unless ($^O eq 'VMS')
   {
      # It is admittedly a bit simplistic to simply concatenate all
      # flags, but it seems to work in most cases.
      $compile = join ' ', $Config{cc}, $incs, $Config{ccflags},
	  $Config{ldflags}, $libs;
   }
   else
   {
      $compile = join ' ', $Config{cc}, $incs, $Config{ccflags}, $libs;
   }

   if (exists $config{verbose})
   {
      print "\nCompiler: '$compile'\n";
   }

   open  LINKTEST, '>', "$file.c" or die "Can't create test file '$file.c'.";
   print LINKTEST $prog;
   close LINKTEST;

   my $compile_line = "$compile -o $file $file.c $libs 2> $devnull";
   #print $compile_line, "\n";

   if ($^O eq 'VMS')
   {
      $compile_line = "pipe $compile $file.c $libs 2> NL:";
   }

   if (exists $config{verbose})
   {
      print "\nCompiler command line: '$compile_line'\n";
   }

   my $result = system($compile_line) / 256;
   unlink $file, "$file.c", "$file$obj_ext";

   if ($result == 0)
   {
      return 1;
   }
   else
   {
      return 0;
   }
}