# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;
use ExtUtils::MakeMaker 5.16 qw(WriteMakefile prompt);
use Config;

die "RDFStore needs Perl 5.004_05 or better. This is $]\n" 
    if $] <= 5.00404;

$| = 1;

# check required modules
my %missing_modules=();
print "Checking for URI..............";
eval {
        require URI;
        URI->VERSION(1.09);
	};
if ($@) {
    print " missing - The URI module is required\n";
    $missing_modules{'URI'}=1;
} else {
    print " ok\n";
	};

print "Checking for DBI..............";
eval {
        require DBI;
	};
if ($@) {
    print " missing - You need the DBI module to run RDQL/Squish queries; without it only API based triple-match will be possible\n";
    $missing_modules{'DBI'}=1;
} else {
    print " ok\n";
	};

# Some of this configurations are coming from the DB_File Makefile.PL extension written by Paul Marquess <Paul.Marquess@btinternet.com>
print "Checking for XML::Parser......";
eval {
	require XML::Parser;
	require XML::Parser::Expat;
	XML::Parser->VERSION(2.00);
	};
if ($@) {
    print " missing - XML::Parser module version 2 or higher is required\n";
    $missing_modules{'XML::Parser'}=1;
} else {
    	print " ok\n\n";
	warn qq|WARNING:

	You have installed a recent version of the XML::Parser (>2.29) with built in expat XML parser support. 
	If you are going to use RDFStore inside the Apache Web server using mod_perl, bear in mind that there 
	is a symbol conflict between XML::Parser and apache when built with expat. To properly use the RDFStore 
	parsing modules you need to rebuilt apache disabling expat (use --disable-rule=EXPAT while building your
	apache-1.3.xx + mod_perl)

|
		if($XML::Parser::VERSION gt '2.29');
	};

if(     (exists $missing_modules{'XML::Parser'}) ||
        (exists $missing_modules{'DBI'}) ||
        (exists $missing_modules{'URI'}) ) {
        print "\nSorry, can not build RDFStore :(\nPlease installed the missing modules above and try again.\n\n";
        exit;
	};


my $PLATFORM = " -DRDFSTORE_PLATFORM_" . uc($^O);
my ($LIB_DIR,$INC_DIR,$DB_NAME,$LIBS,$COMPAT185,$DB,$DBMS_INSTDIR);
my %options = map { $_, 1 } qw( INCLUDE LIB DBNAME DBMS_INSTDIR RDFSTORE_WORD_STEMMING RDFSTORE_MAXRECORDS );

# complete the compilation parameters (precedence to %ENV settings)
$INC_DIR = $ENV{'RDFSTORE_BDB_INCLUDE'}
	if(defined $ENV{'RDFSTORE_BDB_INCLUDE'} and -e $ENV{'RDFSTORE_BDB_INCLUDE'} and -d _ );
$INC_DIR = $options{'INCLUDE'}
	if(exists $options{'INCLUDE'} and -e $options{'INCLUDE'} and -d _ );
$LIB_DIR = $ENV{'RDFSTORE_BDB_LIB'}
	if(defined $ENV{'RDFSTORE_BDB_LIB'} and -e $ENV{'RDFSTORE_BDB_LIB'} and -d _ );
$LIB_DIR = $options{'LIB'}
	if(exists $options{'LIB'} and -e $options{'LIB'} and -d _ );
$DB_NAME = $options{'DBNAME'}
	if(exists $options{'DBNAME'} and $options{'DBNAME'} ne '1');
$COMPAT185 = "-DCOMPAT185 -DDB_LIBRARY_COMPATIBILITY_API"
       	if (	(defined $ENV{'RDFSTORE_BDB_COMPAT185'}) &&
		($ENV{'RDFSTORE_BDB_COMPAT185'} =~ /^\s*(on|true|1)\s*$/i) );
if(exists $options{'DBMS_INSTDIR'} and -e $options{'DBMS_INSTDIR'} and -d _ ) {
	$DBMS_INSTDIR = $options{'DBMS_INSTDIR'};
} else {
	$DBMS_INSTDIR = '/RDFStore';
	};

