#! /usr/bin/perl -w
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GENIE configuration script
#
# For help, type ./configure --help
#
# C.Andreopoulos <costas.andreopoulos \at stfc.ac.uk>, Rutherford Lab.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# print out info & exit if any of the command-line arguments was --help
#
if(($match = grep(/--help/i, @ARGV)) > 0) { 
  print "\n";
  print "*** GENIE configure script *** \n\n";
  print "Usage: ./configure [option]... [flag=value]... \n\n";
  print "    FLAG              DESCRIPTION                                              DEFAULT\n\n";
  print "    --prefix          installation location (for 'make install')               /usr/local/\n";
  print "\n enable/disable options with either --enable- or --disable- (eg --enable-lhapdf --disable-flux-drivers)\n\n";
  print "    profiler          GENIE code profiling using Google perftools              default: disabled \n";
  print "    doxygen-doc       Generate doxygen documentation at build time             default: disabled \n";
  print "    dylibversion      Adds version number in library names (recommended)       default: enabled  \n";
  print "    lowlevel-mesg     Disable (rather than filter out at run time) prolific    default: disabled \n";
  print "                      debug/info level messages known to slow GENIE down       \n";
  print "    debug             Adds -g in the compiler options to request debug info    default: disabled \n";
  print "    neut-cascade      Alt. intranuclear hadron transport MC, requires NEUT     default: disabled (T2K internal) \n";
  print "    gibuu             Alt. intranuclear hadron transport MC, requires GiBUU    default: disabled (devel) \n";
  print "    lhapdf            Use the LHAPDF parton density function library           default: enabled  \n";
  print "    cernlib           Use the CERN libraries                                   default: disabled (to be phased-out / please use LHAPDF instead)\n";
  print "    flux-drivers      Built-in flux drivers                                    default: enabled  \n";
  print "    geom-drivers      Built-in detector geometry drivers                       default: enabled  \n";
  print "    mueloss           Muon energy loss modeling                                default: enabled  \n";
  print "    validation-tools  GENIE physics model validation tools (requires MySQL)    default: disabled \n";
  print "    viewer            Event generation GUI (incomplete)                        default: disabled \n";
  print "    test              Build test programs                                      default: disabled \n";
  print "    t2k               Enable T2K-specific ev.gen. drivers and tools            default: disabled \n";
  print "    minos             Enable MINOS-specific ev.gen. drivers and tools          default: disabled \n";
  print "\n options for 3rd party software, prefix with --with- (eg --with-lhapdf-lib=/some/path/)\n\n";
  print "    optimiz-level     Compiler optimization        any of O,O2,O3,OO,Os / default: O2 \n";
  print "    profiler-lib      Path to profiler library     needed if you --enable-profiler \n";
  print "    doxygen-path      Doxygen binary path          needed if you --enable-doxygen-doc  (if unset: checks for a \$DOXYGENPATH env.var.) \n";
  print "    neut-lib          Path to the NEUT libraries   needed if you --enable-neut-cascade (if unset: checks for a \$NEUT_LIB env.var.) \n";
  print "    gibuu-lib         Path to GiBUU library        needed if you --enable-gibuu        (if unset: checks for a \$GIBUU_LIB env.var.) \n";
  print "    pythia6-lib       PYTHIA6 library path         always needed                       (if unset: checks for a \$PYTHIA6 env.var.,    then tries to auto-detect it) \n";
  print "    cern-lib          CERN libraries path          needed if you --enable-cernlib      (if unset: checks for a \$CERNLIB env.var.,    then tries to auto-detect it) \n";
  print "    lhapdf-inc        Path to LHAPDF includes      needed if you --enable-lhapdf       (if unset: checks for a \$LHAPDF_INC env.var., then tries to auto-detect it) \n";
  print "    lhapdf-lib        Path to LHAPDF libraries     needed if you --enable-lhapdf       (if unset: checks for a \$LHAPDF_INC env.var.. then tries to auto-detect it) \n";
  print "    libxml2-inc       Path to libxml2 includes     always needed                       (if unset: tries to auto-detect it) \n";
  print "    libxml2-lib       Path to libxml2 library      always needed                       (if unset: tries to auto-detect it) \n";
  print "    log4cpp-inc       Path to log4cpp includes     always needed                       (if unset: tries to auto-detect it) \n";
  print "    log4cpp-lib       Path to log4cpp library      always needed                       (if unset: tries to auto-detect it) \n\n";
  print "If the printout was too long then pipe it through a pager, eg: \n";
  print "./configure --help | more \n";
  exit 0;
}

