#!/usr/bin/perl
#
# Configure script for Net-SNMP and MSVC
# Written by Alex Burger
# March 5th, 2004
#
use Getopt::Long;
use strict;

my $version = "unknown";
my $config;
my $sdk = 0;
my $linktype;
my $prefix;
my $prefixdos;
my $openssl = 0;
my $b_ipv6 = 0;
my $b_winextdll = 0;
my $help = 0;

GetOptions      ('config=s' => \$config,
                 'with-sdk' => \$sdk,
                 'linktype=s' => \$linktype,
                 'destdir=s' => \$prefix,
                 'prefix=s' => \$prefix,
                 'with-ssl' => \$openssl,
                 'with-ipv6' => \$b_ipv6,
                 'with-winextdll' => \$b_winextdll,
                 'help' => \$help);

if ($help == 1)
{
my $USAGE = qq/
Usage:
    perl Configure [<options>]

Options:

    --config=[release | debug]       Compile as release or with debug symbols
    --with-sdk                       Link against MS Platform SDK
    --linktype=[static | dynamic]    Build static or dynamic (DLL)
    --prefix=\"path\"                  Set INSTALL_BASE path (install path)
    --destdir=\"path\"                 Same as --prefix
    --with-ssl                       Link against OpenSSL
    --with-ipv6                      Build in IPv6 transports (enables SDK)
    --with-winextdll                 Build winExtDLL agent (enables SDK, see README.win32)
    --help                           This help screen
/;

  print $USAGE;

  exit(0);

}

$config = lc($config);
if (($config ne "debug") && ($config ne "release")) {
  $config = "release";
}

$linktype = lc($linktype);
if (($linktype ne "static") && ($linktype ne "dynamic")) {
  $linktype = "static";
}

if ($prefix eq "") {
  $prefix = "c:/usr";
}

# Make sure prefix only contains forward slashes
$prefix =~ s/\\/\//g;

$prefixdos = "\"$prefix\"";
# Make sure prefixdos only contains backward slashes
$prefixdos =~ s/\//\\/g;

# Enable SDK for IPV6 and winExtDLL
if ($b_ipv6 == 1) {
  $sdk = 1;
}
if ($b_winextdll == 1) {
  $sdk = 1;
}

print "\n\n";

###############################################
#
# Determine version from unix configure script
#
###############################################

my $unix_configure_in = "../configure";

open (UNIX_CONFIGURE_IN, "<$unix_configure_in") || die "Can't Open $unix_configure_in\n";

while (<UNIX_CONFIGURE_IN>)
{
  chomp;
  /PACKAGE_VERSION='(.*)'/;
  if ($1 ne "") {
    $version = $1;
    last;
  }
}

# Arguments:
# $1: Name of output file
# $2: Name of input file
# $3: Reference to a hash with substitutions. A substitution is an array with
#     two elements where element [0] is the pattern to search for and element
#     [1] the replacement text.
sub substitute
{
  my $out  = shift;
  my $in   = shift;
  my $subs = shift;

  open (OUT, ">$out") || die "Can't open $out\n";
  open (IN,  "<$in")  || die "Can't open $in\n";

  print "creating $out\n";

  while (<IN>) {
    chomp;
    foreach my $e (keys %$subs) {
        my $p = $$subs{$e}[0];
        my $q = $$subs{$e}[1];
        s/$p/$q/;
    }
    print OUT $_ . "\n";
  }

  close IN;
  close OUT;
}

###############################################
#
# Perform substitutions
#
###############################################
my %subs = (
    "cfg" => [ "^CFG=",        "CFG=$config"                        ],
    "int" => [ "^INTDIR=",     "INTDIR=.\\$config"                  ],
    "lnk" => [ "^LINKTYPE=",   "LINKTYPE=$linktype"                 ],
    "out" => [ "^OUTDIR=",     "OUTDIR=.\\$config"                  ],
    "pfx" => [ "^PREFIX=",     "PREFIX=$prefix"                     ],
    "dos" => [ "^PREFIX_DOS=", "PREFIX_DOS=$prefixdos"              ],
    "sdk" => [ "^SDK=",        $sdk == 1 ? "SDK=true" : "SDK=false" ],
    );

