#!/usr/bin/perl
######################################################################
# Prelude
######################################################################

# Yes I know about autoconf ... and autoconf sux.

# assume standard: binutils, gcc, as, ld, gnu toolchain
# assume standard: make, sh?, perl?
# assume standard: diffutils, fileutils, findutils, grep, patchutils, sh-utils
## assume standard: diff, patch



my $project = 
    "yacfe";
my $projectcmdline = 
    "./yacfe demos/foo.c";

######################################################################
# Options
######################################################################

my $prefix="/usr/local";

my $bt=0;
my $mpi=0;

local $_ = join ' ', @ARGV;

# Parse options
/-h/ || /--help/ and die "usage: $0 [--prefix=path] [--with/out-gui] [--with/out-gdbm] [--with/out-mpi]\n";
/--prefix=([^ ]*)/ and $prefix = $1;

/--with-backtrace/ and $bt = 1;
/--without-backtrace/ and $bt = 0;

my $src="$prefix/share/$project";

######################################################################
# Side effects
######################################################################

######################################################################
# Helpers
######################################################################
BEGIN { die "need Perl 5 or greater" if $] < 5 ; }

#use Common;
sub pr2 { print STDERR "@_\n" }
sub cat { 
    my ($what) = @_;
    my @list;
    open(TMP, $what);
    while(<TMP>) { push @list, "$_"; }
    \@list;
}
sub notb   { !$_[0] }
sub mapf   { my ($f, $xs) = @_; [ map { &$f($_) } @{$xs} ] }
sub plural { my ($e) = @_; if ($e > 1) { "s" } else { "" } }

sub check_config { my ($command, $expect, $msggood, $msgbad) = @_;
    my $error = 0;
		   
    my $full = cat($command);
    my $res = join(" ", @{$full});
#	       pr2 $res;
    if(notb($res =~ $expect)) { $error++; pr2 "!!!! $msgbad !!!!"; }
    else { pr2 $msggood }
    return $error;
}
######################################################################
# Let's go
######################################################################

pr2 "Checking your configuration.\n";

my $error = 0;

#---------------------------------------------------------------------
# Compilers and runtimes
#---------------------------------------------------------------------
$error += 
    check_config("echo \"1;;\\n\" | ocaml |",
                 "Objective(.*) 3.*",
                 "OCaml (the wonderful language) is present.",
                 "The program ocaml is missing or is not a good version. We need at least 3.09",
                 );

#---------------------------------------------------------------
# Developers tools
#---------------------------------------------------------------

pr2 "";

$error += check_config(
  "make -v 2>&1 |grep Make|",
  "GNU Make 3\.[0-9]+", #version 3.81
  "make (gnu version) is present.",
  "The program gnu make is missing or is not a good version.
We need  3.XX",
);


#---------------------------------------------------------------------
# More developers tools
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Librairies
#---------------------------------------------------------------------

######################################################################
# Generate config files (platform/portability issues)
######################################################################

######################################################################
# Generate globals files (features issues)
######################################################################

######################################################################
# Diagnostic 
######################################################################

if($error) { 
    pr2 "
----------------------------------------------------------------------
!!!! There seems to have problem, we have found $error missing package" . 
plural($error) . ".
" . (($error > 1) ? "Some of those packages" : "This package") . 
    " may be installed by picking " . ($error > 1 ? "them" : "it") . 
    " in $project-dependencies.tgz available
on the $project website. !!!!
----------------------------------------------------------------------
";
} else { 

    pr2 "
----------------------------------------------------------------------

All seems fine for $project.

To compile $project type:
 make depend; make

Then, to test $project simply type:
 $projectcmdline

----------------------------------------------------------------------
";

}


######################################################################
# Generating the configuration
######################################################################

pr2 "$project target prefix: $prefix (you can use --prefix to override it)";
pr2 "Generating Makefile.config (BT=$bt)";
open(CONFIG, ">Makefile.config");
print CONFIG "# autogenerated by configure

# Where to install the binary
BINDIR=$prefix/bin

# Where to install the man pages
MANDIR=$prefix/man

# Where to install the lib
LIBDIR=$prefix/lib

# Where to install the configuration files
SHAREDIR=$src

# Features
FEATURE_BACKTRACE=$bt
FEATURE_MPI=$mpi
";

pr2 "Generating commons/features.ml";
`cpp -DFEATURE_MPI=$mpi -DFEATURE_BACKTRACE=$bt commons/features.ml.cpp > commons/features.ml`;
#pr2 "Generating features.ml (GDBM=$gdbm, GUI=$gui, MPI=$mpi)";
#`make features.ml`;


pr2 "Modifying globals/config.ml";
my $command = "perl -p -i -e 's#Not_found.\*#Not_found->\\\"$src\\\"#' globals/config.ml";
`$command`;