# Check that $GENIE is set
#
$GENIE = $ENV{'GENIE'};
die ("*** Error *** The GENIE environmental variable (pointing to the top level GENIE directory) is not defined") 
unless defined $GENIE;

# Check that $ROOTSYS is set
#
$ROOTSYS = $ENV{'ROOTSYS'};
die ("*** Error *** The ROOTSYS environmental variable is not defined. Is ROOT installed?") 
unless defined $ROOTSYS;

# Print GENIE banner 
#
$BANNER_FILE = "$GENIE/data/banner/BANNER.txt";
if(-e $BANNER_FILE) {
  open(BANNER, "<$BANNER_FILE");
  @hdr=<BANNER>;
  print @hdr;
  close(BANNER);
}

# Enable auto-detection?
#
system("find $GENIE/configure");
print "\n";
$auto_detect = ($?==0) ? 1 : 0;
if(! $auto_detect) {
     print "\n*** Warning *** Path auto-detection is turned off. You need the 'find' utility to use that feature\n\n";
}

# Open Make.config to write configuration options
#
$MKCONF_FILE = "$GENIE/src/make/Make.config";
open(MKCONF, ">$MKCONF_FILE") or die("Can not write out the Make.config file!");

print MKCONF "# \n";
print MKCONF "# Make.config \n";
print MKCONF "# This file was automatically generated by the 'configure' script  \n";
print MKCONF "# and is included into the project Makefiles  \n";
print MKCONF "# \n";

# Create a string by joining all the command line arguments
#
my $options = join(" ", @ARGV);

# Get & save installation location (--prefix) or set default
#
my $prefix="/usr/local/";
if(($match = grep(/--prefix/i, @ARGV)) > 0) { 
  $options=~m/--prefix=(\S*)/i;
  $prefix = $1;
  if( $GENIE eq $prefix ) {
     print "*** Error *** --prefix can not point to the GENIE top level directory!\n\n";
     exit 1;
  }
}
print MKCONF "GENIE_INSTALLATION_PATH=$prefix\n";

# Default --enable/--disable config options (for a minimal genie build)
#
my $gopt_enable_profiler         = "NO";
my $gopt_enable_doxygen_doc      = "NO";
my $gopt_enable_dylibversion     = "YES";
my $gopt_enable_lowlevel_mesg    = "NO";
my $gopt_enable_debug            = "NO";
my $gopt_enable_neut_cascade     = "NO";
my $gopt_enable_gibuu            = "NO";
my $gopt_enable_lhapdf           = "YES";
my $gopt_enable_cernlib          = "NO";
my $gopt_enable_flux_drivers     = "YES";
my $gopt_enable_geom_drivers     = "YES";
my $gopt_enable_mueloss          = "YES";
my $gopt_enable_validation_tools = "NO";
my $gopt_enable_viewer           = "NO";
my $gopt_enable_test             = "NO";
my $gopt_enable_minos            = "NO";
my $gopt_enable_t2k              = "NO";
#my $gopt_enable_neugen          = "NO";

