
# OpenFTPD Construction file
# (C) 2001 primemover / team openftpd
# Revision 2001-07-09

use Config;

# openftpd version
$VERSION = "0.31";

Help << 'EOT';
              __
           __/__)
           /./%%%%%    OpenFTPD Contructor
          (_%--0-0)    -------------------
             C _o,     USAGE: cons <target> [<options>]
         ___/ /_
        /\  (_) /\     OPTIONS:
     __/ /  )  )_|__     DEBUG=yes ............ enable debug mode
    | / /)----/    ||    WARNINGS=yes ......... enable all warnings
    |/-/ |__'_)    ||    SITEEXEC=no .......... disable the SITE EXEC command
  __/ / /::[+]|    ||    TRCHECK=yes .......... enable traceroute check
 / ,-\ /     )!    ||    TRBIN=<path> ......... location of traceroute binary
 |/ |__'     . \___||    PORTRANGE=<l>:<h> .... lowest and highest port used
        \^___._|                                for data connections
         \   \ |         DIRCOMPLETION=yes .... allow incomplete directory
          \  | |                                names on cd command
           \ )/          LOGIPS=yes ........... enable ip logging
           / |           ENCRYPT=yes .......... enable userbase encryption
           | |           TLS=yes .............. enable AUTH TLS support
           | |           DELINCOMPLETE=yes..... deletes incomplete files by
	   | |                                  default
           |/|         EXAMPLE:
           :!:#__        cons . DEBUG=yes SITEEXEC=no PORTRANGE=5000:5050
    b'ger /#/\,,,)     
         (_/           Use target "." to make all targets.

EOT

print "Use cons -h to see available options!\n";


# link the build to the src directory
Link 'build' => 'src';
Link 'build/lib' => 'lib';

# compiler
$CC = "gcc";


############################################################################
# check for glib                                                           #
############################################################################

# name of the glib configuration file
if ( -e "/usr/local/bin/glib12-config") {
	# dirty hack for freebsd
	$GLIB_CONFIG = "/usr/local/bin/glib12-config";
}
else { 
	$GLIB_CONFIG = "glib-config";
}

# check if glib-config exists
$rc = system("$GLIB_CONFIG 2>/dev/null");
$rc /= 256;

if ( $rc != 1 ) {
	die "\n" .
	    "  ERROR: glib-config was not found!\n" .
	    "         The library glib is needed to build OpenFTPD.\n".
	    "         Please install glib version 1.2 or higher!\n\n".
	    "         If you installed glib already make sure that the\n".
	    "         location of glib-config is added to \$PATH.\n\n";
}

# check for glib version
$GLIB_VERSION = `$GLIB_CONFIG --version`;
if ($GLIB_VERSION < 1.2) {
	die "\n" .
	    "  ERROR: Your version of glib is too old (< 1.2)!\n".
	    "         Please install a recent version of glib.\n\n";
}

$GLIB_LIBS   = `$GLIB_CONFIG --libs`;
$GLIB_CFLAGS = `$GLIB_CONFIG --cflags`;

chomp($GLIB_LIBS);
chomp($GLIB_CFLAGS);


$OPTION_CFLAGS = "";
$OPTION_LIBS   = "";


############################################################################
# check for perl modules                                                   #
############################################################################

my (@modules)=("Digest::MD5", 
               "Bit::Vector", 
	       "LWP::Simple");

foreach  $_ (@modules) {
  eval "use $_;";
  if ($@) {
    print <<EOM;
  ERROR: You are missing a vital perl module, $_
         The best way to obtain this module would be to use CPAN.
	 
	 # perl -MCPAN -e'install $_'

EOM
    exit 1;
  }
}

############################################################################
# check for TLS mode                                                       #
############################################################################
if ($ARG{TLS} eq "yes") {
	$OPTION_CFLAGS .= "-DTLS ";
  $OPTION_LIBS .= "-lssl -lcrypto ";
}

############################################################################
# check for debug mode                                                     #
############################################################################
if ($ARG{DEBUG} eq "yes") {
	$OPTION_CFLAGS .= "-DDEBUG=1 -ggdb ";
}


############################################################################
# check for all warnings mode                                              #
############################################################################
if ($ARG{WARNINGS} eq "yes") {
	$OPTION_CFLAGS .= "-Wall ";
}

############################################################################
# log ip addresses ?                                                       #
############################################################################
if ($ARG{'LOGIPS'} eq "no") {
	$OPTION_CFLAGS .= "-DPARANOIA ";
}


############################################################################
# disable site exec ?                                                      #
############################################################################
if ($ARG{'SITEEXEC'} eq "no") {
	$OPTION_CFLAGS .= "-DDISABLE_EXEC ";
}


############################################################################
# experimental trial mode                                                  #
############################################################################
if ($ARG{TRIALMODE} eq "yes") {
	$OPTION_CFLAGS .= "-DTRIALMODE ";
}

############################################################################
# encrypt userfile ?                                                       #
############################################################################
if ($ARG{ENCRYPT} eq "yes") {
	$OPTION_CFLAGS .= "-DDES_ENCRYPTION -Ilib/ ";
	$OPTION_LIBS .= "build/lib/libdes.a ";
}

############################################################################
# directory completion on chdir ?                                          #
############################################################################
if ($ARG{DIRCOMPLETION} eq "yes") {
	$OPTION_CFLAGS .= "-DDIRCOMPLETION ";
}

############################################################################
# delete incomplete files by default?                                      #
############################################################################
if ($ARG{DELINCOMPLETE} eq "yes") {
	$OPTION_CFLAGS .= "-DDELINCOMPLETE ";
}

############################################################################
# traceroute checks                                                        #
############################################################################
if ($ARG{TRCHECK} eq "yes") {
	if (-x $ARG{TRBIN}) {
		$TRACEROUTE_BIN = $ARG{TRBIN};
	}
	elsif (-x "/usr/sbin/traceroute") {
		$TRACEROUTE_BIN = "/usr/sbin/traceroute";
	}

	if ($TRACEROUTE_BIN) {
		$OPTION_CFLAGS .= "-DTRACEROUTE_CHECK " .
		                  "-DTRACEROUTE_BIN=\'\"$TRACEROUTE_BIN\"\' ";
	}
}

if ($ARG{PORTRANGE} =~ /(\d+):(\d+)/) {
    $OPTION_CFLAGS .= "-DLOWERPORT=$1 -DHIGHERPORT=$2 ";
}

############################################################################
# length of integers                                                       #
############################################################################
if (exists $Config{'intsize'}) {
	$OPTION_CFLAGS .= "-DSIZEOF_INT=$Config{'intsize'} ";
}
else {
	die "Unable to detect size of integer";
}

if (exists $Config{'longsize'}) {
	$OPTION_CFLAGS .= "-DSIZEOF_LONG=$Config{'longsize'} ";
}
else {
	die "Unable to detect size of long";
}


############################################################################
# check for several system functions                                       #
############################################################################
if ($Config{'i_systime'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_TIME_H=1 ";
}
if ($Config{'i_syswait'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_WAIT_H=1 ";
}
if ($Config{'i_sysvfs'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_VFS_H=1 ";
}
if ($Config{'i_sysndir'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_NDIR_H=1 ";
}
if ($Config{'i_sysdir'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_DIR_H=1 ";
}
if ($Config{'i_sysndir'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SYS_NDIR_H=1 ";
}
if ($Config{'i_dirent'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_DIRENT_H=1 ";
}
if ($Config{'d_memmove'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_MEMMOVE=1 ";
}
if ($Config{'d_inetaton'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_INET_ATON=1 ";
}
if ($Config{'d_seteuid'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SETEUID=1 ";
}
if ($Config{'d_setegid'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_SETEGID=1 ";
}
if ($Config{'d_statvfs'} eq "define" or
    $Config{'d_fstatvfs'} eq "define")
{
	$OPTION_CFLAGS .= "-DHAVE_STATVFS=1 ";
}
if ($Config{'pidtype'} ne "pid_t") {
	$OPTION_CFLAGS .= "-Dpid_t=int ";
}
if ($Config{'uidtype'} ne "uid_t") {
	$OPTION_CFLAGS .= "-Duid_t=int ";
}
if ($Config{'gidtype'} ne "gid_t") {
	$OPTION_CFLAGS .= "-Dgid_t=int ";
}
if ($Config{'sizetype'} ne "size_t") {
	$OPTION_CFLAGS .= "-Dsize_t=unsigned ";
}

if ($Config{'d_flock'} eq "define") {
	$OPTION_CFLAGS .= "-DHAVE_FLOCK=1 ";
}

# this is maybe not very clean but should work :)
# all libs used by perl will be linked
if ($Config{'libs'}) {
	$OPTION_LIBS .= "$Config{'libs'} ";
	$OPTION_LIBS =~ s/-lgdbm//;
	$OPTION_LIBS =~ s/-ldbm//;
	$OPTION_LIBS =~ s/-ldb//;
}

# solaris had this weird setenv bug
if ($Config{'osname'} ne "sunos") {
	$OPTION_CFLAGS .= "-DHAVE_SETENV ";
}

# pipe fix for openbsd
if (($Config{'osname'} eq "openbsd") or 
    ($ARG{'PIPEFIX'} eq "yes")) 
{
	$OPTION_CFLAGS .= "-DPIPEFIX ";
}

if ($Config{'osname'} eq "freebsd") {
	$OPTION_CFLAGS .= "-DFREEBSD ";
}

# name of the architecture
$OPTION_CFLAGS .= "-DHOST_ARCH=\'\"$Config{'myarchname'}\"\' ";

# Check for system strlcpy()

eval {
    if(open DUMMY, "> strlcpy_test.c") {
        print DUMMY <<EOT;
#include <string.h>

int main() {
    strlcpy("", "", 0);
}
EOT
	close DUMMY;

	qx($CC -Wimplicit -Werror -c strlcpy_test.c 2>&1 >/dev/null);
	if(-f "strlcpy_test.o") {
	    $OPTION_CFLAGS .= "-DHAVE_STRLCPY ";
	}
	unlink "strlcpy_test.o";
	unlink "strlcpy_test.c";
    }

};

# Check for system strlcat()

eval {
    if(open DUMMY, "> strlcat_test.c") {
        print DUMMY <<EOT;
#include <string.h>

int main() {
    strlcat("", "", 0);
}
EOT
	close DUMMY;

	qx($CC -Wimplicit -Werror -c strlcat_test.c 2>&1 >/dev/null);
	if(-f "strlcat_test.o") {
	    $OPTION_CFLAGS .= "-DHAVE_STRLCAT ";
	}
	unlink "strlcat_test.o";
	unlink "strlcat_test.c";
    }

};

# Check where system crypt() is

eval {
    if(open DUMMY, "> crypt_test.c") {
        print DUMMY <<EOT;
#include <unistd.h>

int main() {
    crypt("", "");
}
EOT
	close DUMMY;
	
	qx($CC -Wimplicit -Werror -c crypt_test.c 2>&1 >/dev/null);
    }

    if(-f "crypt_test.o") {
	$OPTION_CFLAGS .= "-DCRYPT_UNISTD_H ";
    }
    else {
	if(open DUMMY, "> crypt_test.c") {
	    print DUMMY <<EOT;
#include <crypt.h>

int main() {
    crypt("", "");
}
EOT
            qx($CC -Wimplicit -Werror -c crypt_test.c 2>&1 >/dev/null);
	    if(-f "crypt_test.o") {
		$OPTION_CFLAGS .= "-DCRYPT_CRYPT_H ";
	    }
	}
    }

    unlink "crypt_test.o";
    unlink "crypt_test.c";
};

############################################################################
# openftpd version                                                         #
############################################################################
$OPTION_CFLAGS .= "-DVERSION=\'\"$VERSION\"\' ";

$OPTION_CFLAGS .= "-DHAVE_PERL ";

# export these variables
Export qw (GLIB_LIBS GLIB_CFLAGS CC OPTION_CFLAGS OPTION_LIBS);


# process the scripts in the subdirs
Build qw ( build/lib/Conscript
           build/common/Conscript
           build/ftpd/Conscript
           build/ftps/Conscript
	   build/ftpa/Conscript
           build/misc/Conscript
	 );

# -- end of file --
