#!/usr/bin/perl -w

die "You don't want to use this\nRead the comment at the start of the file";
# Sorry, but this script is probably broken
# 
# It is not changed for the new makefile methods, and we don't use it at
# debian, so we also don't test it. We're happy if someone fixes it, and
# would welcome patches on this.

use strict;
use English;

my ($TRUE, $FALSE) = (1,0);

# This program generates Makefile.config, which is included by all of the
# Netpbm makefiles.  You run this program as the first step in building 
# Netpbm.  (The second step is 'make').

# This program is only a convenience.  It is supported to create 
# Makefile.config any way you want.  In fact, an easy way is to copy
# Makefile.config.in and follow the instructions in the comments therein
# to uncomment certain lines and make other changes.

# Note that if you invoke 'make' without having first run 'configure',
# the make will call 'configure' itself when it finds
# 'Makefile.config' missing.  That might look a little messy to the
# user, but it isn't the normal build process.

# The argument to this program is the filepath of the Makefile.config.in
# file.  If unspecified, the default is 'Makefile.config.in' in the 
# current directory.

# For explanations of the stuff we put in the make files, see the comments
# in Makefile.config.in.

sub symlink_command();
sub getsvga($);

my $config_in_path;
if (@ARGV > 0) {
    $config_in_path = $ARGV[0];
} else {
    $config_in_path = "Makefile.config.in";
}

if (stat("Makefile.config")) {
    print("Discard existing Makefile.config?\n");
    print("Y or N (N) ==> ");

    my $answer = <STDIN>;
    chomp($answer);
    if (uc($answer) ne "Y") {
        exit(1);
    }
}

print("\n");

my $platform;
{
    print("Which of the following best describes your platform?\n");
 
    print("gnu      GNU/Linux\n");
    print("sun      Solaris or SunOS\n");
    print("hp       HP-UX\n");
    print("aix      AIX\n");
    print("win      Windows/DOS (Cygwin, DJGPP, Mingw32)\n");
    print("tru64    Tru64\n");
    print("irix     Irix\n");
    print("bsd      NetBSD, BSD/OS\n");
    print("openbsd  OpenBSD\n");
    print("osx      Darwin or Mac OS X\n");
    print("unixware Unixware\n");
    print("beos     BeOS\n");
    print("none     none of these are even close\n");
    print("\n");
    
    my $default;
    if ($OSNAME eq "linux") {
        $default = "gnu";
    } elsif ($OSNAME eq "aix") {
        $default = "aix";
    } elsif ($OSNAME eq "cygwin") {
        $default = "win";
    }
    # OK - if you know what $OSNAME is on any other platform, send me a patch!

    print("Your choice ");

    if (defined($default)) {
        print("($default)");
    }
    print("==> ");

    my $response = <STDIN>;

    print("\n");

    if (!defined($response)) {
        print("Aborting.\n");
        exit;
    }

    chomp($response);

    if ($response eq "") {
        if (defined($default)) {
            $response = $default;
        } else {
            print("Aborting.\n");
            exit;
        }
    }
        
    my %platform = ("gnu"      => "GNU",
                    "sun"      => "SOLARIS",
                    "hp"       => "HP-UX",
                    "aix"      => "AIX",
                    "tru64"    => "TRU64",
                    "irix"     => "IRIX",
                    "win"      => "WINDOWS",
                    "beos"     => "BEOS",
                    "bsd"      => "NETBSD",
                    "openbsd"  => "OPENBSD",
                    "unixware" => "UNIXWARE",
                    "osx"      => "DARWIN",
                    "none"     => "NONE"
                    );

    $platform = $platform{$response};
    if (!defined($platform)) {
        print("'$response' isn't one of the choices.\n");
        exit 8;
    }
}

if ($platform eq "NONE") {
    print("You will have to construct Makefile.config manually.  To do \n");
    print("this, copy Makefile.config.in as Makefile.config, and then \n");
    print("edit it.  Follow the instructions and examples in the file. \n");
    print("Please report your results to the Netpbm maintainer so he \n");
    print("can improve the configure program. \n");
    exit;
}

