#! /usr/bin/perl
# If this doesn't work, just call it as .../perl configure

# Copyright (c) 1995-1998 Felix Schroeter <felix@mamba.pond.sub.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer,
#     either in a separate file included with the distribution, or
#     copied into the source files.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND HIS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR HIS CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

$redo = 0;
if ($#ARGV == 0) {
  if ($ARGV[0] =~ /^-?-?r(e(do?)?)?$/) {
    if (! -f "./config.sed") {
      die "Can't redo without prior configuration\n";
    }
    $redo = 1;
  }
}

@conffiles = (
  "Makefile",
  "batcher",
  "batcher.c",
  "bsmtp.c",
  "bsmtp.m4.8.8",
  "bsmtp.m4.8.7",
  "bsmtp.m4.8.6",
  "rcsmtp",
  "rsmtp",
  "rsmtp.wrapper.c",
  "transmitter.c-cbsmtp",
  "transmitter.c-rcsmtp",
  "transmitter.g-rcsmtp",
  "transmitter.g-rgsmtp",
  "transmitter.n-rsmtp",
  "_bqueue.c",
  "bqueue",
  "contrib/andyr/deliver.pl"
);
if (! $redo) {
  open (CONFIGIN, "config.in") || die "Can't open config.in\n";
  $i = 0;
  while (<CONFIGIN>) {
    next if (/^\s*\#.*$/);
    if (/^\>/) {
      s/^\>[ \t]*//;
      print; next;
    }
    /^([^\s]+)\s+(([^\s]|\\.)+)\s+([^\[\s]+([^\[]*[^\[\s])?)(\s*\[(.*)\])?\s*$/;
    $macro[$i] = $1;
    $default[$i] = $2; $default[$i] =~ s/\\(.)/\1/g;
    $help[$i] = $4;
    $flag[$i] = $7;
    if ($flag[$i] =~ /auto/) {
      $macro = $macro[$i];
      if ($macro eq "\@CC\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/cc" && -x "$_/cc") {
            $cc = "$_/cc";
            last;
          }
        }
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/gcc" && -x "$_/gcc") {
            $gcc = "$_/gcc";
            last;
          }
        }
        if ($cc && $gcc) {
          print "cc and gcc found. Preferring gcc.\n";
        }
        $cc = $gcc if ($gcc);
        $default[$i] = $cc;
      } elsif ($macro eq "\@INSTALL\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/install" && -x "$_/install") {
            $default[$i] = "$_/install"; last;
          }
        }
      } elsif ($macro eq "\@SH\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/sh" && -x "$_/sh") {
            $default[$i] = "$_/sh"; last;
          }
        }
      } elsif ($macro eq "\@GZIP\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/gzip" && -x "$_/gzip") {
            $default[$i] = "$_/gzip"; last;
          }
        }
      } elsif ($macro eq "\@COMPRESS\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/compress" && -x "$_/compress") {
            $default[$i] = "$_/compress"; last;
          }
        }
      } elsif ($macro eq "\@PERL\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/perl" && -x "$_/perl") {
            $default[$i] = "$_/perl"; last;
          }
          if (-x "$_/perl5") {
            $default[$i] = "$_/perl5"; last;
          }
        }
      } elsif ($macro eq "\@UUX\@") {
        for (split (/:/, $ENV{'PATH'})) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/uux" && -x "$_/uux") {
            $default[$i] = "$_/uux"; last;
          }
        }
      } elsif ($macro eq "\@SENDMAIL\@") {
        for (split (/:/, $ENV{'PATH'} . ":/sbin:/usr/sbin:/usr/libexec:/usr/lib")) {
          s/^$/$ENV{'HOME'}/;
          if (-f "$_/sendmail" && -x "$_/sendmail") {
            $default[$i] = "$_/sendmail"; last;
          }
        }
      } elsif ($macro eq "\@DAEMONUID\@") {
        if (! defined @getpwnam) {
          if (! defined $value{'@DAEMONUSER@'}) {
            die "\@DAEMONUSER\@ must be defined before \@DAEMONUID\@\n";
          }
          @getpwnam = getpwnam ($value{'@DAEMONUSER@'});
          if (! @getpwnam) {
            die "User $value{'@DAEMONUSER@'} doesn't exist\n";
          }
        }
        ($name, $_, $uid, $_, $_, $_, $_, $_, $_) = @getpwnam;
        $default[$i] = $uid;
      } elsif ($macro eq "\@DAEMONGID\@") {
        if (! defined @getpwnam) {
          if (! defined $value{'@DAEMONUSER@'}) {
            die "\@DAEMONUSER\@ must be defined before \@DAEMONUID\@\n";
          }
          @getpwnam = getpwnam ($value{'@DAEMONUSER@'});
          if (! @getpwnam) {
            die "User $value{'@DAEMONUSER@'} doesn't exist\n";
          }
        }
        ($name, $_, $_, $gid, $_, $_, $_, $_, $_) = @getpwnam;
        $default[$i] = $gid;
      } elsif ($macro eq '@CFLAGS@') {
        if (defined $value{'@CC@'} && $value{'@CC@'} =~ /gcc/) {
          $default[$i] = "-O2";
        } else {
          $default[$i] = "-O";
        }
      } elsif ($macro eq '@INSTALLMAILER@') {
        if (defined ($value{'@SENDMAILVERS@'})) {
          if ($value{'@SENDMAILVERS@'} =~ /^\s*8\.[678](\..*)?\s*$/) {
	    $default[$i] = 'true';
	  } else {
	    $default[$i] = 'false';
	  }
        } elsif (defined ($value{'@OLDSENDMAIL@'})) {
          $default[$i] = "true";
	} else {
	  die "Either \@OLDSENDMAIL\@ or \@SENDMAILVERS\@ must be defined\n" .
	    "before \@INSTALLMAILER\@\n";
	}
      } elsif ($macro eq '@MAILERVERSION@') {
	if (defined ($value{'@SENDMAILVERS@'})) {
	  if ($value{'@SENDMAILVERS@'} =~ /^\s*8\.6(\..*)?\s*$/) {
	    $default[$i] = '8.6';
	  } elsif ($value{'@SENDMAILVERS@'} =~ /^\s*8\.7(\..*)?\s*$/) {
	    $default[$i] = '8.7';
	  } elsif ($value{'@SENDMAILVERS@'} =~ /^\s*8\.8(\..*)?\s*$/) {
	    $default[$i] = '8.8';
	  } else {
	    $default[$i] = ''; # Will not be installed
	  }
	} elsif (defined ($value{'@OLDSENDMAIL@'})) {
          if ($value{'@OLDSENDMAIL@'} =~ /^false$/) {
            $default[$i] = '8.7';
          } else {
            $default[$i] = '8.6';
          }
	} else {
	  die "Either \@OLDSENDMAIL\@ or \@SENDMAILVERS\@ must be defined\n" .
	    "before \@MAILERVERSION\@\n";
        }
      } elsif ($macro eq '@LOCKDEFINE@') {
	if (! defined ($value{'@FLOCK@'})) {
	  die "\@FLOCK\@ must be defined before \@LOCKDEFINE\@\n";
	}
	if ($value{'@FLOCK@'} =~ /^\s*(y(es?)?|t(r(ue?)?)?)\s*$/i) {
	  $default[$i] = '-DUSE_FLOCK';
	} else {
	  $default[$i] = '-DUSE_LOCKFILES';
	}
      } elsif ($macro eq '@BATCHER@') {
	if (! defined ($value{'@FLOCK@'})) {
	  die "\@FLOCK\@ must be defined before \@BATCHER\@\n";
	}
	if ($value{'@FLOCK@'} =~ /^\s*(y(es?)?|t(r(ue?)?)?)\s*$/i) {
	  $default[$i] = 'batcher.new';
	} else {
	  $default[$i] = 'batcher';
	}
      } else {
        die "Autoconf macro $macro not defined yet\n";
      }
    } else {
      $flag[$i] = "conf";
    }
    if ($flag[$i] =~ /conf/) {
      print "$help[$i] [$default[$i]]: ";
      $answer = <STDIN>;
      $answer =~ tr/\r\n//d;
      if ($answer =~ /^$/) {
        $answer = $default[$i];
      }
      $default[$i] = $answer;
    }
    $value{$macro[$i]} = $default[$i];
    $i ++;
  }
  $max = $i;
  
  close CONFIGIN;
  print "Writing config.sed\n";
  open (CONFIGSED, ">config.sed") || die "Can\'t open config.sed\n";
  for ($i = 0; $i < $max; $i ++) {
    next if ($macro[$i] =~ /^\>/);
    print "s,", $macro[$i], ",";
    print CONFIGSED "s,", $macro[$i], ",";
    $_ = $value{$macro[$i]};
    s/\,/\\\,/g;
    print;
    print CONFIGSED;
    print ",\n";
    print CONFIGSED ",\n";
  }
  close CONFIGSED;
} else {
  print "Redoing configuration with old values\n";
}
for (@conffiles) {
  print "$_.in -> $_: ";
  if (system ("sed -f config.sed <$_.in >$_")) {
    print "Error\n";
  } else {
    print "ok\n";
  }
}
exit 0;
