#
# Building Makefile for Embperl
#
# (C) 1997 G.Richter (richter@dev.ecos.de) / ECOS
#
# Note: This is a quick hack, hope it works...
#       Still beta no warrenty!
#


use ExtUtils::MakeMaker;
use Cwd ;


sub MY::test_via_harness 
	{
	my ($txt) = shift -> MM::test_via_harness (@_) ;
	$txt =~ s/PERL_DL_NONLAZY=1/PERL_DL_NONLAZY=0/ ;
	#$txt =~ s/\$\(FULLPERL\)/\$\(FULLPERL\) \-T / ;
	return $txt ;
	}

sub MY::test_via_script 
	{
	my ($txt) = shift -> MM::test_via_script (@_) ;
	$txt =~ s/PERL_DL_NONLAZY=1/PERL_DL_NONLAZY=0/ ;
	#$txt =~ s/\$\(FULLPERL\)/\$\(FULLPERL\) \-T / ;
	return $txt ;
	}
	

## ----------------------------------------------------------------------------

sub GetString
	{
	my ($prompt, $default) = @_ ;

	printf ("%s [%s]", $prompt, $default) ;
	chop ($_ = <STDIN>) ;
	if (!/^\s*$/)
	    {return $_ ;}
	else
    	{
        if ($_ eq "")
	        {return $default ;}
	    else
            { return "" ; }
    
        }
    }

## ----------------------------------------------------------------------------

sub GetYesNo
	{
	my ($prompt, $default) = @_ ;
	my ($value) ;

	do
	    {
	    $value = lc (GetString ($prompt . "(y/n)", ($default?"y":"n"))) ;
	    }
	until (($value cmp "j") == 0 || ($value cmp "y") == 0 || ($value cmp "n" ) == 0) ;

	return ($value cmp "n") != 0 ;
	}

## ----------------------------------------------------------------------------

#
# Check if known config
#

eval 'use Apache::MyConfig' ;

if ($@ eq '')
    { 
    $apache_src = $Apache::MyConfig::Setup{Apache_Src} ; 
    }
else
    {
    $apache_src = '' ;
    }

$base = '..' ;

$b = 0 ;


$apache = GetYesNo ("Build with support for Apache mod_perl?", 'y') ;

if ($apache && $apache_src ne '')
    {
	if (-e "$apache_src/httpd.h")
		{
		$b = GetYesNo ("Use $apache_src as Apache source", 'y') ;
		}
    }

while ($apache && !$b)
    {
    print "Searching for Apache sources...\n" ;
    foreach $src_dir (<$base/apache_1.1*/src>,
                      <$base/apache_1.2*/src>,
		      <$base/apache*/src>,
		      <$base/src>,
		      <./src>,
		      <$base>) 
       {
	print "Look at $src_dir\n" ;
	
	if (-e "$src_dir/httpd.h")
		{
		$b = GetYesNo ("Use $src_dir as Apache source", 'y') ;
		if ($b)
			{
			$apache_src = $src_dir ;
			last ;
			}
		}
	}		
    if (!$b)
    	{
    	$base = GetString ("Apache source not found, enter path name or q to quit", '') ;
    	if ($base eq 'q')
    		{
    		$apache = 0 ;
    		}
    	
    	}
    	

		
    }

if ($b && $apache)
	{
	print "Will use $apache_src for Apache Headers\n" ;
	$i = "-I$apache_src -I$apache_src/regex" ;
	$d = "-DAPACHE" ;
	}
else
	{
	$apache = 0 ;
	print "Will build without mod_perl support\n" ;
	$i = '' ;
	$d = '' ;

	}
		
#
# Check to see which user to use for httpd tests
#

$EPPATH  = cwd ;

if ($b && $apache)
	{
    $EPPORT  = 8529 ;
    $EPUSER  = getpwuid($>) ||  $> ;
    $EPGROUP = getgrgid($)) || $) ;
    $EPHTTPD = "$apache_src/httpd" ;
    if ($EPUSER eq 'root')
        {
        my $nobody = (getpwnam('nobody'))[0] ;
        $EPUSER = $nobody if $nobody ;
        }

    if ($EPUSER eq 'root')
        {
        print "Cannot run test httpd as User $EPUSER\n" ;
        $EPUSER = GetString ("User to run httpd", 'nobody') ;
        $EPGROUP = GetString ("Group to run httpd", $EPGROUP) ;
        }

    if (!-e $EPHTTPD)
        {
        $EPHTTPD = GetString ("Enter file to start as httpd", "$EPHTTPD") ;
        }

    print "Test start $EPHTTPD\n" ;
    print "Test httpd will run as user $EPUSER and group $EPGROUP\n" ;
    print "Test httpd will listen on port $EPPORT\n" ;
    open FH, ">$EPPATH/test/conf/config.pl" or die "Cannot open $EPPATH/test/conf/config.pl" ;
    print FH "# This file is automaticly generated by Makefile.PL, do not edit\n" ;
    print FH "\$EPPATH='$EPPATH' ;\n" ;
    print FH "\$EPUSER='$EPUSER' ;\n" ;
    print FH "\$EPGROUP='$EPGROUP' ;\n" ;
    print FH "\$EPPORT='$EPPORT' ;\n" ;
    print FH "\$EPHTTPD='$EPHTTPD' ;\n" ;
    close FH ;
    }
else
    {
    open FH, ">$EPPATH/test/conf/config.pl" or die "Cannot open $EPPATH/test/conf/config.pl" ;
    print FH "# This file is automaticly generated by Makefile.PL, do not edit\n" ;
    print FH "\$EPPATH='$EPPATH' ; \n" ;
    print FH "\$EPHTTPD='' ;\n" ;
    close FH ;
    }

WriteMakefile(
    'NAME'	   => 'HTML::Embperl',
    'VERSION_FROM' => 'Embperl.pm', 		# finds $VERSION
    'MYEXTLIB'     => 'epmain.o epio.o epchar.o epcmd.o eputil.o epeval.o',
    'LIBS'	   => [''],   			 
    'DEFINE'	   => "$d \$(DEFS)", 			 
    'INC'	   => $i,     			 
    'dist'         => { COMPRESS => 'gzip', SUFFIX => 'gz'},
);