my $subplatform;

if ($platform eq "WINDOWS") {
    print("Are you building in/for the Cygwin environment?\n");
    print("\n");

    my $default;
    if ($OSNAME eq "cygwin") {
        $default = "y";
    } else {
        $default = "n";
    }

    print("(y)es or (n)o ($default)=> ");

    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    }

    if (uc($response) =~ /^(Y|YES)$/)  {
        $subplatform = "cygwin";
    } elsif (uc($response) =~ /^(N|NO)$/)  {
        $subplatform = "other";
    } else {
        print("'$response' isn't one of the choices.  \n" .
              "You must choose 'yes' or 'no' (or 'y' or 'n').\n");
        exit 12;
    }
}
my $compiler;

if ($platform eq "SOLARIS") {
    print("GNU compiler or Sun compiler?\n");
    print("\n");

    my $default = "gcc";
    print("gcc or sun ($default)=> ");

    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    }
    if ($response eq "gcc") {
        $compiler = "gcc";
    } elsif ($response eq "sun") {
        $compiler = "sun";
    } else {
        print("'$response' isn't one of the choices.  \n" .
              "You must choose 'gcc' or 'sun'.\n");
        exit 12;
    }
}

my $default_target;

print("Do you want a regular build or a merge build (see README)?\n");
print("\n");

{
    my $default = "regular";
    print("regular or merge ($default)=> ");
    
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    }
    if ($response eq "regular") {
        $default_target = "nonmerge";
    } elsif ($response eq "merge") {
        $default_target = "merge";
    } else {
        print("'$response' isn't one of the choices.  \n" .
              "You must choose 'regular' or 'merge'.\n");
        exit 12;
    }
}

print("\n");

my ($netpbmlibtype, $netpbmlibsuffix, $shlibprefixlist);
my $willBuildShared;

print("Do you want static-linked Netpbm libraries or shared?\n");
print("\n");

{
    my $default = ($default_target eq "merge") ? "static" : "shared";

    print("static or shared ($default)=> ");

    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    }
    if ($response eq "shared") {
        $willBuildShared = $TRUE;
        if ($platform eq "WINDOWS") {
            $netpbmlibtype = "dll";
            $netpbmlibsuffix = "dll";
            if ($subplatform eq 'cygwin') {
                $shlibprefixlist = "cyg lib";
            } else {
                $shlibprefixlist = "lib";
            }
        } elsif ($platform eq "DARWIN") {
            $netpbmlibtype = "dylib";
            $netpbmlibsuffix = "dylib";
            $shlibprefixlist = "lib";
        } else {
            $netpbmlibtype = "unixshared";
            if ($platform eq "AIX") {
                $netpbmlibsuffix = "a";
            } elsif ($platform eq "HP-UX") {
                $netpbmlibsuffix = "sl";
            } else {
                $netpbmlibsuffix = "so";
            }
            $shlibprefixlist = "lib";
        }
    } elsif ($response eq "static") {
        $willBuildShared = $FALSE;
        $netpbmlibtype = "unixstatic";
        $netpbmlibsuffix = "a";
        $shlibprefixlist = "lib"; # not used in Makefile static
        # targets, but needed for building
        # libopt 
    } else {
        print("'$response' isn't one of the choices.  \n" .
              "You must choose 'static' or 'shared'.\n");
        exit 12;
    }
}
print("\n");

my $staticlib_too;

if ($netpbmlibtype eq "unixshared" or $netpbmlibtype eq "dll") {
    print("Do you want to build static libraries too (for linking to \n");
    print("programs not in the Netpbm package?\n");
    print("\n");

    my $default = "y";

    print("(y)es or (n)o ($default)=> ");

    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = "y";
    } 
    if (uc($response) =~ /^(Y|YES)$/)  {
        $staticlib_too = "y";
    } elsif (uc($response) =~ /^(N|NO)$/)  {
        $staticlib_too = "n";
    } else {
        print("'$response' isn't one of the choices.  \n" .
              "You must choose 'yes' or 'no' (or 'y' or 'n').\n");
        exit 12;
    }
} else {
    $staticlib_too = "n";
}
print("\n");