#other specific options either from enviornment or arch_conf.in
my ($RDFSTORE_WORD_STEMMING,$RDFSTORE_MAXRECORDS);
$RDFSTORE_WORD_STEMMING = $ENV{'RDFSTORE_WORD_STEMMING'}
	if(defined $ENV{'RDFSTORE_WORD_STEMMING'});
$RDFSTORE_WORD_STEMMING = $options{'RDFSTORE_WORD_STEMMING'}
	if(exists $options{'RDFSTORE_WORD_STEMMING'});

my $RDFSTORE_DEFAULT_MAXRECORDS=2097152;
my $RDFSTORE_MIN_MAXRECORDS=128;
my $RDFSTORE_MAX_MAXRECORDS=60000000; # 60M triples
$RDFSTORE_MAXRECORDS = (	$ENV{'RDFSTORE_MAXRECORDS'} >= $RDFSTORE_MIN_MAXRECORDS and 
				$ENV{'RDFSTORE_MAXRECORDS'} <= $RDFSTORE_MAX_MAXRECORDS ) ? 
				$ENV{'RDFSTORE_MAXRECORDS'} :
				$RDFSTORE_DEFAULT_MAXRECORDS
	if(defined $ENV{'RDFSTORE_MAXRECORDS'});
$RDFSTORE_MAXRECORDS = (	$options{'RDFSTORE_MAXRECORDS'} >= $RDFSTORE_MIN_MAXRECORDS and 
				$options{'RDFSTORE_MAXRECORDS'} <= $RDFSTORE_MAX_MAXRECORDS ) ? 
				$options{'RDFSTORE_MAXRECORDS'} :
				$RDFSTORE_DEFAULT_MAXRECORDS
	if(exists $options{'RDFSTORE_MAXRECORDS'});


my $debug = ($ENV{RDFSTORE_DEBUG} =~ m/1|yes|on|enable/) ? '-DRDFSTORE_DEBUG' : '';
my $debug_malloc = ($ENV{RDFSTORE_DEBUG_MALLOC} =~ m/1|yes|on|enable/) ? '-DRDFSTORE_DEBUG_MALLOC' : '';
my $dbms_debug = ($ENV{RDFSTORE_DBMS_DEBUG} =~ m/1|yes|on|enable/) ? '-DRDFSTORE_DBMS_DEBUG' : '';
my $dbms_debug_malloc = ($ENV{RDFSTORE_DBMS_DEBUG_MALLOC} =~ m/1|yes|on|enable/) ? '-DRDFSTORE_DBMS_DEBUG_MALLOC' : '';
my $flat_store_debug = ($ENV{RDFSTORE_FLAT_STORE_DEBUG} =~ m/1|yes|on|enable/) ? '-DRDFSTORE_FLAT_STORE_DEBUG' : '';
my $profile = ($ENV{RDFSTORE_PROFILE} =~ m/1|yes|on|enable/) ? 1 : 0;

my $RDFSTORE_OPTIONS = ' -DRDFSTORE_WORD_STEMMING='.$RDFSTORE_WORD_STEMMING
	if(defined $RDFSTORE_WORD_STEMMING);
$RDFSTORE_OPTIONS .= ' -DRDFSTORE_MAXRECORDS='.$RDFSTORE_MAXRECORDS
	if(defined $RDFSTORE_MAXRECORDS);

#print ARCH_CONF "\n# add the include directory of your BDB installation is if not found to the bottom line\n";
#print ARCH_CONF ($INC_DIR) ? 'INCLUDES += -I'.$INC_DIR : '#INCLUDES += -I/usr/local/BerkeleyDB-4.0.14/include';
#print ARCH_CONF "\n\n# add the library directory of your BDB installation is if not found to the bottom line\n";
#print ARCH_CONF 'LIBS_DIR  = -L../libdbms ' . ( ($LIB_DIR) ? ' -L'.$LIB_DIR : ' # -L/usr/local/BerkeleyDB-4.0.14/lib' ) . "\n";
#print ARCH_CONF ( ($LIB_DIR) ? 'BDB_LD_LIBS_DIR  = '.$LIB_DIR : '#BDB_LD_LIBS_DIR = /usr/local/BerkeleyDB-4.0.14/lib' ) . "\n";

