#                              -*- Mode: Perl -*- 
# $Basename: Construct $ -- 
# ITIID           : $Source: Construct $ $Revision: 1.6 $
# Author          : Ulrich Pfeifer
# Created On      : Wed Jul 31 10:59:49 1996
# Last Modified By: Ulrich Pfeifer
# Last Modified On: Wed May  7 16:40:17 1997
# Language        : CPerl
# Update Count    : 255
# Status          : Unknown, Use with caution!
# 
# (C) Copyright 1996, Universitt Dortmund, all rights reserved.
# 

require Config;

$ARCH = $Config::Config{archname};
$CONS = new cons (ENV     => {
                              PATH => $ENV{PATH},
                              LD_LIBRARY_PATH => '.',
                             },
                  LIBPATH => "$ARCH/ir", # static version only
                 );

$TEST = "#$ARCH/test";
Export qw(CONS LINK ARCH LIB TEST BIN SCRIPT INCLUDE MAN1 MAN3 LIBDIR
          GERMAN PROXIMITY METACONF);

if (-e 'Configure') {
  metaconfig();
} elsif (-e 'configure') {
  autoconf();
}

if ($ARG{STATIC}) {
  $LIB     = "#$ARCH/ir/libwais.a";
  $LIBWAIS = '-lwais';
  $LDFLAGS = '';
} else {
  # It would be cleaner to prefix 'LIB' and 'LIBWAIS' by '$ARCH'.
  # But that would currently require to install the library as e.g.
  # '/lib/$ARCH/libwais.so' which is not desirable
  $LIB           = "#libwais.so.$VERSION"; 
  $LIBWAIS       = "libwais.so";

  Command $CONS $LIBWAIS, "$LIBWAIS.$VERSION", "ln %< %>";
  Command $CONS "$LIBDIR/$LIBWAIS", "$LIBDIR/$LIBWAIS.$VERSION",  "ln %< %>";
  
  $CONF{CFLAGS} .= $Config::Config{cccdlflags};

  $LDFLAGS       = $Config::Config{ccdlflags}; # path to shared library
  if (!$ARG{export}) {
    $LDFLAGS       =~ s{$Config::Config{installarchlib}/CORE}{$LIBDIR};
  }

  if ($ARCH eq 'sun4-sunos') {
    $LDFLAGS      .= " -L$LIBDIR" ;
    $LIBWAIS       = "-lwais";
  }
  $LDFLAGS      .= " $Config::Config{ldflags}";
  %OVER = (
           SUFLIB  => ".so.$VERSION",
           AR      => $Config::Config{ld},
           LD      => $Config::Config{ld},
           ARCOM   => "%AR %ARFLAGS -o %> %<",
           ARFLAGS => "$Config::Config{lddlflags}",
          );
}

unless (defined $ARG{configure}) {
  # Setup the links and the list of build scripts
  @SCRIPTS = qw(lib ir bin ui x test doc/original-TM-wais/manl);
  @CPPPATH = split ':', $CONS->{CPPPATH};
  unshift @LIBS, split(' ', $CONS->{LIBS});

  if ($CTYPE) {
    push @SCRIPTS, $CTYPE;
    push @CPPPATH, "#$ARCH/$CTYPE";
  }
  if ($REGEXP) {
    push @SCRIPTS, $REGEXP;
    push @CPPPATH, "#$ARCH/$REGEXP";
  }

  # sanity check during developement
  # @SCRIPTS = grep -f "$_/Conscript", @SCRIPTS;

  map (Link("$ARCH/$_",$_), @SCRIPTS);

  @SCRIPTS = map ($_ = "$ARCH/$_/Conscript", @SCRIPTS);

  if (-e 'ir/grundform.c') {    # Uli's private version
    require Config;
    use ExtUtils::Embed;

    my @cpppath = (@CPPPATH, "$Config::Config{installarchlib}/CORE");
    my $libperl = "$Config::Config{installarchlib}/CORE/$Config::Config{libperl}";

    # Temporary hack for my Linux box
    if (-e "/lib/$Config::Config{libperl}") {
      $libperl = $Config::Config{libperl};
    }
    if ($ARG{libperl} and -e $ARG{libperl}) {
      $libperl = $ARG{libperl};
    }
    my @ldops   = ldopts(1);
    my (@libs, @ldflags);
    for (map split, @ldops) {
      if (/^-l/ or -f $_) {
        if ($ARCH ne 'sun4-sunos' and /^-lperl/ && -f $libperl) {
          $_ = $libperl;
        }
        push @libs, $_;
      } else {
        push @ldflags, $_;
      }
    }
    #print "ldopts=@ldops\nlibs=@libs\nldflags=@ldflags\n";
    my %seen;

    # install grundform.c so that ir/Conscript can test for it's
    # presence.
    Install $CONS "$ARCH/ir", 'ir/grundform.c';

    @libs    = grep !$seen{$_}++, ($LIBWAIS,  @LIBS, @libs);
    @ldflags = grep !$seen{$_}++, (split (' ', $CONS->{LDFLAGS}), @libs);
    $LINK = $CONS->clone('CPPPATH' => join(':', @cpppath),
                         'LIBS'    => "@libs",
                         'LDFLAGS' => "@ldflags $LDFLAGS",
                         'CFLAGS'  => "$CONF{CFLAGS} $Config::Config{ccflags}",
                         %OVER
                        );
  } else {
    $LINK = $CONS->clone(LIBS    => join(' ', $LIBWAIS, @LIBS),
                         CFLAGS  => $CONF{CFLAGS},
                         CPPPATH => join(':', @CPPPATH),
                         LDFLAGS => $LDFLAGS,
                         %OVER
                        );
  }
  Build @SCRIPTS;
}                               # else $ARG{configure}

