#!/usr/bin/perl
# Wrapper script for running the NRG code
# First argument is the filename of the executable (relative path).
# A file "FILES" is a list (one per line) of files that are compared
# against target files with the same name and suffix ".test" (see script
# "compare" in this directory).
# Part of "NRG Ljubljana"
# Rok Zitko, rok.zitko@ijs.si, Dec 2006

use warnings;
use strict;

if (@ARGV != 1) {
    die ("Synthax: calc <executable>\n");
}

open(FILES, "<FILES") or die "Can't open FILES\n";

# Delete old results
while (<FILES>) {
    chomp;
    my $f = $_;
    system("rm -f $f");
}

my $path = "../../src/";
my $prg = $ARGV[0];
my $exe = "$path$prg";

if( ! -e $exe) {
    die ("OOPS: Can't find the executable $exe.\n");
}

# Remove data file from previous runs (redundant if included in FILES)
unlink "data";

my $return;

$return = system("$exe -d -s ../../sneg.m -i ../../initial.m >log.d");

# Check for errors
if ($return != 0) {
    die "Generation of data file failed: $return\n";
}
if (!-e "data") {
    die "Data file not found.\n";
}

# Remove chi file from previous runs (redundant if included in FILES)
unlink "chi";

$return = system("$exe -w . >log.nrg");

if ($return != 0) {
    die "NRG iteration failed: $return\n";
}
if (!-e "chi") {
    die "Output file chi not found. Did NRG iteration fail?";
}
