#!/usr/bin/perl
#
#  ut_hxdgsoxbpi
#

use strict;
use ut_xrtobj;

# main
{

    # Object Istances
    my $ut = ut_xrtobj->new(
                             name    => "hxdgsoxbpi",
                             utname  => "ut_hxdgsoxbpi",
                             version => "1.0",
    );

    # Set Parameter
    #$ut->setParam("validate", 0);
    my $input   = $ut->getParam( "input_dir" );
    my $output  = $ut->getParam( "output_dir" ) . "/" . $ut->getParam( "utname" );
    $ut->setParam( "output_dir", $output );
    my $chat    = $ut->getParam( "chatter" );
    my $clobber = $ut->getParam( "clobber" );

    # I/O Directory Check
    $ut->checkIODirectory( ) if $ut->isValid;

    # Environment check
    my @chk_env = ( "LHEASOFT", "HEADAS", "CALDB" );

    $ut->checkEnvironment( @chk_env ) if $ut->isValid;

    my $ArchDir = $ut->getParam( "utname" );

    # Input Files Check
    my @chk_inp = (
                    "$input/$ArchDir/testin_ae101005010hxd_0_gsono_cl.evt.gz",
                    "$input/$ArchDir/testin_ae101005010hxd_0_pse_cl.evt.gz",
                    "$input/$ArchDir/testin_ae101005010_hxd_gsobgd.evt.gz",
    );

    $ut->checkExist( "Input", @chk_inp ) if $ut->isValid;

    # Prototype Check
    my @chk_proto = (
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_nxb.pi.gz",
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_sr.pi.gz",
                      "$input/$ArchDir/testchk_ae101005010hxd_gso_sr_grp.pi.gz"
    );

    $ut->checkExist( "Prototype", @chk_proto ) if $ut->isValid;

    # Executables Files Check
    my @chk_exe = ( "hxdgsoxbpi", "ftdiff", "fverify" );
    $ut->checkExecutable( @chk_exe ) if $ut->isValid;

    # expected output files
    my @outfiles = (
                     "$output/testout_ae101005010hxd_gso_nxb.pi",
                     "$output/testout_ae101005010hxd_gso_sr.pi",
                     "$output/testout_ae101005010hxd_gso_sr_grp.pi"
                   );
    unlink @outfiles;

    # run the test
    my %par_task = (
                     input_fname     => "$chk_inp[ 0 ]",
                     pse_event_fname => "$chk_inp[ 1 ]",
                     bkg_event_fname => "$chk_inp[ 2 ]",
                     gti_fname       => 'NONE',
                     outstem         => "$output/testout_ae101005010",
                     nxb_scale       => 1.0,
                     groupspec       => 'yes',
                     groupfile       => "DEFAULT",
                     cleanup         => 'yes',
                     clobber         => 'yes',
                     chatter         => $chat,
                     history         => 'yes'
    );

    print "task start\n";
    $ut->runTask( %par_task ) if $ut->isValid;
    if ( $ut->isValid ) {

        # fverify
        foreach my $outfile ( @outfiles ) {
            $ut->fverify( "$outfile" );
        }

        # ftdiff
        for ( my $i = 0; $i < @outfiles; $i++ ) {
            ftdiff( $ut, "$outfiles[ $i ]", "$chk_proto[ $i ]" );
        }
    }

    $ut->delete( );
}

sub ftdiff
{
	my ($this, $file, $template) = @_;

	my $search = "^\\*\\*\\*\\* End of file comparison:  0 differences were found\$";
	my $command = "ftdiff " . $file . " " . $template .
	    " exclude = \"DATE, USER, CREATOR, DATASUM, FILENAME, FKRSP001\"" .
		" tolerance=0.035";
	my $stem = $this->stem();
	$this->printk (0,
		       "$stem: running ftdiff between $file $template\n");
	$this->printk (4, "$stem: running $command\n");
	
	my $result = $this->execCmd($command, $search);

#	$search =~ s/0/\[0-9\]\+/;
	if ($result =~ $search)
	{
	    $this->success("No difference found between $file and prototype\n");
	}
	else
	{
	    $this->printk(4,"Error: = <$result>\n");
	    $this->error(1024, "difference found from prototype\n");
	}   

}
