#! /usr/bin/perl -w

# Takes a snapshot of the GENIE build environment and saves it build-env.log file
# C.V.Andreopoulos

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

$SRT_ENV_SET       = $ENV{'SRT_ENV_SET'};
$ROOTSYS           = $ENV{'ROOTSYS'};
$PATH              = $ENV{'PATH'};
$LD_LIBRARY_PATH   = $ENV{'LD_LIBRARY_PATH'};
$DYLD_LIBRARY_PATH = $ENV{'DYLD_LIBRARY_PATH'};

# suppress PERL warnings when using undefined variables
$SRT_ENV_SET                 = "**undefined**" unless defined $SRT_ENV_SET;
$ROOTSYS                     = "**undefined**" unless defined $ROOTSYS;
$PATH                        = "**undefined**" unless defined $PATH;
$GENIE                       = "**undefined**" unless defined $GENIE;
$LD_LIBRARY_PATH             = "**undefined**" unless defined $LD_LIBRARY_PATH;
$DYLD_LIBRARY_PATH           = "**undefined**" unless defined $DYLD_LIBRARY_PATH;

# create log
$BLD_ENV_LOG = "$GENIE/build-env.log";
open(LOG, ">$BLD_ENV_LOG") 
    or die("Can not open build-env.log file to save your build environment");

print LOG "[The GENIE spy reports]\n\n";

print LOG "\$GENIE                       -> $GENIE\n";
print LOG "\$SRT_ENV_SET                 -> $SRT_ENV_SET\n";
print LOG "\$LD_LIBRARY_PATH             -> $LD_LIBRARY_PATH\n";
print LOG "\$DYLD_LIBRARY_PATH           -> $DYLD_LIBRARY_PATH\n";
print LOG "\$PATH                        -> $PATH\n\n";

# Read the GENIE config status
$GCONF_FILE = "$GENIE/src/make/Make.config";
if(-e $GCONF_FILE) {
  open(GCONF, "<$GCONF_FILE") or die("Can not open $GENIE/src/make/Make.config!");
  @genie_conf=<GCONF>;
  close(GCONF);
}
print LOG "Saving the configuration of your GENIE installation: \n\n";
print LOG "@genie_conf \n\n";

# Read the ROOT config status
$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]);
}
print LOG "Saving the configuration of your ROOT installation: \n\n";
print LOG "$root_conf_status \n\n";

print LOG "Saving info on your OS, activity, disk usage & quota limit: \n\n";

close(LOG);

system("uname -a >> $BLD_ENV_LOG");
system("echo     >> $BLD_ENV_LOG");
#system("ps aux   >> $BLD_ENV_LOG");
#system("echo     >> $BLD_ENV_LOG");
system("df       >> $BLD_ENV_LOG");
system("echo     >> $BLD_ENV_LOG");
system("quota -v >> $BLD_ENV_LOG");