# Check configure's command line arguments for non-default values
#
if(($match = grep(/--enable-profiler/i,         @ARGV)) > 0) { $gopt_enable_profiler         = "YES"; }
if(($match = grep(/--enable-doxygen-doc/i,      @ARGV)) > 0) { $gopt_enable_doxygen_doc      = "YES"; }
if(($match = grep(/--disable-dylibversion/i,    @ARGV)) > 0) { $gopt_enable_dylibversion     = "NO";  }
if(($match = grep(/--enable-lowlevel-mesg/i,    @ARGV)) > 0) { $gopt_enable_lowlevel_mesg    = "YES"; }
if(($match = grep(/--enable-debug/i,            @ARGV)) > 0) { $gopt_enable_debug            = "YES"; }
if(($match = grep(/--enable-neut-cascade/i,     @ARGV)) > 0) { $gopt_enable_neut_cascade     = "YES"; }
if(($match = grep(/--enable-gibuu/i,            @ARGV)) > 0) { $gopt_enable_gibuu            = "YES"; }
if(($match = grep(/--disable-lhapdf/i,          @ARGV)) > 0) { $gopt_enable_lhapdf           = "NO";  }
if(($match = grep(/--enable-cernlib/i ,         @ARGV)) > 0) { $gopt_enable_cernlib          = "YES"; }
if(($match = grep(/--disable-flux-drivers/i,    @ARGV)) > 0) { $gopt_enable_flux_drivers     = "NO";  }
if(($match = grep(/--disable-geom-drivers/i,    @ARGV)) > 0) { $gopt_enable_geom_drivers     = "NO";  }
if(($match = grep(/--disable-mueloss/i,         @ARGV)) > 0) { $gopt_enable_mueloss          = "NO";  }
if(($match = grep(/--enable-validation-tools/i, @ARGV)) > 0) { $gopt_enable_validation_tools = "YES"; }
if(($match = grep(/--enable-viewer/i,           @ARGV)) > 0) { $gopt_enable_viewer           = "YES"; }
if(($match = grep(/--enable-test/i,             @ARGV)) > 0) { $gopt_enable_test             = "YES"; }
if(($match = grep(/--enable-minos/i,            @ARGV)) > 0) { $gopt_enable_minos            = "YES"; }
if(($match = grep(/--enable-t2k/i,              @ARGV)) > 0) { $gopt_enable_t2k              = "YES"; }
#if(($match = grep(/--enable-neugen/i,          @ARGV)) > 0) { $gopt_enable_neugen           = "YES"; }

my $gopt_with_cxx_debug_flag="";
if($gopt_enable_debug eq "YES") { $gopt_with_cxx_debug_flag = "-g"; }


# A couple of warnings for recently modified configuration options
#
if($options=~m/--enable-minos-evserv/i) {
  print "\n";
  print "*** Warning *** Note that the option --enable-minos-evserv has been renamed to --enable-minos \n";
  print "*** Warning *** The MINOS-specific tools will not be enabled if you mis-type the configuration option.\n";
  print "*** Warning *** Please see './configure --help' for all config option updates \n\n";
}
if($options=~m/--enable-t2k-evgen/i) {
  print "\n";
  print "*** Warning *** Note that the option --enable-t2k-evgen has been renamed to --enable-t2k \n";
  print "*** Warning *** The T2K-specific tools will not be enabled if you mis-type the configuration option.\n";
  print "*** Warning *** Please see './configure --help' for all config option updates \n\n";
}

# Check for --with options:

# Check compiler optimization level
#
my $gopt_with_cxx_optimiz_flag="O2"; # default
if( $options=~m/--with-optimiz-level=(\S*)/i ) {
  $gopt_with_cxx_optimiz_flag = $1;
}

# If --enable-profiler was set then the full path to the profiler library must be specified
#
my $gopt_with_profiler_lib = "";
if($gopt_enable_profiler eq "YES") {
  if(($match = grep(/--with-profiler-lib/i, @ARGV)) > 0) { 
    $options=~m/--with-profiler-lib=(\S*)/i;
    $gopt_with_profiler_lib = $1;
  }
}

# If --enable-doxygen-doc was set then the full path to the doxygen binary path must be specified
# unless it is in the $PATH
#
my $gopt_with_doxygen_path = "";
if($gopt_enable_doxygen_doc eq "YES") {
  if(($match = grep(/--with-doxygen-path/i, @ARGV)) > 0) { 
    $options=~m/--with-doxygen-path=(\S*)/i;
    $gopt_with_doxygen_path = $1;
  }
  # if it was not set, try to pick it up from the environment
  if(! -f $gopt_with_doxygen_path && defined $ENV{'DOXYGENPATH'}) { $gopt_with_doxygen_path = $ENV{'DOXYGENPATH'}; }
  # complain
  if(! -f $gopt_with_doxygen_path) {
     print "*** Error *** You need to specify the path to doxygen using --with-doxygen-path=/some/path/\n";
     print "*** Error *** Otherwise, you should --disable-doxygen-doc\n\n";
     exit 1;
  }
}