substitute("Makefile", "Makefile.in", \%subs);
substitute("local/Makefile", "local/Makefile.in", \%subs);

my @programs = qw
/encode_keychange
snmpbulkget
snmpbulkwalk
snmpdelta
snmpdf
snmpget
snmpgetnext
snmpset
snmpstatus
snmptable
snmptest
snmptranslate
snmptrap
snmpusm
snmpvacm
snmpwalk
/;

foreach my $progName (@programs) {
    $subs{"prg"} = [ "^PROGNAME=", "PROGNAME=$progName" ];
    substitute("$progName/Makefile", "Makefile-apps.in", \%subs);
}

foreach my $progName ("libagent", "libnetsnmptrapd", "local",
                      "netsnmpmibs", "snmpd", "snmptrapd", "snmpnetstat",
                      $linktype eq "dynamic" ? "libsnmp_dll" : "libsnmp") {
    $subs{"prg"} = [ "^PROGNAME=", "PROGNAME=$progName" ];
    substitute("$progName/Makefile", "$progName/Makefile.in", \%subs);
}

my %subs = (
    "env" => [ "\@ENV_SEPARATOR\@", ";" ]
);

substitute("../local/snmpconf", "../local/snmpconf.in", \%subs);


my %subs = (
    "ver" => [ "^#define PACKAGE_VERSION.*",
               "#define PACKAGE_VERSION \"$version\"" ]
    );
if ($prefix ne "") {
    $subs{"pfx"} = [ "^#define INSTALL_BASE.*", "#define INSTALL_BASE \"$prefix\"" ];
}
if ($linktype eq "dynamic") {
    $subs{"dll"} = [ "^.*#undef NETSNMP_USE_DLL.*", "#define NETSNMP_USE_DLL 1" ];
}
if ($sdk == 1) {
    $subs{"sdk"} = [ "^.*#undef HAVE_WIN32_PLATFORM_SDK.*", "#define HAVE_WIN32_PLATFORM_SDK 1" ];
}
if ($openssl == 1) {
    $subs{"ssl"} = [ "^.*#undef NETSNMP_USE_OPENSSL.*", "#define NETSNMP_USE_OPENSSL 1" ];
} else {
    $subs{"ssl"} = [ "^.*#undef NETSNMP_USE_INTERNAL_MD5.*", "#define NETSNMP_USE_INTERNAL_MD5 1" ];
}
if ($b_ipv6 == 1) {
    $subs{"ipv6"} = [ "^.*#undef NETSNMP_ENABLE_IPV6.*", "#define NETSNMP_ENABLE_IPV6 1" ];
}
if ($b_winextdll == 1) {
    $subs{"winextdll"} = [ "^.*#undef USING_WINEXTDLL_MODULE.*", "#define USING_WINEXTDLL_MODULE 1" ];
}

substitute("net-snmp/net-snmp-config.h", "net-snmp/net-snmp-config.h.in",
           \%subs);

print qq/
---------------------------------------------------------
            Net-SNMP configuration summary:
---------------------------------------------------------

/;

if ($version eq "unknown") {
  $version = "unknown - Could not determine version from ../configure!";
}

print "  Version:                    $version\n";
print "  Config type:                $config\n";
print "  SDK:                        " . ($sdk == 1 ? "enabled" : "disabled") . "\n";
print "  Link type:                  $linktype\n";
print "  Prefix / Destdir:           " . ($prefix ne "" ? $prefix : "(default)") . "\n";
print "  OpenSSL:                    " . ($openssl == 1 ? "enabled" : "disabled") . "\n";
print "  IPv6 transport:             " . ($b_ipv6 == 1 ? "enabled" : "disabled") . "\n";
print "  winExtDLL agent:            " . ($b_winextdll == 1 ? "enabled" : "disabled") . "\n";

if ($ENV{INCLUDE} eq "") {
  print "\n\nVisual Studio environment not detected.  Please run VCVARS32.BAT before\n";
  print "running nmake\n\n";
}

