#!/usr/bin/env perl
#
# configure [args ...]
#
# A trivial configure script.
#
# (C) 2002-2022 by Marc Huber <Marc.Huber@web.de>
# All rights reserved.
#
# $Id$
#

use strict;

my $width = `stty size 2>/dev/null`;
if ($?) {
	$width = 80;
} else {
	$width =~ /\d+\s+(\d+)/;
	$width = $1;
}

$width--;

sub wprint($) {
	my $first = 1;
	my @TXT = split(/\n/, $_[0], -1);
	foreach my $txt (@TXT) {
		print "\n" unless defined $first;
		$first = undef;
		my $len = 0;
		my $t = $txt;
		while (length $t > 0) {
			$t =~ /^(\s*)(\S*)(.*)/;
			my $l = length $1;
			if ($len + $l < $width) {
				print $1;
				$len += $l;
			} else {
				print "\n";
				$len = 0;
			}
			$l = length $2;
			if ($len + $l < $width) {
				print $2;
				$len += $l;
			} else {
				print "\n", $2;
				$len = $l;
			}
			$t = $3;
		}
	}
}

print "="x$width . "\n";

my ($sysname, $release, $machine, $cwd);

if (eval("require POSIX")) {
	($sysname, undef, $release, undef, $machine) = POSIX::uname();
	$cwd = POSIX::getcwd();
} else {
	$sysname = `uname -s`;
	$release = `uname -r`;
	$machine = `uname -m`;
	$cwd = `pwd`;
	chomp ($sysname, $release, $machine, $cwd);
}

$sysname =~ tr/ \//--/;
$machine =~ tr/ \//--/;
$release =~ s/\([^)]+\)//; # cygwin ...
$release =~ tr/ \//--/;

$sysname = lc $sysname;
$machine = lc $machine;
$release = lc $release;

my $LIBARCH = undef;

$LIBARCH = "64" if $sysname =~ /linux/i && $machine =~ /64/ && -d "/usr/lib64";

my $aux = "";

my @DIRS_DEFAULT = qw(mavis spawnd mavisd ftpd tac_plus tac_plus-ng tcprelay);
my @ALLDIRS = qw(mavis spawnd mavisd ftpd tac_plus tac_plus-ng tcprelay );
my $DIRS_DEFAULT = join(" ", @DIRS_DEFAULT);
my $ALLDIRS = join(" ", @ALLDIRS);

my @options = qw(pcre2 ssl zlib freeradius-client freeradius pam execinfo sctp curl ipc ares crypto ldap radcli);
my %COMMENT;

my $sbin = "sbin";
my $lib = "lib";
my $libarch = "lib$LIBARCH";
my $doc = "share/mavis";

$COMMENT{"devpoll"} =
"The \"devpoll\" option enables /dev/poll support. You may use it on Linux with /dev/poll patch (untested, and unlikely), or on Sun Solaris (7.0 onwards).
";

$COMMENT{"epoll"} =
"Usually, Linux epoll support is autodetected. This option overrides autodetection, which may be useful if you're using a patched pre-sysepoll kernel (how much unlikely that is).
";

$COMMENT{"ares"} =
"c-ares is a library for asynchronous DNS requests. This option enables ftpd and tac_plus(-ng) to do DNS lookups.\n";

$COMMENT{"pcre2"} =
"ftpd can use the PCRE v2 (\"Perl Compatible Regular Expressions\" v2) library for path rewriting, and tac_plus may use it as an alternative to POSIX regular expression matching. Using it is highly recommended and virtually mandatory for tac_plus-ng.
";

$COMMENT{"freeradius-client"} =
"If you have the freeradius-client library installed, you may authenticate MAVIS requests by using the radmavis binary.
Source is available from https://github.com/FreeRADIUS/freeradius-client
";

$COMMENT{"radcli"} =
"If you have the radcli library installed, you may authenticate MAVIS requests by using the radmavis binary.
";

$COMMENT{"pam"} =
"If you have the pam libraries installed, you may authenticate MAVIS requests by using the pammavis binary.
";

$COMMENT{"curl"} =
"If you have the curl libraries installed, you may use URLs to refer to configuration files.
";

$COMMENT{"ipc"} =
"If SYS-V IPC is available, configuration files may be cached in shared memory.
";

$COMMENT{"crypto"} =
"Use the \"crypto\" option for certain libcrypto support, e.g. DES or SHA1. The ssl option implies this one.
";