sub autoconf {

  # Prepare configuration. We drop the Makefile generation
  Command $CONS "$ARCH/configure", 'configure',
  qq[sed -e 's:defaults.h Makefile.*test/Makefile:defaults.h:' ].
    qq[    -e 's:ac_unique_file=config.h:ac_unique_file=configure:' %< > %>];

  # copy what configure needs
  Install $CONS $ARCH, qw(config.h.in defaults.h.in);

  # Do configuration.
  # We protect this rule with a cons argument so that 'cons -r' will not
  # remove the targets
  if (defined $ARG{configure}) {
    Command $CONS ["$ARCH/config.status",
                   "$ARCH/defaults.h",
                   "$ARCH/config.h",
                   "$ARCH/config.log"],
    "$ARCH/configure",
    "$ARCH/defaults.h.in",
    "$ARCH/config.h.in",
    qq[cd $ARCH; sh ./configure $ARG{configure}];
  } else {
    die qq[You must run 'cons configure=<configure arguments> $ARCH' first!\n].
      qq[Use for example 'cons configure="--prefix=/usr/local" $ARCH'\n]
        unless
          -e "$ARCH/config.h" and
            -e "$ARCH/config.status" and
              -e "$ARCH/config.h";

    # Read in the configuration parameters
    open(CONF, "$ARCH/config.status") or die "Oops: no '$ARCH/config.status'\n";
    while (<CONF>) {
      if (/^s%\@(\w+)\@%(.*)%g/) {
        $CONF{$1} = $2;
      }
    }
    close CONF;

    # Expand the configuration parameters
    while (($key, $value) = each %CONF) {
      $CONF{$key} = expand($value);
    }

    $BIN       = $CONF{bindir};
    $SCRIPT    = $CONF{bindir};
    $MAN1      = "$CONF{mandir}/man1";
    $MAN3      = "$CONF{mandir}/man3";
    $INCLUDE   = $CONF{libdir}; #$CONF{includedir};
    $INCLUDE   =~ s:/lib:/include:;
    $LIBDIR    = $CONF{libdir};
    $GERMAN    = $CONF{UPPERCASE} =~ //;
    $PROXIMITY = defined $CONF{PROXIMITY} ? 1 : 0;
    @LIBS      = split(' ', $CONF{LIBS});
    $CTYPE     = $CONF{CTYPEDIR};
    $REGEXP    = $CONF{REGEXPDIR};
    $METACONF  =  0;
    $VERSION   = $CONF{FREE_WAIS_SF_VERSION}
  }
}


sub expand {
  my $value = shift;

  if ($value =~ m/\$\{(\w+)\}/) {
    my $nv = expand($CONF{$1});
    $value =~ s/\$\{(\w+)\}/$nv/;
  }
  $value;
}

sub metaconfig {
  # copy what Configure needs
  Install $CONS $ARCH, qw(Configure config_h.SH patchlevel.h);

  # copy files in main directory
  Install $CONS $ARCH, qw(defaults.h confmagic.h);

  my @hints = <hints/*.sh>;
  Install $CONS "$ARCH/hints", @hints;
  @hints = map $_ = "$ARCH/$_", @hints;

  # Do configuration.
  # We protect this rule with a cons argument so that 'cons -r' will not
  # remove the targets
  if (defined $ARG{configure}) {
    Command $CONS ["$ARCH/config.sh",
                   "$ARCH/config.h",
                  ],
    "$ARCH/Configure",
    "$ARCH/config_h.SH",
    @hints,
    qq[cd $ARCH; sh ./Configure $ARG{configure}];
  } else {
    die qq[You must run 'cons configure=<Configure arguments> $ARCH' first!\n].
      qq[Use for example 'cons configure= $ARCH'\n]
        unless
          -e "$ARCH/config.h" and
            -e "$ARCH/config.sh";

    # Read in the configuration parameters
    open(CONF, "$ARCH/config.sh") or die "Oops: no '$ARCH/config.status'\n";
    while (<CONF>) {
      next if /^\#/;
      next if /^\s*$/;
      s/\$\(TOP\)\///;
      if (/^(\w+)=\'(.*)\'/) {
        $CONF{$1} = $2;
      }
    }
    close CONF;

    # CONF{xlibpth} = /usr/lib/386 /lib/386

    $BIN       = $CONF{binexp};
    $SCRIPT    = $CONF{scriptdir};
    $MAN1      = $CONF{mansrcexp};
    $MAN3      = $MAN1; $MAN3 =~ s/1/3/g;
    #$INCLUDE   = $CONF{usrinc};
    $INCLUDE   = $CONF{installprivlib};
    $INCLUDE   =~ s:/lib:/include:;
    $LIBDIR    = $CONF{installprivlib};
    $GERMAN    = $CONF{uchars} =~ //;
    $PROXIMITY = $CONF{d_proximity} eq 'defined';
    @LIBS      = split(' ', $CONF{LIBS});
    $CTYPE     = $CONF{ctypedir};
    $REGEXP    = $CONF{regexpdir};
    $METACONF  =  1;
    open(IN, 'patchlevel.h') or die "Could not read 'patchlevel.h': $!\n";
    while (<IN>) {
      if (/VERSION ([\d.]+)/) {
        $VERSION = $1;
      } elsif (/PATCHLEVEL (\d+)/) {
        $VERSION .= '.'.$1;
      } 
    }
    close IN;
  }
}

