#!/usr/bin/perl
# Mathematica script wrapper with error checking
# Rok Zitko, rzitko@gwdg.de, Jan, Oct 2008
# $Id$

# d-level ("conduction band") parameters
my $U = `getparam U param.loop`;
my $delta = `cat param.delta`;
my $ed = $delta-$U/2;
chomp(my $scale = `getparam scale param.loop`);

use POSIX;
my $pid = getpid();
my $time = time();
my $fn = "unique-$pid-$time.m";
open(F,">$fn") or die "Can't open $fn for writing.";

# *******************************************************
print F <<ENDOFSCRIPT;

ed=$ed;
scale=$scale;
<<bandDOS.m;

ENDOFSCRIPT
# *******************************************************

-e "$fn" or die "Failed!?";

$ENV{'DISPLAY'} = '';

my $result = 'res/imaw.dat';
unlink $result;

while (!-e $result) {
    system("math -batchinput -batchoutput < $fn");
    if (!-e $result) {
	# Workaround for systems with floating licenses, where
	# Mathematica might not start up.
	print "bandDOS failed. Will retry in 10 seconds.\n";
	sleep(10);
    }
}

unlink $fn
  or warn "Can't remove temporary script file $fn.";