#******************************************************************************
#
#  INSTALLATION QUESTIONS
#
#*****************************************************************************

print("The remaining questions concern how Netpbm is installed when you do\n");
print("'make install'.  None of these answers affect what gets built by\n");
print("'make'.  In particular, none of the directory paths you specify\n");
print("are built into any Netpbm program or library -- you can move them\n");
print("around as you like.\n");
print("\n");

my $install_prefix;

print("Enter the installation directory.  Everything gets installed under\n");
print("This directory.  If you want to spread Netpbm across your system,\n");
print("specify '/'\n");
print("\n");

{
    my $default;
    if ($platform eq "TRU64") {
        $default = "/usr/local1/DEC/packages/netpbm";
    } elsif ($platform eq "WINDOWS" and stat("/djgpp")) {
        $default = "/djgpp";
    } elsif ($platform eq "WINDOWS" and $subplatform eq "cygwin") {
        $default = "/usr/local";
    } else {
        $default = "/usr/local/netpbm";
    }

    print("install prefix ($default)=> ");
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    }
    $install_prefix = $response;
}
print("\n");

my $installbinaries;

print("Enter the install location for programs, relative to " .
      "$install_prefix.\n");
print("\n");
{
    my $default;
    if ($platform eq "WINDOWS") {
        $default = "bin/netpbm";
    } else {
        $default = "bin";
    }
    print("program directory ($default)=> ");
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    } elsif (substr($response,0,1) eq "/") {
        die("Cannot start with '/'.  This is a _relative_ path.");
    }
    $installbinaries = '$(INSTALL_PREFIX)/' . $response;
}
print("\n");

my $installlibs;

if ($willBuildShared) {
    print("Enter the install location for shared (runtime) libraries,\n");
    print("relative to $install_prefix.\n");
    print("\n");
    {
        my $default = "lib";
        print("library directory ($default)=> ");
        my $response = <STDIN>;
        chomp($response);
        if ($response eq "") {
            $response = $default;
        } elsif (substr($response,0,1) eq "/") {
            die("Cannot start with '/'.  This is a _relative_ path.");
        }
        $installlibs = '$(INSTALL_PREFIX)/' . $response;
    }
}
print("\n");

my $installstaticlibs;

print("Enter the install location for linktime libraries,\n");
print("relative to $install_prefix.\n");
print("These are libraries that get used when you build a program that\n");
print("is not in the Netpbm package, but uses the Netpbm subroutine\n");
print("libraries.  This includes both static-linking libraries and\n");
print("stubs for shared libraries\n");
print("\n");
{
    my $default = "lib";
    print("link directory ($default)=> ");
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    } elsif (substr($response,0,1) eq "/") {
        die("Cannot start with '/'.  This is a _relative_ path.");
    }
    $installstaticlibs = '$(INSTALL_PREFIX)/' . $response;
}
print("\n");

my $installhdrs;

print("Enter the install location for interface headers,\n");
print("relative to $install_prefix.\n");
print("These get used when you compile a program which is not in the\n");
print("Netpbm package, but uses the Netpbm subroutine libraries.\n");
print("\n");
{
    my $default = "include";
    print("header directory ($default)=> ");
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    } elsif (substr($response,0,1) eq "/") {
        die("Cannot start with '/'.  This is a _relative_ path.");
    }
    $installhdrs = '$(INSTALL_PREFIX)/' . $response;
}
print("\n");

my $installman;

print("Enter the install location for man pages,\n");
print("relative to $install_prefix.\n");
print("\n");
{
    my $default = "man";
    print("man page directory ($default)=> ");
    my $response = <STDIN>;
    chomp($response);
    if ($response eq "") {
        $response = $default;
    } elsif (substr($response,0,1) eq "/") {
        die("Cannot start with '/'.  This is a _relative_ path.");
    }
    $installman = '$(INSTALL_PREFIX)/' . $response;
}
print("\n");