$COMMENT{"ssl"} =
"Use the \"ssl\" option if you have the OpenSSL libraries available. If enabled, TLS support is added to FTPd and TcpRelay, and TACACS+ will be able to utilize the crypto library.
";

$COMMENT{"sctp"} =
"Use this option if your system supports SCTP.
";

$COMMENT{"zlib"} =
"The ftp daemon can support transparent gzip-style compression.
";

$COMMENT{"execinfo"} =
"In case of a program crash, execinfo may return a backtrace of the crashed process, which may be valuable for debugging.
";

$COMMENT{"ldap"} =
"This will add \"ldapmavis-mt\" to the build, a pthread() enabled LDAP backend.
";

if ($#ARGV > -1 && $ARGV[0] =~ /^--help$/)
{
	wprint(
"Please note that this program is *not* a GNU configure program generated by autoconf, but a custom Perl script.

Usage: ./configure [args ...] [targets]

Recognized arguments:	Defaults:
  --prefix=DIR		[/usr/local]
  --bindir=DIR		[/usr/local/bin]
  --etcdir=DIR		[/usr/local/etc]
  --sbindir=DIR		[/usr/local/$sbin]
  --libdir=DIR		[/usr/local/$lib]
  --libarchdir=DIR	[/usr/local/$lib$LIBARCH]
  --libexecdir=DIR	[/usr/local/libexec]
  --docdir=DIR		[/usr/local/$doc]
  --installroot=DIR	[]

The C compiler used is usually clang. You may override that choice by specifiying a different one:
  --with-cc=CC
For example, on MacOS X the following creates universal binaries with PPC and x86 code, both 32 and 64 bit:
  ./configure \"--with-cc=gcc -arch ppc64 -arch ppc -arch i386 -arch x86_64\"

$COMMENT{'devpoll'}
  --without-devpoll
  --with-devpoll

$COMMENT{'epoll'}
  --without-epoll
  --with-epoll

$COMMENT{'sctp'}
  --without-sctp
  --with-sctp

");

wprint("The options

  --without-PACKAGE
  --with-PACKAGE\[=DIR]
  --with-PACKAGE-include=DIR
  --with-PACKAGE-lib=DIR

are recognized for the following packages:

");
	foreach my $o (sort @options)
	{
		wprint( "$o: $COMMENT{$o}
");
	}
	wprint (
"Supported targets:
  $ALLDIRS
Default targets:
  $DIRS_DEFAULT

This script should be smart enough to figure out availability of most configuration options by itself, so \"./configure\" without any arguments is sufficient in most cases.

You can easily skip features you don't need, e.g.:

  ./configure --without-curl --without-ssl --without-crypto --without-ares --without-sctp --without-zlib [targets]

or, as a shortcut,

  ./configure --minimum [targets]

"
);

	print "="x$width . "\n";
	exit;
}

my %A;

foreach $a (@options) {
	$A{"--with-$a"} = undef;
}

my $hint = "";
$hint = "(Called without arguments. \"--help\" will display the command line options.)\n" if $#ARGV < 0;

while ($#ARGV > -1) {
	if ($ARGV[0] =~ /^(--[^=]+)=(.*)$/) {
		$A{$1} = $2;
	} elsif ($ARGV[0] =~ /^--([^-]+-[^-]+|debug)$/) {
		$A{$ARGV[0]} = "1";
	} elsif ($ARGV[0] =~ /^--[^-]+-[^-]+-[^-]+$/) {
		$A{$ARGV[0]} = $ARGV[1];
		shift @ARGV;
	} else {
		$A{$ARGV[0]} = undef;
	}

	shift @ARGV;
}

my $a;

my $DIRS = "";

foreach $a (keys %A) {
	if ($a =~ /^--with-([^-]+)-include$/) {
		$A{"--with-$1"} = "1" unless exists $A{"--with-$1"};

		unless (exists $A{"--with-$1-lib"}) {
			$A{"--with-$1-lib"} = $A{"--with-$1-include"};
			$A{"--with-$1-lib"} =~ s/include$/lib/;
		}
	}
	elsif ($a =~ /^--with-([^-]+)-lib$/) {
		$A{"--with-$1"} = "1" unless exists $A{"--with-$1"};

		unless (exists $A{"--with-$1-include"}) {
			$A{"--with-$1-include"} = $A{"--with-$1-lib"};
			$A{"--with-$1-include"} =~ s/lib$/include/;
		}
	} elsif ($a =~ /^--without-([^-]+)$/) {
		$A{"--with-$1"} = "0";
		delete $A{$a};
	} elsif ($a =~ /^mavis$/) {
		$DIRS = "$a$DIRS";
		delete $A{$a};
	} elsif (-d $a) {
		$DIRS .= " $a";
		delete $A{$a};
	}
}

$DIRS = $DIRS_DEFAULT if $DIRS eq "";

if ($DIRS !~ /^mavis /){
	$DIRS = "mavis$DIRS";
}

my %DEFS;

$DEFS{'DIRS'} = $DIRS unless $DIRS eq "";
$DEFS{"DEF"} = "";
$DEFS{"LD"} = "ld";
$DEFS{"BASE"} = $cwd;
$DEFS{"BASE"} = $cwd;

if (exists $A{"--with-devpoll"}) {
	$DEFS{"WITH_DEVPOLL"} =  $A{"--with-devpoll"};
	delete $A{"--with-devpoll"};
}

if (exists $A{"--with-epoll"}) {
	$DEFS{"WITH_EPOLL"} = $A{"--with-epoll"};
	delete $A{"--with-epoll"};
}

if (exists $A{"--debug"}) {
	$DEFS{"DEBUG"} = $A{"--debug"};
	delete $A{"--debug"};
}

if (exists $A{"--ggdb"}) {
	$DEFS{"gdb"} = $A{"--ggdb"};
	delete $A{"--ggdb"};
}

$A{"--prefix"} = "/usr/local" unless exists $A{"--prefix"};
$DEFS{"DEST"} = $A{"--prefix"};

foreach $a ("bin", "sbin", "etc", "libexec", "lib") {
	$A{"--$a"."dir"} = $A{"--prefix"} . "/$a" unless exists $A{"--$a"."dir"};
	$DEFS{uc($a)."DIR_DEST"} = $A{"--$a"."dir"};
	delete $A{"--$a"."dir"};
}

my $LIBARCHDIR_DEST;

foreach $a ("libarch") {
	$A{"--$a"."dir"} = $A{"--prefix"} . "/lib$LIBARCH" unless exists $A{"--$a"."dir"};
	$LIBARCHDIR_DEST = $A{"--$a"."dir"};
	$DEFS{uc($a)."DIR_DEST"} =  $A{"--$a"."dir"};
	delete $A{"--$a"."dir"};
}

foreach $a ("doc") {
	$A{"--$a"."dir"} = $A{"--prefix"} . "/$doc" unless exists $A{"--$a"."dir"};
	$DEFS{uc($a)."DIR_DEST"} = $A{"--$a"."dir"};
	delete $A{"--$a"."dir"};
}

delete $A{"--prefix"};

if (exists $A{"--installroot"}) {
	$DEFS{"INSTALLROOT"} = $A{"--installroot"};
	delete $A{"--installroot"};
}

if (exists $A{"--minimum"} || exists $A{"--minimal"}) {
	$DEFS{"WITH_CURL"} = 0;
	$DEFS{"WITH_TLS"} = 0;
	$DEFS{"WITH_SSL"} = 0;
	$DEFS{"WITH_CRYPTO"} = 0;
	$DEFS{"WITH_ARES"} = 0;
	$DEFS{"WITH_SCTP"} = 0;
	$DEFS{"WITH_ZLIB"} = 0;
	delete $A{"--minimum"};
	delete $A{"--minimal"};
}

foreach $a (@options) {
	if (exists $A{"--with-$a"} && defined $A{"--with-$a"} && $A{"--with-$a"} =~ /^\//) {
		$A{"--with-$a-lib"} = $A{"--with-$a"} . "/lib" unless exists $A{"--with-$a-lib"};
		$A{"--with-$a-include"} = $A{"--with-$a"} . "/include" unless exists $A{"--with-$a-include"};
		$A{"--with-$a"} = 1;
	}
}

foreach $a (@options) {
	if (exists $A{"--with-$a"} && defined $A{"--with-$a"} && $A{"--with-$a"} != 0) {
		$A{"--with-$a-lib"} = "/usr/lib" unless exists $A{"--with-$a-lib"};
		$A{"--with-$a-include"} = "/usr/include" unless exists $A{"--with-$a-include"};
	}
}

foreach $a (@options) {
	if (defined $A{"--with-$a"}) {
		$DEFS{"WITH_" . uc ($a)} = $A{"--with-$a"};

		if (exists $A{"--with-$a"} && defined $A{"--with-$a"} && $A{"--with-$a"} != 0) {
			$DEFS{"LIBDIR_" . uc ($a)} = $A{"--with-$a-lib"};
			delete $A{"--with-$a-lib"};

			$DEFS{"INCDIR_" . uc ($a)} =  $A{"--with-$a-include"};

			delete $A{"--with-$a-include"};
		}
	}

	delete $A{"--with-$a"};
}

$DEFS{"LIBARCH"} = $LIBARCH if defined $LIBARCH;

if (exists $A{"--with-cc"}) {
	$DEFS{"CC"} = $A{"--with-cc"};
	delete $A{"--with-cc"};
}

my $cctest;

$DEFS{"CC"} = $ENV{"CC"} if exists $ENV{"CC"} && !exists $DEFS{"CC"};

unless (exists $DEFS{"CC"}) {
	$cctest = `clang -v 2>&1`;
	$DEFS{"CC"} = "clang" unless $!;
}

unless (exists $DEFS{"CC"}) {
	$cctest = `gcc -v 2>&1`;
	$DEFS{"CC"} = "gcc" unless $!;
}

unless (exists $DEFS{"CC"}) {
	$cctest = `cc -v 2>&1`;
	$DEFS{"CC"} = "cc" unless $!;
}

die "Couldn't detect a C compiler on your system. Aborting right now.\n" unless exists $DEFS{"CC"};

$cctest = `$DEFS{"CC"} -v 2>&1` if $cctest eq "";

my $gcc_multiarch = `$DEFS{"CC"} -print-multiarch 2>/dev/null || echo`;
chomp $gcc_multiarch;
$LIBARCH = "/$gcc_multiarch" if $gcc_multiarch ne "" && -d "/usr/lib/$gcc_multiarch";

my (@rest) = sort keys %A;
if ($#rest > -1) {
	wprint("The following options were not recognized:
\t" . join("\n\t", @rest) . "

Please correct the command line options, then call ./configure again. \"./configure --help\" will give you a list of all available options.
");
	exit(-1);
}

my @OSLEVEL_ARR = split(/[^\d]+/, $release);
push(@OSLEVEL_ARR, 0, 0, 0);
$DEFS{"OSLEVEL"} = sprintf ("0x%.2x%.2x%.4x", @OSLEVEL_ARR);
$DEFS{"OSTYPE"} = $sysname;
$DEFS{"OS"} = "$sysname-$release-$machine";
$DEFS{'DEF'} .= " -DOSTYPE=$DEFS{'OSTYPE'} -DOSLEVEL=$DEFS{'OSLEVEL'} -DOS=" . '"\\"' . $DEFS{'OS'} . '\\""';

if ($cctest =~ /clang/) {
	$DEFS{"CC"} = "clang";
	$DEFS{"CC_GCC"} = 1;
	$DEFS{"CC_CLANG"} = 1;
} elsif ($cctest =~ /gcc/) {
	$DEFS{"CC"} = "gcc";
	$DEFS{"CC_GCC"} = 1;
} elsif ($cctest =~ /Sunc C/) {
	$DEFS{"CC_SUN"} = 1;
}	

if (defined $DEFS{"CC_GCC"}) {
	$DEFS{"CFLAGS"} = "-Wall -W -Wno-strict-prototypes -Wno-implicit-fallthrough";
	$DEFS{"CFLAGS"} .= " -O2" if exists $DEFS{"CC_CLANG"} && !exists $DEFS{'DEBUG'} && !exists $DEFS{'GGDB'};
	$DEFS{"CFLAGS_PIC"} = "-fPIC";
	$DEFS{"LDOPT_R"} = "-Wl,-rpath,";
	$DEFS{"LD_SHARED"} = "$DEFS{'CC'} -shared";

	$DEFS{"CFLAGS"} .= " -Werror -Wno-deprecated-declarations -Wno-unused-result" if exists $DEFS{'DEBUG'} || exists $DEFS{'GGDB'};
}

if (defined $DEFS{"CC_SUN"}) {
	$DEFS{"CFLAGS"} = "-O";
	$DEFS{"CFLAGS_PIC"} = "-G";
	$DEFS{"LDOPT_R"} = "-Wl,-rpath,";
	$DEFS{"LD_SHARED"} = "$DEFS{'CC'} $DEFS{'FLAGS_PIC'}";
}


$DEFS{"SHLIB_PRE"} = "lib";
$DEFS{"SHLIB_EXT"} = ".so";

if ($sysname eq "darwin") {
	$DEFS{"LD_SHARED"} = "$DEFS{'CC'} -dynamiclib";
	$DEFS{"SHLIB_EXT"} = ".dylib";
	$DEFS{"LIB_MAVIS"} = "\"$DEFS{'LDOPT_R'}\@loader_path/../lib\"";
}

if ($sysname eq "cygwin_nt") {
	$DEFS{"CFLAGS_PIC"} = "";
	$DEFS{"SHLIB_PRE"} = "cyg";
	$DEFS{"SHLIB_EXT"} = ".dll";
	$DEFS{"EXEC_EXT"} = ".exe";
}

$DEFS{"INSTALL"} = "install -c";

if ($sysname eq "sunos") {
	$DEFS{"LIB_NET"} = " -lnsl -lsocket";
	if (`ld -v 2>&1` =~ /GNU/ != "") {
		$DEFS{'LDOPT_R'} = "-R";
		$DEFS{'LD_SHARED'} = "$DEFS{'LD'} -G -z textwarn";
	}

	unless (defined $DEFS{'CC_GCC'}) {
		$DEFS{"CFLAGS"}	= "-xO3 -xstrconst -xdepend -Xa -errwarn=%all";
		$DEFS{"CFLAGS_PIC"} = "-KPIC";
	}

	$DEFS{"DEF"} .= " -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64";
	$DEFS{"LIB_MAVIS"} = "$DEFS{'LDOPT_R'}\$\$ORIGIN/../lib";

	foreach my $install ("/usr/bin/ginstall", "/usr/ucb/install", "/usr/bin/install") {
		$DEFS{"INSTALL"} = $install if -x $install;
		last;
	}
}

if ($sysname eq "linux") {
	$DEFS{"DEF"} .= " -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64";
	$DEFS{"LIB_MAVIS"} = "$DEFS{'LDOPT_R'}\$\$ORIGIN/../lib$DEFS{'LIBARCH'}"
}

if (exists $DEFS{"DEBUG"}) {
	$DEFS{"DEF"} .= " -DDEBUG";
	$DEFS{"CFLAGS"}	.= " -g -Og";
	$DEFS{"CFLAGS"} .= " -ggdb" if exists $DEFS{"CC_GCC"};
}

if (exists $DEFS{"GGDB"}) {
	$DEFS{"CFLAGS"}	.= " -g -Og";
	$DEFS{"CFLAGS"} .= " -ggdb" if exists $DEFS{"CC_GCC"};
}

$DEFS{"DIR_MAVIS"} = "$cwd/mavis";
$DEFS{"LIB_MAVIS"} .= " -L$cwd/build/$DEFS{'OS'}/mavis $DEFS{'LDOPT_R'}$DEFS{'LIBARCHDIR_DEST'} -lmavis";
$DEFS{"DIR_MISC"} = "$cwd/misc";
$DEFS{"INC"} .= " -I$cwd";

my (%FOUND, %NOT_FOUND);

my @LIB_SEARCH_DIRS;
my $search_dir = `$DEFS{'LD'} --verbose 2>/dev/null | grep SEARCH_DIR`;
if ($? == 0) {
	@LIB_SEARCH_DIRS = split(/; ?/, $search_dir);
	map {s/SEARCH_DIR\("=([^"]+)"\)*/$1/g; } @LIB_SEARCH_DIRS;
	pop @LIB_SEARCH_DIRS;
	$DEFS{'# LIB_SEARCH_DIRS'} = join(':', @LIB_SEARCH_DIRS);
}

my @INC_SEARCH_DIRS;
$search_dir = `$DEFS{'CC'} -E -Wp,-v - </dev/null 2>&1`;
if ($? == 0) {
	foreach my $sd (split(/\n/m, $search_dir)) {
		next unless $sd =~ /^ (\/.*)$/;
		push @INC_SEARCH_DIRS, $1;
	}
	$DEFS{'# INC_SEARCH_DIRS'} = join(':', @INC_SEARCH_DIRS);
}

sub check_for($$$$$) {
	my ($PKG, $pkg, $hdr, $lib, $defs) = @_;

	return if exists $FOUND{"LIB-$PKG"};

	unless (exists $DEFS{"WITH_$PKG"}) {
		my $pc = `pkg-config --exists $pkg 2>/dev/null`;
		if ($? == 0) {
			my $L = `pkg-config --libs-only-L $pkg`;
			my $I = `pkg-config --cflags-only-I $pkg`;
			$DEFS{"LIBDIR_$PKG"} = $1 if (`pkg-config --libs-only-L $pkg` =~ /^-L(.*)$/);
			$DEFS{"INCDIR_$PKG"} = $1 if (`pkg-config --cflags-only-I $pkg` =~ /^-I(.*)$/);
			$DEFS{"WITH_$PKG"} = 1;
		}

		unless (exists $DEFS{"WITH_$PKG"}) {
			my $inc;
			for my $prefix ("/usr", "/usr/local", "/usr/sfw", "/usr/pkg", @INC_SEARCH_DIRS) {
				if (-e "$prefix/include/$hdr") {
					$inc = "$prefix/include";
				} elsif (-e "$prefix/$hdr") {
					$inc = "$prefix";
				} else {
					next;
				}
				my @g = glob("$prefix/$DEFS{'LIBARCH'}/lib$lib$DEFS{'SHLIB_EXT'}*");
				if ($#g > -1) {
					$DEFS{"WITH_$PKG"} = 1;
					$DEFS{"INCDIR_$PKG"} = $inc;
					$DEFS{"LIBDIR_$PKG"} = "$prefix/lib/$DEFS{'LIBARCH'}";
					last;
				}
				@g = glob("$prefix/lib/lib$lib$DEFS{'SHLIB_EXT'}*");
				if ($#g > -1) {
					$DEFS{"WITH_$PKG"} = 1;
					$DEFS{"INCDIR_$PKG"} = $inc;
					$DEFS{"LIBDIR_$PKG"} = "$prefix/lib";
					last;
				}
				@g = glob("$prefix/lib/$machine-linux-gnu/lib$lib$DEFS{'SHLIB_EXT'}*");
				if ($#g > -1) {
					$DEFS{"WITH_$PKG"} = 1;
					$DEFS{"INCDIR_$PKG"} = $inc;
					$DEFS{"LIBDIR_$PKG"} = "$prefix/$machine-linux-gnu/lib";
					last;
				}
			}
			if (defined $inc && !exists $DEFS{"WITH_$PKG"}) {
				for my $prefix (@LIB_SEARCH_DIRS) {
					my @g = glob("$prefix/lib$lib$DEFS{'SHLIB_EXT'}*");
					if ($#g > -1) {
						$DEFS{"WITH_$PKG"} = 1;
						$DEFS{"INCDIR_$PKG"} = $inc;
						$DEFS{"LIBDIR_$PKG"} = $prefix;
						last;
					}
				}
			}
		}
	}
	if (exists $DEFS{"WITH_$PKG"} && $DEFS{"WITH_$PKG"} == 1) {
		$DEFS{"INC_$PKG"} = "-I" . $DEFS{"INCDIR_$PKG"} if exists $DEFS{"INCDIR_$PKG"};
		$DEFS{"LIB_$PKG"} = "";
		$DEFS{"LIB_$PKG"} .= "-L" . $DEFS{"LIBDIR_$PKG"} . " " . $DEFS{'LDOPT_R'} . $DEFS{"LIBDIR_$PKG"} . " " if exists $DEFS{"LIBDIR_$PKG"};
		$DEFS{"LIB_$PKG"} .= "-l$lib" if defined $lib;
		$DEFS{'DEF'} .= " -DWITH_$PKG";
		$DEFS{'DEF'} .= " $defs" if defined $defs;;
		$FOUND{"LIB-$PKG"} = 1;
		delete $NOT_FOUND{"LIB-$PKG"} if exists $NOT_FOUND{"LIB-$PKG"};
	} else {
		$NOT_FOUND{"LIB-$PKG"} = 1;
	}
}

delete $DEFS{"WITH_TLS"} if exists $DEFS{"WITH_SSL"} && exists $DEFS{"WITH_TLS"} &&  $DEFS{"WITH_SSL"} == 1 && $DEFS{"WITH_TLS"} == 1;

check_for("ARES", "libcares", "ares.h", "cares", "-DWITH_DNS");
$DEFS{"LIB_DNS"} = $DEFS{"LIB_ARES"} if exists $DEFS{"LIB_ARES"};
$DEFS{"WITH_DNS"} = 1 if exists $DEFS{"LIB_DNS"};
check_for("SSL", "openssl", "openssl/ssl.h", "ssl", undef) if !exists $DEFS{"WITH_TLS"} || $DEFS{"WITH_TLS"} == 0;
$DEFS{"LIB_SSL"} .= " -lcrypto" if exists $DEFS{"LIB_SSL"} && exists $DEFS{"WITH_CRYPTO"} && $DEFS{"WITH_CRYPTO"} == 0;
check_for("CRYPTO", "openssl", "openssl/ssl.h", "crypto", undef);
check_for("PCRE2", "libpcre2-8", "pcre2.h", "pcre2-8", "-DPCRE2_CODE_UNIT_WIDTH=8");
$DEFS{"INC_PCRE"} = $DEFS{"INC_PCRE2"} if exists $DEFS{"INC_PCRE2"};
$DEFS{"LIB_PCRE"} = $DEFS{"LIB_PCRE2"} if exists $DEFS{"LIB_PCRE2"};
check_for("RADCLI", "radcli", "radcli/radcli.h", "radcli", undef) if !exists $DEFS{"WITH_FREERADIUS_CLIENT"} || $DEFS{"WITH_FREERADIUS_CLIENT"} == 0;
check_for("FREERADIUS_CLIENT", "freeradius", "freeradius-client.h", "freeradius-client", undef) if !exists $DEFS{"WITH_RADCLI"} || $DEFS{"WITH_RADCLI"} == 0;
check_for("FREERADIUS_CLIENT", "freeradius-client", "freeradius-client.h", "freeradius-client", undef) if !exists $DEFS{"WITH_RADCLI"} || $DEFS{"WITH_RADCLI"} == 0;
check_for("ZLIB", "zlib", "zlib.h", "z", undef);
check_for("EXECINFO", "execinfo", "execinfo.h", "execinfo", undef);
if ($sysname eq "linux" && -f "/usr/include/execinfo.h") {
	$DEFS{"WITH_EXECINFO"} = 1;
	delete $NOT_FOUND{"LIB-EXECINFO"};
}
check_for("CRYPT", "crypt", "crypt.h", "crypt", undef);
check_for("CRYPT", "crypt", "unistd.h", "crypt", undef);
check_for("CURL", "libcurl", "curl/curl.h", "curl", undef);
check_for("PAM", "libpam", "security/pam_appl.h", "pam", "-DHAVE_SECURITY_PAM_APPL_H");
check_for("PAM", "libpam", "pam/pam_appl.h", "pam", undef);
check_for("SCTP", "sctp", "netinet/sctp.h", "sctp", undef);
check_for("PTHREAD", "pthread", "pthread.h", "pthread", undef);
check_for("LDAP", "ldap", "ldap.h", "ldap", undef);
check_for("LBER", "lber", "lber.h", "lber", undef);

$DEFS{"LIB_DL"} = "-ldl" if $sysname eq "linux";

if ($sysname eq "sunos") {
	$DEFS{"LIB_DL"} = "-ldl";
	$DEFS{"WITH_PORT"} = 1 if !exists $DEFS{"WITH_PORT"} && -e "/usr/include/port.h";
	$DEFS{"WITH_DEVPOLL"} = 1 if !exists $DEFS{"WITH_DEVPOLL"} && -e "/usr/include/sys/devpoll.h";
}

my $USRINCLUDESYS = "/usr/include/sys";
$USRINCLUDESYS = "/usr/include/$machine-linux-gnu/sys" if -d "/usr/include/$machine-linux-gnu/sys";
$DEFS{"WITH_IPC"} = 1 if -f "/usr/include/sys/ipc.h";
$DEFS{"WITH_IPC"} = 1 if -f "$USRINCLUDESYS/ipc.h";

my $mech_found;

if (!exists $DEFS{"WITH_POLL"} && -e "/usr/include/poll.h") {
	$DEFS{"WITH_POLL"} = 1;
	$mech_found = 1;
}
if (!exists $DEFS{"WITH_POLL"} && -e "$USRINCLUDESYS/poll.h") {
	$DEFS{"WITH_POLL"} = 1;
	$DEFS{"WITH_SYSPOLL"} = 1;
	$mech_found = 1;
}
if (!exists $DEFS{"WITH_EPOLL"} && -e "$USRINCLUDESYS/epoll.h") {
	$DEFS{"WITH_EPOLL"} = 1;
	$mech_found = 1;
}
if (!exists $DEFS{"WITH_SELECT"} && -e "$USRINCLUDESYS/select.h") {
	$DEFS{"WITH_SELECT"} = 1;
	$mech_found = 1;
}
if (!exists $DEFS{"WITH_KQUEUE"} && -e "$USRINCLUDESYS/event.h") {
	$DEFS{"WITH_KQUEUE"} = 1;
	$mech_found = 1;
}

unless ($mech_found) {
	wprint "None of the supported event polling mechanisms seems to be available on your\nsystem. Check your installation, some include files may be missing.\n";
	exit(-1);
}

foreach my $m ("PORT", "DEVPOLL", "EPOLL", "KQUEUE", "POLL", "SELECT", "IPC") {
	$DEFS{"DEF"} .= " -DWITH_$m" if exists $DEFS{"WITH_$m"} && $DEFS{"WITH_$m"} == 1;
}

$DEFS{'# DEV FILES FOUND'} = join(" ", sort keys %FOUND ) if int keys %FOUND > 0;
$DEFS{'# DEV FILES NOT FOUND'} = join(" ", sort keys %NOT_FOUND ) if int keys %NOT_FOUND > 0;

my $content = "";
foreach my $k (sort keys %DEFS) {
	$content .= "$k=$DEFS{$k}\n";
}

my $OUT;

my $what = "just created";

mkdir "build", 0755;

my $out = "build/Makefile.inc." . $DEFS{"OS"};
my $oldcontent = "";
if (-f $out) {
	my $IN;
	$/ = undef;
	open IN, "<$out";
	$oldcontent = <IN>;
	close IN;
	$oldcontent =~ s/^#[^\n]*\n//s;
}
if ($oldcontent eq $content) {
	$what = "left unchanged";
} else {
	rename $out, "$out.bak";
	open OUT, ">$out" or die "open ($out): $!";
	print OUT "# Generated by configure at " . localtime () . "\n";
	print OUT $content;
	close OUT;
	system("make clean 2>/dev/null 1>&2") if $oldcontent ne "";
}


wprint $hint . "\n" if $hint ne "";;

wprint "Development files were not found for: " . join(", ", sort keys %NOT_FOUND) . "\n\n" if 0 < int keys %NOT_FOUND;
wprint "Development files were found for: " . join(", ", sort keys %FOUND) . "\n\n" if 0 < int keys %FOUND;

wprint "The file

    $out

was $what. You may run \"make\" now. After that, you may wish to do a \"make install\". Alternatively, you'll find the compiled binaries (plus some ancillary scripts) in the

    build/$DEFS{'OS'}/fakeroot/

directory structure. It's probably advisable to have a look there in any case, as you may or may not like the particular file system layout, and this will give you a chance to see it before installing.

Please direct support requests to the \"Event-Driven Servers\" Google Group at

    event-driven-servers\@googlegroups.com
    https://groups.google.com/group/event-driven-servers

or file an issue at

    https://github.com/MarcJHuber/event-driven-servers

Support requests sent to the author's private email address may be silently ignored.
";

$out = "spawnd/perl/Makefile.PL";
open OUT, ">$out" or die "open ($out): $!";
print OUT <<EOT;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
   NAME		=> 'Scm',
   VERSION_FROM => 'Scm.pm',
   INC		=> '-I../..',
   LIBS		=> '-Wl,-rpath,$LIBARCHDIR_DEST -L$LIBARCHDIR_DEST -lmavis',
   OBJECT	=> 'Scm.o',
);
EOT
close OUT;

unless (-d ".git/refs/heads") {
my $no_git = "GIT info is unavailable. This build might be outdated. Please make sure to update to the latest GIT version before issuing a support request.";
	wprint ("

$no_git
"
	);
	mkdir(".git");
	open OUT, ">>.git/index"; # GNUmakefile refers this. Just touch, dont overwrite.
	close OUT;
	$out = "misc/version.h";
	open OUT, ">$out" or die "open ($out): $!";
	print OUT "#define VERSION \"$no_git\"\n";
	close OUT;
}

print "="x$width . "\n";
# vim: ts=4