#if($^O eq 'darwin') {
#	print ARCH_CONF "UID=root\n";
#	print ARCH_CONF "GID=wheel\n";
#} else {
#	print ARCH_CONF "UID=bin\n";
#	print ARCH_CONF "GID=bin\n";
#}

#print ARCH_CONF "\n\n# uncomment the following line to use BDB 1.85 compatibility code\n";
#print ARCH_CONF ($COMPAT185) ? 'DEFINES  += "'.$COMPAT185.'"' : '#DEFINES += " -DCOMPAT185 -DDB_LIBRARY_COMPATIBILITY_API "';
#print ARCH_CONF "\n";

#if($^O eq 'solaris') {
#	print ARCH_CONF <<EOT;

# for SOLARIS
#LIBS += -lsocket -lnsl

#EOT
#	};

WriteMakefile(
	#DIR	=> [ 'RDFNode' ],
    	NAME      => 'RDFStore',
	VERSION_FROM => 'RDFStore.pm',
	INC             => '-I@ts_db_dir@/include -I@top_srcdir@/include/rdfstore -I@top_srcdir@/include/dbms -I.',
	LIBS            => [ '-L@ts_db_dir@/lib -l@ts_db_lib@' ], 
	XSPROTOARG      => '-noprototypes',
	OPTIMIZE        => '-g',
	DEFINE          => " $PLATFORM -D_NOT_CORE -DSTATIC_BUFF -DSTATIC_CS_BUFF -DSTATIC_SC_BUFF -DDB@ts_db_version@_INCLUDE",
        LDFROM    => q[RDFStore$(OBJ_EXT) rdfstore_log$(OBJ_EXT) rdfstore_compress$(OBJ_EXT) rdfstore_flat_store$(OBJ_EXT) rdfstore_iterator$(OBJ_EXT) rdfstore_serializer$(OBJ_EXT) rdfstore_kernel$(OBJ_EXT) rdfstore_ap_sha1$(OBJ_EXT) rdfstore_digest$(OBJ_EXT) rdfstore_bits$(OBJ_EXT) rdfstore_utf8$(OBJ_EXT) rdfstore_xsd$(OBJ_EXT) sflcomp$(OBJ_EXT) my_compress$(OBJ_EXT) fraenkel_compress$(OBJ_EXT) backend_bdb_store$(OBJ_EXT) backend_dbms_store$(OBJ_EXT) backend_caching_store$(OBJ_EXT)],
        OBJECT    => q[RDFStore$(OBJ_EXT) rdfstore_log$(OBJ_EXT) rdfstore_compress$(OBJ_EXT) rdfstore_flat_store$(OBJ_EXT) rdfstore_iterator$(OBJ_EXT) rdfstore_serializer$(OBJ_EXT) rdfstore_kernel$(OBJ_EXT) rdfstore_ap_sha1$(OBJ_EXT) rdfstore_digest$(OBJ_EXT) rdfstore_bits$(OBJ_EXT) rdfstore_utf8$(OBJ_EXT) rdfstore_xsd$(OBJ_EXT) my_compress$(OBJ_EXT) sflcomp$(OBJ_EXT) fraenkel_compress$(OBJ_EXT) backend_bdb_store$(OBJ_EXT) backend_dbms_store$(OBJ_EXT) backend_caching_store$(OBJ_EXT) ],
    	'clean'     => { FILES => "y.tab.o lex.yy.o *.db" },
	EXE_FILES    => [ "@top_srcdir@/bin/rdfstore/rdf.pl", "@top_srcdir@/bin/rdfstore/rdfquery.pl", "@top_srcdir@/bin/rdfstore/rdfdump.pl" ],
	($] ge '5.005') ? (
                               'AUTHOR' => 'Alberto Reggiori <areggiori@webweaving.org>, Dirk-Willem van Gulik <dirkx@webweaving.org>',
                               'ABSTRACT' => 'Perl/C RDF storage and API',
                           ) : ()
);