my ($linuxsvgahdr, $linuxsvgalib) = getsvga($platform);

#******************************************************************************
#
#  BUILD THE FILE
#
#*****************************************************************************

my @Makefile_config;
    # This is the complete Makefile.config contents.  We construct it here
    # and ultimately write the whole thing out as Makefile.config.

# First, we just read the 'Makefile.config.in' in

open (CONFIG_IN,"<$config_in_path") or
    die("Unable to open file '$config_in_path' for input.  That file is " .
    "supposed to be in the Netpbm source top level directory, the " .
    "same as the 'configure' program.");

@Makefile_config = <CONFIG_IN>;

unshift(@Makefile_config, 
        "####This file was automatically created by 'configure.'\n",
        "####Many variables are set twice -- a generic setting, then \n",
        "####a system-specific override at the bottom of the file.\n",
        "####\n");

close(CONFIG_IN);

# Now, add the variable settings that override the default settings that are
# done by the Makefile.config.in contents.

push(@Makefile_config, "\n\n\n\n");
push(@Makefile_config, "####Lines above were copied from Makefile.config.in " .
     "by 'configure'.\n");
push(@Makefile_config, "####Lines below were added by 'configure' based on " .
     "the $platform platform.\n");


push(@Makefile_config, "DEFAULT_TARGET = $default_target\n");

push(@Makefile_config, "NETPBMLIBTYPE=$netpbmlibtype\n");
push(@Makefile_config, "NETPBMLIBSUFFIX=$netpbmlibsuffix\n");
push(@Makefile_config, "SHLIBPREFIXLIST=$shlibprefixlist\n");

push(@Makefile_config, "STATICLIB_TOO=$staticlib_too\n");


