#! /usr/bin/perl -w
#
# Creates the $GENIE/src/Conventions/GBuild.h header file
#
# C.Andreopoulos <costas.andreopoulos \at stfc.ac.uk>, Rutherford Lab.
#

$GENIE = $ENV{'GENIE'};
die ("Not even the GENIE environmental variable is defined!") unless defined $GENIE;

# GENIE config file
#
$GCONF_FILE = "$GENIE/src/make/Make.config";
die("Can not open $GENIE/src/make/Make.config!") unless (-e $GCONF_FILE);

# Read the ROOT config 
#
$ROOTSYS = $ENV{'ROOTSYS'};
$RCONF_FILE = "$ROOTSYS/config.status";
$root_conf_status="";
if(-e $RCONF_FILE) {
  open(RCONF, "<$RCONF_FILE") or die("Can not open $ROOTSYS/config.status!");
  @root_conf=<RCONF>;
  close(RCONF);
  chomp($root_conf_status = $root_conf[0]);
}

# Open header file
#
$GBLD_FILE = "$GENIE/src/Conventions/GBuild.h";
open(GBLD, ">$GBLD_FILE") or die("Can not write out the GBuild.h file!");

# Write header file
#
print GBLD "#ifndef _GBUILD_H_ \n";
print GBLD "#define _GBUILD_H_ \n";
print GBLD "/* \n";
print GBLD " * Build information automatically generated by the GENIE installer \n";
print GBLD "*/ \n\n";

# neut-cascade enabled?
#
@nret = `grep 'GOPT_ENABLE_NEUT_CASCADE=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_NEUT_CASCADE_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_NEUT_CASCADE_ENABLED__\n"; }

# gibuu enabled?
#
@nret = `grep 'GOPT_ENABLE_GIBUU=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_GIBUU_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_GIBUU_ENABLED__\n"; }

# fluka enabled?
#
@nret = `grep 'GOPT_ENABLE_FLUKA=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_FLUKA_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_FLUKA_ENABLED__\n"; }

# pythia6 enabled?
#
if( $root_conf_status=~m/enable-pythia6/i ) 
      { print GBLD   "#define __GENIE_PYTHIA6_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_PYTHIA6_ENABLED__\n"; }

# mysql enabled?
#
if( $root_conf_status=~m/enable-mysql/i ) 
      { print GBLD   "#define __GENIE_MYSQL_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_MYSQL_ENABLED__\n"; }

# flux drivers enabled?
#
@nret = `grep 'GOPT_ENABLE_FLUX_DRIVERS=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_FLUX_DRIVERS_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_FLUX_DRIVERS_ENABLED__\n"; }

# geometry drivers enabled?
#
@nret = `grep 'GOPT_ENABLE_GEOM_DRIVERS=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_GEOM_DRIVERS_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_GEOM_DRIVERS_ENABLED__\n"; }

# low-level msg printout enabled?
#
@nret = `grep 'GOPT_ENABLE_LOW_LEVEL_MESG=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_LOW_LEVEL_MESG_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_LOW_LEVEL_MESG_ENABLED__\n"; }

# LHAPDF enabled?
#
@nret = `grep 'GOPT_ENABLE_LHAPDF=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_LHAPDF_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_LHAPDF_ENABLED__\n"; }

# CERNLIB enabled?
#
@nret = `grep 'GOPT_ENABLE_CERNLIB=YES' $GCONF_FILE`;
if(@nret>0) 
      { print GBLD   "#define __GENIE_CERNLIB_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_CERNLIB_ENABLED__\n"; }

# grab libxml2 version information
#
$libxml2_inc = "";
$ret1 = `grep GOPT_WITH_LIBXML2_INC $GCONF_FILE`;
if($ret1=~m/GOPT_WITH_LIBXML2_INC=(.+)/) {
	$libxml2_inc = $1;
}
open(VRS_FILE, "$libxml2_inc/libxml/xmlversion.h") or warn("$libxml2_inc/libxml/xmlversion.h!");
$vrs_set = 0;
foreach(<VRS_FILE>) {
	chomp $_;
#	print "$_\n";
	if(m/\Wdefine\s+LIBXML_VERSION\s+(\d+)/i) {
		$libxml2_vrs = $1;
#		print "GENIE seems to be using libxml2 version $libxml2_vrs\n";
                print GBLD   "#define __GENIE_USES_LIBXML2_VERSION__ $libxml2_vrs \n"; 
                $vrs_set = 1;
	}
}
if($vrs_set==0) {
        print GBLD   "//#define __GENIE_USES_LIBXML2_VERSION__ <unknown> \n"; 
}
close VRS_FILE;

# grab log4cpp version information
#
$libxml2_inc = "";
$ret1 = `grep GOPT_WITH_LOG4CPP_INC $GCONF_FILE`;
if($ret1=~m/GOPT_WITH_LOG4CPP_INC=(.+)/) {
	$log4cpp_inc = $1;
}

open(VRS_FILE, "$log4cpp_inc/log4cpp/config.h") or warn("$log4cpp_inc/log4cpp/config.h!");
$vrs_set = 0;
foreach(<VRS_FILE>) {
	chomp $_;
#	print "$_\n";
	if(m/\Wdefine\s+LOG4CPP_VERSION\s+.(\d+).(\d+)./i) {
	        $log4cpp_vrs = 0;
		if($1 >0 ) { $log4cpp_vrs = $1 * 100 + $2; }
#		print "GENIE seems to be using log4cpp version $log4cpp_vrs \n";
                print GBLD   "#define __GENIE_USES_LOG4CPP_VERSION__ $log4cpp_vrs \n"; 
                $vrs_set = 1;
	}
}
if($vrs_set==0) {
        print GBLD   "//#define __GENIE_USES_LOG4CPP_VERSION__ <unknown> \n"; 
}
close VRS_FILE;

print GBLD "\n#endif\n\n";

# Close header file
#
close(GBLD);