# If --enable-neugen was set then the location to the neugen installation must be specified
#
#my $gopt_with_neugen_path = "";
#if($gopt_enable_neugen eq "YES") {
#  if(($match = grep(/--with-neugen-path/i, @ARGV)) > 0) { 
#    $options=~m/--with-neugen-path=(\S*)/i;
#    $gopt_with_neugen_path = $1;
#  }
#  # if it was not set, try to pick it up from the environment
#  if($gopt_with_neugen_path eq "" && defined $ENV{'NEUGEN3PATH'}) { $gopt_with_neugen_path = $ENV{'NEUGEN3PATH'}; }
#  # check
#  my $file = "$gopt_with_neugen_path/lib/libneugen3.a";
#  if(! -e $file) {
#     print "*** Error *** You need to specify the top level neugen3 directory using --with-neugen-path=/some/path/\n";
#     print "*** Error *** Otherwise, you should --disable-neugen\n\n";
#     exit 1;
#  }
#}

# If --enable-neut-cascade was set then the full path to the libNEUT.a library must be specified
#
my $gopt_with_neut_lib = "";
if($gopt_enable_neut_cascade eq "YES") {
  if(($match = grep(/--with-neut-lib/i, @ARGV)) > 0) { 
    $options=~m/--with-neut-lib=(\S*)/i;
    $gopt_with_neut_lib = $1;
  }
  # if it was not set, try to pick it up from the environment
  if($gopt_with_neut_lib eq "" && defined $ENV{'NEUT_LIB'}) { $gopt_with_neut_lib = $ENV{'NEUT_LIB'}; }
  # check
  my @libs = `ls $gopt_with_neut_lib`;
  my %libs_found = (
        'libneutcore'   => 0,
        'libnuccorrspl' => 0,
        'libnuceff'     => 0,
        'libpartnuck'   => 0,
        'libskmcsvc'    => 0  );
  foreach(@libs) {
     chomp($_);
     if ( m/libneutcore/i   ) { $libs_found{'libneutcore'}   = 1; } #print "Found NEUT library: $_ \n"; }
     if ( m/libnuccorrspl/i ) { $libs_found{'libnuccorrspl'} = 1; } #print "Found NEUT library: $_ \n"; }
     if ( m/libnuceff/i     ) { $libs_found{'libnuceff'}     = 1; } #print "Found NEUT library: $_ \n"; }
     if ( m/libpartnuck/i   ) { $libs_found{'libpartnuck'}   = 1; } #print "Found NEUT library: $_ \n"; }
     if ( m/libskmcsvc/i    ) { $libs_found{'libskmcsvc'}    = 1; } #print "Found NEUT library: $_ \n"; }
  }
  for my $lib_key (keys %libs_found) {
    my $found = $libs_found{$lib_key};
    if ($found == 0) {
       print "*** Error *** You need to specify the path to the NEUT $lib_key.<version>.a library using --with-neut-lib=/some/path/\n";
       print "*** Error *** Otherwise, you should --disable-neut-cascade\n\n";
       exit 1;
    }
  }
}

# If --enable-gibuu was set then the full path to the libGiBUU.a library must be specified
#
my $gopt_with_gibuu_lib = "";
if($gopt_enable_gibuu eq "YES") {
  if(($match = grep(/--with-gibuu-lib/i, @ARGV)) > 0) { 
    $options=~m/--with-gibuu-lib=(\S*)/i;
    $gopt_with_gibuu_lib = $1;
  }
  # if it was not set, try to pick it up from the environment
  if($gopt_with_gibuu_lib eq "" && defined $ENV{'GIBUU_LIB'}) { $gopt_with_gibuu_lib = $ENV{'GIBUU_LIB'}; }
  # check
  my $file = "$gopt_with_gibuu_lib/libGiBUU.a";
  if(! -e $file) {
     print "*** Error *** You need to specify the path to the GiBUU library using --with-gibuu-lib=/some/path/\n";
     print "*** Error *** Otherwise, you should --disable-gibuu\n\n";
     exit 1;
  }
}