if ($platform eq "GNU") {
    # All the defaults are designed for a typical GNU system, so we don't
    # have to do much here.

#    # Red Hat Linux doesn't have 'ginstall', for some crazy reason, but
#    # of course 'install' is GNU install.
#    my $ginstall_result = `ginstall --version 2>/dev/null`;
#    if (!$ginstall_result) {
#        # System doesn't have 'ginstall', so use 'install' instead.
#        push(@Makefile_config, "INSTALL = install\n");
#   }
    push(@Makefile_config, "CFLAGS = -pedantic -O3 " .
         "-Wall -Wno-uninitialized -Wmissing-declarations\n");
# The merged programs have a main_XXX subroutine instead of main(),
# which would cause a warning with -Wmissing-declarations
    push(@Makefile_config, "CFLAGS_MERGE = -Wno-missing-declarations\n");
} elsif ($platform eq "SOLARIS") {
#    push(@Makefile_config, "INSTALL = /usr/ucb/install\n");
    # If the system uses gcc as a linker, the following LDFLAGS isn't right.
    push(@Makefile_config, 'LDFLAGS = -R$(INSTALLLIBS)', "\n");
    # If the system uses GNU Ld, the following LDSHLIB isn't right.
    push(@Makefile_config, 
         'LDSHLIB = -Wl,-B,dynamic,-G,-h,$(SONAME) -R$(INSTALLLIBS)', "\n");
    push(@Makefile_config, 'NEED_RUNTIME_PATH = Y', "\n");
    if ($compiler eq "sun") {
        push(@Makefile_config, "CC = cc\n");
        push(@Makefile_config, "CFLAGS = -O\n");
        push(@Makefile_config, "CFLAGS_SHLIB = -Kpic\n");
    }
    push(@Makefile_config, "NETWORKLD = -lsocket -lnsl\n");
} elsif ($platform eq "HP-UX") {
    push(@Makefile_config, 'CFLAGS = -O3 -fPIC $(CDEBUG)', "\n");
    push(@Makefile_config, 'LDFLAGS = -Wl,+b,/usr/pubsw/lib', "\n");
} elsif ($platform eq "AIX") {
    push(@Makefile_config, 'CFLAGS = -O3 -D_ALL_SOURCE $(CDEBUG)', "\n");
    push(@Makefile_config, 'LDFLAGS = -L /usr/pubsw/lib', "\n");
} elsif ($platform eq "TRU64") {
    push(@Makefile_config, "CC=cc\n");
    push(@Makefile_config, "LD=cc\n");
#    push(@Makefile_config, "INSTALL = installbsd\n");
    push(@Makefile_config, 'CFLAGS = -O2 -std1 -DLONG_32 $(CDEBUG)', "\n");
    push(@Makefile_config, "LDFLAGS = -call_shared -oldstyle_liblookup\n");
    push(@Makefile_config, "LDSHLIB = -shared -expect_unresolved \"*\"\n");
    push(@Makefile_config, "TIFFHDR_DIR = /usr/local1/DEC/include\n");
    push(@Makefile_config, "TIFFLIB_DIR = /usr/local1/DEC/lib\n");
    push(@Makefile_config, "JPEGLIB_DIR = /usr/local1/DEC/lib\n");
    push(@Makefile_config, "JPEGHDR_DIR = /usr/local1/DEC/include\n");
    push(@Makefile_config, "PNGLIB_DIR = /usr/local1/DEC/lib\n");
    push(@Makefile_config, "PNGHDR_DIR = /usr/local1/DEC/include\n");
    push(@Makefile_config, "NETWORKLD = NONE\n");
} elsif ($platform eq "IRIX") {
    push(@Makefile_config, "CC=cc\n");
#    push(@Makefile_config, "INSTALL = install\n");
    push(@Makefile_config, 'MANCP = $(SRCDIR)/buildtools/mantocat', "\n");
    push(@Makefile_config, 'RANLIB = true', "\n");
    push(@Makefile_config, 'CFLAGS = -n32 -O3', "\n");
    push(@Makefile_config, 'LDFLAGS = -n32', "\n");
    push(@Makefile_config, 'LDSHLIB = -shared -n32', "\n");
} elsif ($platform eq "WINDOWS") {
    # Until 9.15 (June 2001), we set MANCP here because it appeared that DJGPP
    # required it.  Now I'm not sure.
#    push(@Makefile_config, 'MANCP = $(SRCDIR)/buildtools/mantocat', "\n");
    push(@Makefile_config, "EXE = .exe\n");
    push(@Makefile_config, "NETWORKLD = NONE\n");
#    # Though it may not have the link as "ginstall", "install" in a Windows
#    # Unix environment is usually GNU install.
#    my $ginstall_result = `ginstall --version 2>/dev/null`;
#    if (!$ginstall_result) {
#        # System doesn't have 'ginstall', so use 'install' instead.
#        push(@Makefile_config, "INSTALL = install\n");
#    }
    push(@Makefile_config, 'SYMLINK = ', symlink_command(), "\n");
    push(@Makefile_config, "LDSHLIB = " . 
         '-shared -Wl,--enable-auto-image-base', "\n");
    push(@Makefile_config, 'TIFFHDR_DIR = /usr/include', "\n");
    push(@Makefile_config, 'TIFFLIB_DIR = /usr/lib', "\n");
    push(@Makefile_config, 'JPEGLIB_DIR = /usr/lib', "\n");
    push(@Makefile_config, 'JPEGHDR_DIR = /usr/include', "\n");
    push(@Makefile_config, 'PNGLIB_DIR = /usr/lib', "\n");
    push(@Makefile_config, 'PNGHDR_DIR = /usr/include', "\n");
    push(@Makefile_config, 'ZLIB_DIR = /usr/lib', "\n");
    push(@Makefile_config, 'ZHDR_DIR = /usr/include', "\n");
} elsif ($platform eq "BEOS") {
    push(@Makefile_config, "LDSHLIB = -nostart\n");
} elsif ($platform eq "NETBSD") {
    push(@Makefile_config, 'LDFLAGS = -Wl,--rpath,$(INSTALLLIBS)', "\n");
    push(@Makefile_config, 'CFLAGS_SHLIB = -fpic', "\n");
    push(@Makefile_config, 'TIFFHDR_DIR = $(LOCALBASE)/include', "\n");
    push(@Makefile_config, 'TIFFLIB_DIR = $(LOCALBASE)/lib', "\n");
    push(@Makefile_config, 'JPEGLIB_DIR = $(LOCALBASE)/lib', "\n");
    push(@Makefile_config, 'JPEGHDR_DIR = $(LOCALBASE)/include', "\n");
    push(@Makefile_config, 'PNGLIB_DIR = $(LOCALBASE)/lib', "\n");
    push(@Makefile_config, 'PNGHDR_DIR = $(LOCALBASE)/include', "\n");
    push(@Makefile_config, "\n");
} elsif ($platform eq "OPENBSD") {
    # vedge@vedge.com.ar says on 2001.04.29 that there are a ton of 
    # undefined symbols in the Fiasco stuff on OpenBSD.  So we'll just
    # cut it out of the build until someone feels like fixing it.
    push(@Makefile_config, 'BUILD_FIASCO = N', "\n");
} elsif ($platform eq "UNIXWARE") {
    # Nothing to do.
} elsif ($platform eq "DARWIN") {
    push(@Makefile_config, "CC = cc -no-cpp-precomp\n");
    push(@Makefile_config, 'CFLAGS_SHLIB = -fno-common', "\n");
    push(@Makefile_config, 'LDSHLIB = -dynamiclib -install_name $(INSTALLLIBS)/lib$(LIBROOT).$(MAJ).dylib', "\n");
#    push(@Makefile_config, "INSTALL = install\n");
    push(@Makefile_config, 'TIFFHDR_DIR = $(LOCALBASE)/include', "\n");
    push(@Makefile_config, 'TIFFLIB_DIR = $(LOCALBASE)/lib', "\n"); 
    push(@Makefile_config, 'JPEGLIB_DIR = $(LOCALBASE)/lib', "\n");     
    push(@Makefile_config, 'JPEGHDR_DIR = $(LOCALBASE)/include', "\n");
    push(@Makefile_config, 'PNGLIB_DIR = $(LOCALBASE)/lib', "\n");
    push(@Makefile_config, 'PNGHDR_DIR = $(LOCALBASE)/include', "\n");
} else {
    die ("Internal error: invalid value for \$platform: '$platform'\n");
}


