#!/usr/bin/perl
#
#	ut-uvotimggen
#		unit test for image generation simulator             	
#
#	options
#		input=<directory>
#			set the input directory
#		output=<directory>
#			set the output directory
#		verbose
#			more logging
#		quiet
#			less logging
#
#

use strict;

use FileHandle;

use constant PREFIX => 'ut-uvotimggen: ';


sub report
{
	print(PREFIX, @_);
}


sub fatal
{
	&report;
	die(PREFIX . "aborted\n");
}


sub doCommand
{
	my ($command, %args) = @_;

	my ($first) = split(/\s+/, $command);

	if ($args{verbose}) {
		report("doCommand $command\n");
	}
	elsif (not $args{quiet}) {
		report("running $first\n");
	}

	my $result = { };

	if ($args{pipeFrom}) {
		$result->{handle} = FileHandle->new("$command|");
	}
	else {
		$result->{output} = qx($command);
		$result->{lines} = [ split(/\n/, $result->{output}) ];
	}

	if ($?) {
		my $s = $args{verbose} ? $first : $command;
		fatal("error running $s [$?]: $!\n");
	}

	return $result;
}


{
	my $code = 0;

	my %options = (
		input => 'input',
		output => 'output',
	);

	foreach my $arg (@ARGV) {
		if ($arg eq 'quiet') {
			$options{quiet} = 1;
		}
		elsif ($arg eq 'verbose') {
			$options{verbose} = 1;
		}
		elsif ($arg =~ /^input=(.+)/) {
			$options{input} = $1;
		}
		elsif ($arg =~ /^output=(.+)/) {
			$options{output} = $1;
		}
		else {
			fatal("invalid option: $arg\n");
		}
	}

	report("validating set up\n") if not $options{quiet};

	# validate environment
	foreach my $var (qw(HEADAS FTOOLS)) {
		if (not defined($ENV{$var})) {
			fatal("environment variable $var not set\n");
		}
		elsif (not -d $ENV{$var}) {
			fatal("invalid $var directory $ENV{$var}\n");
		}
	}

	# validate input/output directories
	foreach my $key (qw(input output)) {
		if (not -d $options{$key}) {
			fatal("invalid $key directory: $options{$key}\n");
		}
	}

        my $headas = $ENV{HEADAS};
	my $input = $options{input};
	my $output = $options{output};

	my %task = (
		infile        => "$input/input.fit",
		extlist       => "$input/ImgGen.list",
		outfile       => "$output/output.fit",
		starttime     => "default",
		clobber       => 'yes',
		history       => 'yes',
		chatter       => 3,
	);

	# validate input files
	foreach my $key (qw(infile)) {
		my $input = $task{$key};
		if (not defined($input)) {
			fatal("missing input key: $key\n");
		}
		elsif (not -f $input) {
			fatal("invalid $key file: $input\n");
		}
	}

	# build command line
	my $command = 'uvotimggen';

	foreach my $p (qw(infile extlist outfile starttime 
			clobber history chatter )) {
		if (not defined($task{$p})) {
			fatal("missing parameter $p\n");
		}
		else {
			$command .= " '$p=$task{$p}'";
		}
	}

	doCommand($command, %options);

	$command = qq(fverify infile=$output/output.fit outfile=STDOUT);
	doCommand($command, %options);
 
        my $exclude = "HISTORY,DATE,FILENAME,CREATOR";
	$command = "ftdiff " 
          . "$input/output.fit $output/output.fit " 
          . "tolerance=0.0001 "
          . "hdumaxdiff=20 "
          . "exclude=$exclude ";
	doCommand($command, %options);
	my $result = doCommand($command, %options, pipeFrom => 1);
	my $fh = $result->{handle};

	while (<$fh>) {
		if (/^[<>]/) {
			# diff contents
			report($_);
                        $code = 1 
		}
		elsif (/^---/) {
			# skip mark
		}
		else {
			report("diff?!: $_");
		}
	}
	close($fh);
	
        unlink("$output/output.fit");

        if ($code == 0) {
           report("  Test passed! \n");
        }
        else{
           report("  Test failed! \n");
        }	
        exit($code);
}