# If --enable-lhapdf was set then the full path to the LHAPDF library must be specified
#
my $gopt_with_lhapdf_lib = "";
if($gopt_enable_lhapdf eq "YES") {
  if(($match = grep(/--with-lhapdf-lib/i, @ARGV)) > 0) { 
    $options=~m/--with-lhapdf-lib=(\S*)/i;
    $gopt_with_lhapdf_lib = $1;
  }
  if(! -d $gopt_with_lhapdf_lib)  {
    print "\n*** Warning *** LHAPDF has been enabled but you didn't specify the LHAPDF library path \n";
  }
  # if it was not set, try to pick it up from the environment
  if($gopt_with_lhapdf_lib eq "" && defined $ENV{'LHAPDF_LIB'}) { 
     $gopt_with_lhapdf_lib = $ENV{'LHAPDF_LIB'}; 
     print "The \$LHAPDF_LIB env var is defined. I will pick that and use --with-lhapdf-lib=$gopt_with_lhapdf_lib\n";
  }
  # if it still not set, try autodetecting it
  if(! -d $gopt_with_lhapdf_lib && $auto_detect)  {
    print "Auto-detecting LHAPDF library path...\n";
    $matched = auto_detect("libLHAPDF.a");
    if( $matched=~m/(\S*)\/libLHAPDF.a/i ) {
       $gopt_with_lhapdf_lib = $1;
    }
    print "Setting --with-lhapdf-inc=$gopt_with_lhapdf_lib\n";
  }
  # check
  my $file = "$gopt_with_lhapdf_lib/libLHAPDF.a";
  if(! -e $file) {
     print "*** Error *** You need to specify the path to the libLHAPDF.so library using --with-lhapdf-lib=/some/path/\n";
     print "*** Error *** Otherwise, you should --disable-lhapdf\n\n";
     exit 1;
  }
}

# If --enable-lhapdf was set then the full path to the LHAPDF includes must be specified
#
my $gopt_with_lhapdf_inc = "";
if($gopt_enable_lhapdf eq "YES") {
  if(($match = grep(/--with-lhapdf-inc/i, @ARGV)) > 0) { 
    $options=~m/--with-lhapdf-inc=(\S*)/i;
    $gopt_with_lhapdf_inc = $1;
  }
  if(! -d $gopt_with_lhapdf_inc)  {
    print "\n*** Warning *** LHAPDF has been enabled but you didn't specify the LHAPDF include path \n";
  }
  # if it was not set, try to pick it up from the environment
  if($gopt_with_lhapdf_inc eq "" && defined $ENV{'LHAPDF_INC'}) { 
     $gopt_with_lhapdf_inc = $ENV{'LHAPDF_INC'}; 
     print "The \$LHAPDF_INC env var is defined. I will pick that and use --with-lhapdf-inc=$gopt_with_lhapdf_inc\n";
  }
  # if it still not set, try autodetecting it
  if(! -d $gopt_with_lhapdf_inc && $auto_detect)  {
    print "Auto-detecting the LHAPDF include path...\n";
    $matched = auto_detect("LHAPDF.h","LHAPDF/LHAPDF.h");
    print "$gopt_with_lhapdf_inc  \n";
    if( $matched=~m/(\S*)\/LHAPDF\/LHAPDF.h/i ) {
       $gopt_with_lhapdf_inc = $1;
    }
    print "Setting --with-lhapdf-inc=$gopt_with_lhapdf_inc\n";
  }
  # check
  my $file = "$gopt_with_lhapdf_inc/LHAPDF/LHAPDF.h";
  if(! -e $file) {
     print "*** Error *** You need to specify the LHAPDF includes path using --with-lhapdf-inc=/some/path/\n";
     print "*** Error *** Otherwise, you should --disable-lhapdf\n\n";
     exit 1;
  }
}

