#!/bin/perl -w

use strict;

# 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.

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

print("\n");
print("Which of the following best describes your platform?\n");

print("1) GNU/Linux\n");
print("2) Solaris or SunOS\n");
print("3) AIX\n");
print("4) Tru64\n");
print("5) Irix\n");
print("6) Windows (Cygwin or DJGPP)\n");
print("7) BeOS\n");
print("8) NetBSD\n");
print("9) none of these are even close\n");
print("\n");
print("Your choice ==> ");

my $platform_choice = <STDIN>;

print("\n");

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

chomp($platform_choice);

my %platform = (1 => "GNU",
                2 => "SOLARIS",
                3 => "AIX",
                4 => "TRU64",
                5 => "IRIX",
                6 => "WINDOWS",
                7 => "BEOS",
                8 => "NETBSD",
                9 => "NONE"
                );

my $platform = $platform{$platform_choice};
if (!defined($platform)) {
    print("'$platform_choice' 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;
}

print("Enter the installation directory (the prefix on all installation\n");
print("paths for 'make install').  This is not built into any programs;\n");
print("It is used only by 'make install'.\n");
print("\n");

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

print("install prefix ($install_prefix_default)=> ");
my $install_prefix = <STDIN>;
chomp($install_prefix);
if ($install_prefix eq "") {
    $install_prefix = $install_prefix_default;
}

print("\n");


my ($netpbmlibsuffix, $libtiffsuffix);

if ($platform eq "WINDOWS") {
    print("Configuring for static-linked Netpbm libraries, since Windows\n");
    print("Unix emulation doesn't provide shared libraries.\n");
    $netpbmlibsuffix = "a";
    $libtiffsuffix = "a";
} else {
    print("Do you want static-linked Netpbm libraries or shared?\n");
    print("\n");
    print("static or shared (shared)=> ");
    
    my $staticlib_resp = <STDIN>;
    chomp($staticlib_resp);
    if ($staticlib_resp eq "") {
        $netpbmlibsuffix = "so";
        $libtiffsuffix = "so";
    } elsif ($staticlib_resp eq "static") {
        $netpbmlibsuffix = "a";
        $libtiffsuffix = "a";
    } elsif ($staticlib_resp eq "shared") {
        $netpbmlibsuffix = "so";
        $libtiffsuffix = "so";
    } else {
        print("'$staticlib_resp' isn't one of the choices.  \n" .
              "You must choose 'static' or 'shared'.\n");
        exit 12;
    }
}

print("\n");




#******************************************************************************
#
#  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");

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, "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");

push(@Makefile_config, "NETPBMLIBSUFFIX=$netpbmlibsuffix\n");
push(@Makefile_config, "LIBTIFFSUFFIX=$libtiffsuffix\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`;
    if (!$ginstall_result) {
        # System doesn't have 'ginstall', so use 'install' instead.
        push(@Makefile_config, "INSTALL = install\n");
    }
} elsif ($platform eq "SOLARIS") {
    push(@Makefile_config, "CC=cc\n");
    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)', "\n");
    push(@Makefile_config, "CFLAGS_SHLIB = -fpic\n");
    push(@Makefile_config, "NETWORKLD = -lsocket -lnsl\n");
} elsif ($platform eq "AIX") {
    # There must be special settings for AIX, but no one has reported 
    # them yet.  Maybe they will now.
} 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\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)/mantocat', "\n");
    push(@Makefile_config, 'RANLIB =', "\n");
    push(@Makefile_config, 'CFLAG = -n32', "\n");
    push(@Makefile_config, 'LDFLAGS = -n32', "\n");
    push(@Makefile_config, 'LDSHLIB = -shared -n32');
} elsif ($platform eq "WINDOWS") {
    push(@Makefile_config, 'MANCP = $(SRCDIR)/mantocat', "\n");
    push(@Makefile_config, "EXE = .exe\n");
    push(@Makefile_config, "NETWORKLD = NONE\n");
    push(@Makefile_config, "\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");
} else {
    die ("Internal error: invalid value for \$platform: '$platform'\n");
}

# Now write it out

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 have only made a rough guess at your \n");
print("configuration, and you may want to look at Makefile.config and\n");
print("edit it to your requirements and taste before doing the make. \n");
          
    