my $flex_result = `flex --version`;
if (!$flex_result) {
    # System doesn't have 'flex'.  Maybe 'lex' will work; we don't really
    # know yet.  2001.05.16.

    # lex may work; we don't really know yet.  Schwarb Manfred reports
    # that it compiles OK, but with warnings, on Solaris.  Solaris Lex
    # has a -e option that eliminates the lex warnings, but causes
    # compiler warnings.  AIX and Flex don't have a -e option.
    # -Bryan 2001.05.16.

    push(@Makefile_config, "LEX = lex\n"); 

    # We could also set LEX to null, and then the one part that requires
    # it -- thinkjettopbm -- would simply not get built.

#    print("\n");
#    print("There is no 'flex' command in your environment, so we will not\n");
#    print("Build the part that requires it: thinkjettopbm.\n");

#    print("\n");
#    print("Press ENTER to continue.\n");
    my $key = <STDIN>;
}

if (defined($linuxsvgahdr)) {
    push(@Makefile_config, "LINUXSVGAHDR_DIR = $linuxsvgahdr\n");
}
if (defined($linuxsvgalib)) {
    push(@Makefile_config, "LINUXSVGALIB_DIR = $linuxsvgalib\n");
}

push(@Makefile_config, "INSTALL_PREFIX = $install_prefix\n");
push(@Makefile_config, "# Some people specify PREFIX= on the command line, " .
     "because that's conventional\n");
push(@Makefile_config, 'ifneq ($(PREFIX)x,x)', "\n");
push(@Makefile_config, '  INSTALL_PREFIX = $(PREFIX)', "\n");
push(@Makefile_config, 'endif', "\n");