# If --enable-cernlib was set then the full path to the CERNLIBs must be specified
#
my $gopt_with_cern_lib = "";
if($gopt_enable_cernlib eq "YES") {
  if($options=~m/--with-cernlib=\S*/i) {
     print "*** Error *** Note that the option --with-cernlib has been renamed to --with-cern-lib \n";
     print "*** Error *** Please see './configure --help' for all config option updates \n\n";
     exit 1;
  }
  if($gopt_enable_lhapdf eq "YES") {
     print "\n";
     print "*** Warning *** You have already enabled the LHAPDF library: The CERNLIB library dependency is now redundant. \n";
     print "*** Warning *** No action is required but it is safe for you _not_ to --enable-cernlib. \n\n";
  } else {
     print "\n";
     print "*** Warning *** Using CERNLIB in GENIE is functional but is becoming depreciated. \n";
     print "*** Warning *** Please start using LHAPDF instead. \n\n";
  }
  if($options=~m/--with-cern-lib=(\S*)/i) {
     $gopt_with_cern_lib = $1;
   }
   if(! -d $gopt_with_cern_lib) { 
     print "\n*** Warning *** CERNLIB has been enabled but you didn't specify the CERNLIB path \n";
   }
   # if it was not set, try to pick it up from the environment
   if(! -d $gopt_with_cern_lib && defined $ENV{'CERNLIB'}) { 
     $gopt_with_cern_lib = $ENV{'CERNLIB'}; 
     print "The \$CERNLIB env var is defined. I will pick that and use --with-cern-lib=$gopt_with_cern_lib\n";
   }
   # if it still not set, try autodetecting it
   if(! -d $gopt_with_cern_lib && $auto_detect) 
   {
     print "Auto-detecting the CERNLIB path...\n";
     $matched = auto_detect("libpdflib*.*");
     if( $matched=~m/(\S*)\/libpdflib\S*/i ) {
        $gopt_with_cern_lib = $1;
     }
     print "Setting --with-cern-lib=$gopt_with_cern_lib\n";
   }
   if(! -d $gopt_with_cern_lib) {
       print "*** Error *** Could not locate the CERNLIB path. Please specify it using --with-cern-lib=/some/path/\n\n";
       exit 1;
   }
} 

# Check that at least one of CERNLIB and LHAPDF is enabled
#
if($gopt_enable_lhapdf eq "NO" && $gopt_enable_cernlib eq "NO") {
   print "*** Error *** You can not disable both LHAPDF and CERNLIB. \n";
   print "*** Error *** Please enable one (preferably LHAPDF). \n\n";
   exit 1;
}

# If --enable-validation-tools was set then ROOT must have mysql support enabled
#
if($gopt_enable_validation_tools eq "YES") {

}

# Get pythia6
#

my $gopt_with_pythia6_lib = "";	
if($options=~m/--with-pythia6=\S*/i) {
    print "*** Error *** Note that the option --with-pythia6 has been renamed to --with-pythia6-lib \n";
    print "*** Error *** Please see './configure --help' for all config option updates \n\n";
    exit 1;
}
if($options=~m/--with-pythia6-lib=(\S*)/i) {
  $gopt_with_pythia6_lib = $1;
} else {
  print "\n*** Warning *** You didn't specify the PYTHIA6 path \n";
}
# if it was not set, try to pick it up from the environment
if(! -d $gopt_with_pythia6_lib && defined $ENV{'PYTHIA6'}) { 
  $gopt_with_pythia6_lib = $ENV{'PYTHIA6'}; 
  print "The \$PYTHIA6 env var is defined. I will pick that and set --with-pythia6-lib=$gopt_with_pythia6_lib\n";
}
# if it still not set, try autodetecting it
if(! -d $gopt_with_pythia6_lib && $auto_detect) 
{
  print "Auto-detecting the PYTHIA6 path...\n";
  $matched = auto_detect("libPythia6*");
  if( $matched=~m/(\S*)\/libPythia6\S*/i ) {
     $gopt_with_pythia6_lib = $1;
  }
  print "Setting --with-pythia6-lib=$gopt_with_pythia6_lib\n";
}
if(! -d $gopt_with_pythia6_lib) {
    print "*** Error *** Could not locate the PYTHIA6 library path. Please specify it using --with-pythia6=/some/path/\n\n";
    exit 1;
} 

# Get libxml2 and log4cpp include and library paths 
#

my $gopt_with_libxml2_inc = "";
my $gopt_with_libxml2_lib = "";
my $gopt_with_log4cpp_inc = "";
my $gopt_with_log4cpp_lib = "";

#
# --with-libxml2-inc=
#
if($options=~m/--with-libxml2-inc=(\S*)/i) {
  $gopt_with_libxml2_inc = $1;
}
if(! -d $gopt_with_libxml2_inc && $auto_detect)  {
  print "\n*** Warning *** You didn't specify the libxml2 include path \n";
  print "Auto-detecting...\n";
  $matched = auto_detect("xmlmemory.h");
  if( $matched=~m/(\S*)\/libxml\/xmlmemory.h/i ) {
     $gopt_with_libxml2_inc = $1;
  }
  print "Setting --with-libxml2-inc=$gopt_with_libxml2_inc\n";
}
if(! -d $gopt_with_libxml2_inc) {
  print "*** Error *** You need to specify the libxml2 include path using --with-libxml2-inc=/some/path/\n\n";
  exit 1;
}

