#!/usr/bin/perl -w

use strict;
use Time::gmtime;
use Time::localtime;

sub datestring;

my $project = "tmqcd_nf2p1p1";
my $ensemble = "iwasaki_b1.90_L20T48_k0.16327_mu0.004_musigma0.15_mudelta0.19";
my $series = "0";
# Who generated these configurations?
my $genname = "Siebren Reker";
my $geninst = "RUG";
# Who adds them to the GRID (who are you)?
my $addname = "Siebren Reker";
my $addinst = "RUG";
my $algorithm = "mtHPHMC";
# Compilation date of the code.
my $algdate = "2008-01-01T00:00:00+00:00";
# Machine where the configurations was generated.
my $machinetype = "Blue Gene/L";
my $machinename = "Stella";
my $machineinst = "RUG/CIT";

#Prepare the config XML files
foreach my $config (<conf.[0-9]???>) {
  #This is why we need the xlf-info record.
  my $update = `lime_extract_type $config xlf-info | grep "trajectory nr"| sed -e 's/ trajectory nr = //g'| xargs printf %06d`;
  my $plaq = `lime_extract_type $config xlf-info | grep "plaquette"| sed -e 's/plaquette = //g'`;
  my $lctime = `lime_extract_type $config xlf-info | grep "time"| sed -e 's/ time = //g'`;
  my $gendate = datestring($lctime);
  my $adddate = datestring(time);
  my $algversion = `lime_extract_type $config xlf-info | grep "hmcversion"| sed -e 's/ hmcversion = //g'`;

  #The logical file name
  my $lfn = `lime_extract_type $config ildg-data-lfn`;
  #Is the ildg record not already there?
  if ($lfn eq "") {
    #if not, add the record.
    $lfn = "lfn://ldg/etmc/$project/$ensemble/$config";
    my $error_status = system "addlfn $config $lfn";
    if ($error_status != 0) {die "addlfn failed!\n";}
  }

  #Produce the ILDG-checksum
  my $checksum = `ildg_cksum $config`;
  my $destination = "$config.xml";
  my $configtemplate = "config_template.xml";

  # get rid of newlines.
  chomp $plaq;
  chomp $update;
  chomp $algversion;
  chomp $checksum;

  #Read the template
  open(FILE, '<', $configtemplate) or die "Can't read source file: $!\n";
  my @data = <FILE>;
  close FILE;

  #replace in the whole XML file...
  map {
    s/_ALGORITHM_/$algorithm/g;
    s/_ALGVERSION_/$algversion/g;
    s/_ALGDATE_/$algdate/g;
    s/_GENNAME_/$genname/g;
    s/_GENINST_/$geninst/g;
    s/_GENDATE_/$gendate/g;
    s/_ADDNAME_/$addname/g;
    s/_ADDINST_/$addinst/g;
    s/_ADDDATE_/$adddate/g;
    s/_MACHINEINST_/$machineinst/g;
    s/_MACHINENAME_/$machinename/g;
    s/_MACHINETYPE_/$machinetype/g;
    s/_PROJECT_/$project/g;
    s/_ENSEMBLE_/$ensemble/g; 
    s/_SERIES_/$series/g;
    s/_CONFIG_/$config/g;
    s/_UPDATE_/$update/g;
    s/_PLAQUETTE_/$plaq/g;
    s/_CHECKSUM_/$checksum/g;
  } @data;


  #write to config XML file
  open(OUT, '>', $destination) or die "Can't write to file: $!\n";
  print OUT @data;
  close OUT;

  my $error_status = system "lput $destination";
  if ($error_status != 0) {
    open(OUT, '>>', "notdone.log");
    print OUT "$destination \n";
    close OUT;
  }
}

sub datestring
{
  my ($time) = @_;

  my $gmtime = gmtime($time);
  my $lctime = localtime($time);

  my $str = sprintf "%04d-%02d-%02dT%02d:%02d:%02d%+03d:00",
            ($gmtime->year() + 1900), ($gmtime->mon() + 1), $gmtime->mday(),
            $gmtime->hour(), $gmtime->min(), $gmtime->sec(),
            ($gmtime->hour() - $gmtime->hour());

  return $str;
}