if (defined($installbinaries)) {
    push(@Makefile_config, "INSTALLBINARIES = $installbinaries\n");
}
if (defined($installlibs)) {
    push(@Makefile_config, "INSTALLLIBS = $installlibs\n");
}
if (defined($installstaticlibs)) {
    push(@Makefile_config, "INSTALLSTATICLIBS = $installstaticlibs\n");
}
if (defined($installhdrs)) {
    push(@Makefile_config, "INSTALLHDRS = $installhdrs\n");
}
if (defined($installman)) {
    push(@Makefile_config, "INSTALLMAN = $installman\n");
}



#******************************************************************************
#
#  WRITE OUT THE FILE
#
#*****************************************************************************

open(MAKEFILE_CONFIG, ">Makefile.config") or
    die("Unable to open Makefile.config for writing in the current " .
        "directory.");

print MAKEFILE_CONFIG @Makefile_config;

close(MAKEFILE_CONFIG) or
    die("Error:  Close of Makefile.config failed.\n");

print("\n");
print("We have created the file 'Makefile.config'.  You can now \n");
print("proceed to enter the 'make' command.\n");
print("\n");
print("Note, however, that we guessed a lot at your configuration,\n");
print("and you may want to look at Makefile.config and\n");
print("edit it to your requirements and taste before doing the make. \n");

exit 0;          
    


sub symlink_command() {

    my $retval;

    # Some Windows environments don't have symbolic links (or even a
    # simulation via a "ln" command, but have a "cp" command which works
    # in a pinch.
    
    # I wish I knew how to eliminate the stderr messages from these,
    # but I don't think I can count on /dev/null existing on these
    # systems.
    print("We now check to see if the 'ln' and 'cp' commands exist on \n");
    print("your system.  Ignore any error messages.\n");
    my $ln_rc = system("ln");
    my $cp_rc = system("cp");

    # I think 127 is universally the return code for "command not found"
    if ($ln_rc != 127) {
        $retval = "ln -s";
    } elsif ($cp_rc != 127) {
        $retval = "cp";
    } else {
        # Well, maybe we just made a mistake.
        $retval = "ln -s";
    }
    return $retval;
}

sub getsvga($) {
    my ($platform) = @_;

    my ($linuxsvgahdr, $linuxsvgalib);

    if ($platform eq "GNU") {
        # Look for SVGALIB 
        if (-f "/usr/include/vga.h") {
            $linuxsvgahdr = "/usr/include";
        } elsif (-d "/usr/include/svgalib") {
            $linuxsvgahdr = "/usr/include/svgalib";
        } else {
            print("Cannot find the include files for SVGALIB.  Enter the " .
                  "directory where they reside, or NONE.  " .
                  "If NONE, a default " .
                  "make will not build programs that require " .
                  "the SVGALIB library.\n");

            print("Svgalib header location (NONE)=> ");

            my $response = <STDIN>;
            chomp($response);
            if ($response eq "") {
                $response = "NONE";
            }
            $linuxsvgahdr = $response;
        }
        if ($linuxsvgahdr eq "NONE") {
            $linuxsvgalib = "NONE";
        } else {
            if (-f "/usr/lib/svgalib.so") {
                $linuxsvgalib = "/usr/lib/svgalib.so";
            } elsif (-d "/usr/link/svgalib") {
                $linuxsvgalib = "/usr/link/svgalib";
            } else {
                print("Cannot find the link libraries for SVGALIB.  " .
                      "Enter the " .
                      "directory where they reside, or NONE.  If NONE, " .
                      "a default make will not build programs that require " .
                      "the SVGALIB library.\n");

                print("Svgalib link lib location (NONE)=> ");

                my $response = <STDIN>;
                chomp($response);
                if ($response eq "") {
                    $response = "NONE";
                }
                $linuxsvgalib = $response;
            }
        }
    }
    return($linuxsvgahdr, $linuxsvgalib);
}