#
# --with-libxml2-lib=
#
if($options=~m/--with-libxml2-lib=(\S*)/i) {
  $gopt_with_libxml2_lib = $1;
}
if(! -d $gopt_with_libxml2_lib && $auto_detect)  {
  print "\n*** Warning *** You didn't specify the libxml2 library path \n";
  print "Auto-detecting...\n";
  $matched = auto_detect("libxml2.*");
  if( $matched=~m/(\S*)\/libxml2\S*/i ) {
     $gopt_with_libxml2_lib = $1;
  }
  print "Setting --with-libxml2-lib=$gopt_with_libxml2_lib\n";
}
if(! -d $gopt_with_libxml2_lib) {
  print "*** Error *** You need to specify the libxml2 library path using --with-libxml2-lib=/some/path/\n\n";
  exit 1;
}

#
# --with-log4cpp-inc=
#
if($options=~m/--with-log4cpp-inc=(\S*)/i) {
  $gopt_with_log4cpp_inc = $1;
}
if(! -d $gopt_with_log4cpp_inc && $auto_detect)  {
  print "\n*** Warning *** You didn't specify the log4cpp include path \n";
  print "Auto-detecting...\n";
  $matched = auto_detect("OstreamAppender.hh");
  if( $matched=~m/(\S*)\/log4cpp\/OstreamAppender.hh/i ) {
     $gopt_with_log4cpp_inc = $1;
  }
  print "Setting --with-log4cpp-inc=$gopt_with_log4cpp_inc\n";
}
if(! -d $gopt_with_log4cpp_inc) {
  print "*** Error *** You need to specify the log4cpp include path using --with-log4cpp-inc=/some/path/\n\n";
  exit 1;
}

#
# --with-log4cpp-lib=
#
if($options=~m/--with-log4cpp-lib=(\S*)/i) {
  $gopt_with_log4cpp_lib = $1;
}
if(! -d $gopt_with_log4cpp_lib && $auto_detect)  {
  print "\n*** Warning *** You didn't specify the log4cpp library path \n";
  print "Auto-detecting...\n";
  $matched = auto_detect("liblog4cpp.*");
  if( $matched=~m/(\S*)\/liblog4cpp\S*/i ) {
     $gopt_with_log4cpp_lib = $1;
  }
  print "Setting --with-log4cpp-lib=$gopt_with_log4cpp_lib\n";
}
if(! -d $gopt_with_log4cpp_lib) {
  print "*** Error *** You need to specify the log4cpp library path using --with-log4cpp-lib=/some/path/\n\n";
  exit 1;
}

