#!/usr/bin/perl -w
#
# script to configure hoppet. To find out more type
#
# ./configure --help 
#
# GPS created 2009-09-05
#----------------------------------------------------------------------
use Cwd;
use strict;

my $usage="
Usage: 
  ./configure [--prefix=...] [FC='...'] [FFLAGS='...'] [LDFLAGS='...']

  --prefix='...'        sets the location for installation
  --mod-prefix='...'    sets the location for installation of modules (by default prefix/include/hoppet)
  --enable-exact-coefs  enables compilation of exact coefficient functions (default is off; NB even when on, they still need to be explicitly requested in the calling code)
  FC='...'              sets the fortran (and f90) compiler
  FFLAGS='...'          sets the fortran (and f90) compiler options
  LDLAGS='...'          sets the link options
";



if ($#ARGV >=0 && ($ARGV[0] eq "-h" || $ARGV[0] eq "--help")) {
  print "$usage";
  exit;
}

# top-level Makefile is in scripts/ to reduce clutter 
# in top-level directory and reduce temptation to run make
# without having configured first
system("cp -p scripts/Makefile.in Makefile");

my $configline=$0." ".join(" ",@ARGV);
my $prefix="/usr/local";
my $modPrefix="";
my $enableExactCoefs=0;

my @args;
foreach my $arg (@ARGV) {
  if    ($arg =~ /^--prefix=(.*)/) { $prefix= $1;}
  elsif ($arg =~ /^--mod-prefix=(.*)/) { $modPrefix= $1;}
  elsif ($arg =~ /^--enable-exact-coefs/) { $enableExactCoefs=1;}
  else {
    if ($arg =~ /^--/) {die "Unknown option $arg\n";}
    push @args, "\"".$arg."\"";
  }
}
push @args, "--prefix=$prefix";
if (!$modPrefix) {$modPrefix = "$prefix/include/hoppet";}
push @args, "--mod-prefix=$modPrefix";

if ($enableExactCoefs) {print "Enabling compilation of exact coefficient functions\n";}

# NB: src comes last, because from src onwards we will include an option to install modules
my @dirs=split(" ","example_f90 benchmarking benchmarking/test_acc src");

my $topdir = getcwd;

# copy a dummy git state file (git state is currently only supported with cmake)
system("cp src/config/hoppet_git_state.f90.in src/config/hoppet_git_state.f90") == 0
  or die "Could not copy git state file: $!";

# now generate the makefiles
foreach my $dir (@dirs) {
  # when we reach src, ensure that modules get installed
  if ($dir eq "src") {push @args, "--install-modules";}
  chdir $dir;
  
  print "Creating makefile in ".getcwd."\n";
  # find out what we're already up to
  my $mkmk=`grep -v '^#' mkmk | grep makef95makefile`;
  chomp($mkmk);
  $mkmk =~ s/\$[^ ]+//g;
  $mkmk =~ s/-remake.*//;
  $mkmk .= " ".join(" ",@args);

  # this code enables alternative sets of files to be used in compilation
  # depending on whether the user wants exact coefficient functions; the
  # alternatives should live in sub-directories exact-coefs/ and no-exact-coefs/
  $mkmk .= " -srcdir param-coefs -srcdir splitting-functions -srcdir mass-thresholds";
  if ($enableExactCoefs && -d "exact-coefs") {$mkmk .= " -srcdir exact-coefs";}
  elsif (!$enableExactCoefs && -d "no-exact-coefs") {$mkmk .= " -srcdir no-exact-coefs";}
  if (-d "config") {$mkmk .= " -srcdir config";}
  #print $mkmk."\n";
  print $dir, $mkmk,"\n";
  system($mkmk);
  chdir $topdir;
}

# get things ready for the hoppet-config script
my $version=`grep 'Welcome to' src/welcome_message.f90 | sed -e 's/.*v\. //' -e 's/ .*//'`;
chomp $version;
$prefix=`cat src/.makef95.prefix`;
chomp $prefix;
(my $escprefix    = $prefix)    =~ s/\//\\\//g;
(my $escmodprefix = $modPrefix) =~ s/\//\\\//g;
system("sed -e 's/\@prefix\@/$escprefix/' -e 's/\@modprefix\@/$escmodprefix/' -e 's/\@VERSION\@/$version/'  scripts/hoppet-config.in > hoppet-config");
system("chmod +x hoppet-config");

# write a file config.log
open (LOG,">config.log") || die "Could not open config.log";
print LOG "# the last configure that was run was\n";
print LOG "$configline\n";
print LOG "prefix=$prefix\n";
close LOG;
