#!/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`;
  $? == 0 or die "Error Could not find makef95makefile line in $dir/mkmk";
  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/ .*//'`;
my $version=`grep 'private_version_string *=' src/welcome_message.f90`;# | sed -e 's/.*v\. //' -e 's/ .*//'`;
$? == 0 or die "ERROR: Could not find version line in src/welcome_message.f90";
chomp $version;
$version =~ s/.*?\"//;
$version =~ s/\".*//;
$prefix=`cat src/.makef95.prefix`;
$? == 0 or die "ERROR: Could not find prefix in 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");
$? == 0 or die "ERROR: Could not create hoppet-config script";
system("chmod +x hoppet-config");
$? == 0 or die "ERROR: Could not make hoppet-config executable";

# write a file config.log
open (LOG,">config.log") || die "ERROR: 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;

print "\nHOPPET has been configured for installation in $prefix\n";
print "You can now type 'make' to compile, and 'make install' to install\n";
print "You can use the script hoppet-config to find out where things are\n";
print "You can reconfigure by running this script again\n";


my $yellow="\033[0;33m";
my $grey="\033[1;30m";
my $blue="\033[0;34m";
my $nocolor="\033[0m";
print "${yellow}\n";
print "WARNING:$nocolor this configure script is DEPRECATED as of version 2.0.0\n";
print "and will be removed in a future version of HOPPET.\n";
print "It is provided for backward compatibility only.\n";
print "You should prefer cmake instead:\n\n";
print "    make distclean  ${blue}# only if you've already run configure$nocolor\n";
print "    cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/install/path $blue<+optional flags>$nocolor\n";
print "    cmake build -LH  $blue# to see what extra options are available (e.g. -D...=ON/OFF)$nocolor\n";
print "    cmake --build  build -j \n";
print "    ctest --test-dir build  -j4\n";
print "    cmake --install build\n";