# Save config options
#
print MKCONF "GOPT_ENABLE_PROFILER=$gopt_enable_profiler\n";
print MKCONF "GOPT_ENABLE_DOXYGEN_DOC=$gopt_enable_doxygen_doc\n"; 
print MKCONF "GOPT_ENABLE_DYLIBVERSION=$gopt_enable_dylibversion\n";
print MKCONF "GOPT_ENABLE_LOW_LEVEL_MESG=$gopt_enable_lowlevel_mesg\n";
#print MKCONF "GOPT_ENABLE_NEUGEN=$gopt_enable_neugen\n";
print MKCONF "GOPT_ENABLE_NEUT_CASCADE=$gopt_enable_neut_cascade\n";
print MKCONF "GOPT_ENABLE_GIBUU=$gopt_enable_gibuu\n";
print MKCONF "GOPT_ENABLE_LHAPDF=$gopt_enable_lhapdf\n";
print MKCONF "GOPT_ENABLE_CERNLIB=$gopt_enable_cernlib\n";
print MKCONF "GOPT_ENABLE_FLUX_DRIVERS=$gopt_enable_flux_drivers\n";
print MKCONF "GOPT_ENABLE_GEOM_DRIVERS=$gopt_enable_geom_drivers\n";
print MKCONF "GOPT_ENABLE_MUELOSS=$gopt_enable_mueloss\n";
print MKCONF "GOPT_ENABLE_VALIDATION_TOOLS=$gopt_enable_validation_tools\n"; 
print MKCONF "GOPT_ENABLE_TEST=$gopt_enable_test\n";
print MKCONF "GOPT_ENABLE_VIEWER=$gopt_enable_viewer\n";
print MKCONF "GOPT_ENABLE_MINOS=$gopt_enable_minos\n";
print MKCONF "GOPT_ENABLE_T2K=$gopt_enable_t2k\n";
print MKCONF "GOPT_WITH_CXX_DEBUG_FLAG=$gopt_with_cxx_debug_flag\n";
print MKCONF "GOPT_WITH_CXX_OPTIMIZ_FLAG=-$gopt_with_cxx_optimiz_flag\n";
print MKCONF "GOPT_WITH_PROFILER_LIB=$gopt_with_profiler_lib\n";
print MKCONF "GOPT_WITH_DOXYGEN_PATH=$gopt_with_doxygen_path\n";
#print MKCONF "GOPT_WITH_NEUGEN_PATH=$gopt_with_neugen_path\n";
print MKCONF "GOPT_WITH_NEUT_LIB=$gopt_with_neut_lib\n";
print MKCONF "GOPT_WITH_GIBUU_LIB=$gopt_with_gibuu_lib\n";
print MKCONF "GOPT_WITH_PYTHIA6_LIB=$gopt_with_pythia6_lib\n";
print MKCONF "GOPT_WITH_CERN_LIB=$gopt_with_cern_lib\n";
print MKCONF "GOPT_WITH_LHAPDF_LIB=$gopt_with_lhapdf_lib\n";
print MKCONF "GOPT_WITH_LHAPDF_INC=$gopt_with_lhapdf_inc\n";
print MKCONF "GOPT_WITH_LIBXML2_INC=$gopt_with_libxml2_inc\n";
print MKCONF "GOPT_WITH_LIBXML2_LIB=$gopt_with_libxml2_lib\n";
print MKCONF "GOPT_WITH_LOG4CPP_INC=$gopt_with_log4cpp_inc\n";
print MKCONF "GOPT_WITH_LOG4CPP_LIB=$gopt_with_log4cpp_lib\n";

close(MKCONF);

print "\nYour input configuration options were: @ARGV";
if($#ARGV < 0) { print "(none)" };
print "\n\n";

if(-e $MKCONF_FILE) {
  print "The $GENIE/src/make/Make.config file has been succesfully generated! \n";
  print "The following config options were set: \n";

  open(MKCONFR, "<$MKCONF_FILE") or die("Can not read back the $GENIE/src/make/Make.config!");
  @make_conf=<MKCONFR>;
  close(MKCONFR);
# print "@make_conf\n" unless ;
  foreach $setting (@make_conf) { 
    chomp($setting); 
    if ($setting=~m/\=/) {print "  $setting\n";} 
  }

  print "\n";
  print "*** To continue building GENIE type: gmake ";
  # warning for SRT users  
  if(defined $ENV{'SRT_ENV_SET'}) {
    print "(Don't forget to 'srt_setup --unsetup' first)";
  }
  print "\n\n";

  exit 0;
}

sub auto_detect {

  @search_dir  = ("/usr","/opt","/lib");           # std search paths
  push (@search_dir,"$ENV{'HOME'}") 
                 if defined $ENV{'HOME'};          # add $HOME
  push (@search_dir,"/WorkApp");                   # add where I add ext apps in my MacBookPro
  push (@search_dir,"$ENV{'RSD_TOP_DIR'}") 
                 if defined $ENV{'RSD_TOP_DIR'};   # add where RSD puts ext supporting libs

  $search_file = shift; 
  $optional_fullpath_substr = shift; # extra help in locating the correct file

  $optional_fullpath_substr = "" unless defined $optional_fullpath_substr;

  foreach(@search_dir) {
     $curr_dir = $_;
     if(! -d $curr_dir) {next;}
     @matches = `find $curr_dir -type f -maxdepth 7 -name $search_file 2> /dev/null | grep \"$optional_fullpath_substr\"`;
     $nmatches = @matches;
     if( $nmatches > 0) { return $matches[0]; }
  }
  return "";
}
