##########################################################################
#                                                                        # #
# Rational Test RealTime                                                 #   #
#                                                                        #######
#                        -*-  Unit Testing node -*-                            #
#                                                                              #
#                                                                              #
# Required global variables:                                                   #
#   @OBJECTS     : object files                                                #
#   $LDFLAGS     : link flags                                                  #
#   $LIBRARIES   : libraries list for linking                                  #
#   $TESTPROGRAM : executable name                                             #
#   $ARGUMENTS   : executable arguments                                        #
#   $EXECMODE    : execution mode ("NORMAL" or "DEBUG")                        #
#   $AMLFILE     : generated AML report                                        #
#   @OTSFILES    : list of OTS files                                           #
#   $BINDIR      : ATTOL binaries directory                                    #
#   $SCRIPTDIR   : Studio scripts directory                                    #
#   $TARGETDIR   : target package directory                                    #
#   $OUTDIR      : output directory                                            #
#   $LOGFILENAME : generated files log                                         #
################################################################################


# To do output in chronological order when stderr and stdout are both used
use FileHandle;
use File::Copy ;

STDOUT->autoflush(1);

# Configuration file
require $ARGV[0];

# Miscellaneous functions 
require $SCRIPTDIR . "Fcts.pl";
require $SCRIPTDIR . "Tools.pl";
require $SCRIPTDIR . "Split.pl";

# Target package related functions
require $SCRIPTDIR . "Products.pl";

# Target-dependant functions
require $TARGETDIR . $PATH_SEPARATOR."cmd".$PATH_SEPARATOR."atl_link.pl";
require $TARGETDIR . $PATH_SEPARATOR."cmd".$PATH_SEPARATOR."atl_exec.pl";

&CheckIntDir;
&EnterInScript;
ClearLogFile($LOGFILENAME);

################################################################################
# Attol Unit Testing postprocessor execution
# Arguments:
#    $atu_out    : traces file
#    $atu_xrd    : generated AML report
# Return value: attolpostpro return value
# Required global variables:
#    $BINDIR
# Used global variables:
################################################################################
sub atu_postpro ($$$)
{
  my ($atu_out,$atu_xrd,$atu_cio) = @_;

  # Suppress old features
  delete $ENV{'ATTOLLAN'} ;
  delete $ENV{'ATTOLCINC'} ;
  delete $ENV{'ATTOLTMPK'} ;
  delete $ENV{'ATTOLCPPINC'} ;
  delete $ENV{'ATW_NO_USR_INCLUDE'} ;

  # Let's environment
  $ENV{'ATTOLHOME'} = $ATTOLHOME;
  $ENV{'ATUDIR'} = $ATTOLHOME . $PATH_SEPARATOR."lib";
  $ENV{'ATUTGT'} = "$TARGETDIR";

  # Defaults options
  my $atu_postpro_name = "attolpostpro";
  my $atu_options = " ";
  my $ada_mode = 0;

  if (($TDP_LANGUAGE eq "ADA") || ($TDP_LANGUAGE eq "ADA83") || ($TDP_LANGUAGE eq "ADA83_V34"))
  {
    $ada_mode = 1;
  }
  if ($LOGFILENAME)
  {
    $atu_options .= " -studio_log=\"$LOGFILENAME\"";
  }

  # Update options

  if (($TDP_LANGUAGE eq "C") || ($TDP_LANGUAGE eq "CPP") || ($TDP_LANGUAGE eq "ADA83_V34"))
  {
     $atu_postpro_name = "attolpostpro";
  }
  if (($TDP_LANGUAGE eq "ADA") || ($TDP_LANGUAGE eq "ADA83"))
  {
     $atu_postpro_name = "attolpostproada";
  }

  if ($ATCMODE)
  {
    $atu_options .= " -cio=\"$atu_cio\"";
  }

  my ($dir,$name,$ext) = SplitFileName( "$atu_out.ri2" );
  my $ri2 = "$name.$ext";
  if ($ATU_COMPARE)
  {
    if (-f $ri2)
    {
       copy ($ri2, "$atu_out.ri2");
       $atu_options .= " -compare=strict";
    }
    else
    {
       print "Error: Unable to diff with file $ri2.\n";
       print "       $ri2 not found,\n";
       print "       Please re-build without having the 'Diff of last two test runs' option turned on.\n";
       return(1);
    }
  }
  else
  {
    copy("$atu_out.rio", "$ri2");
  }

  if ($ATU_FILTER == 1)
  {
      $atu_options .= " -RA=ERR";
  }
  if ($ATU_FILTER == 2)
  {
      $atu_options .= " -RA=TEST";
  }
  if ($ATU_STRATEGY == 1)
  {
      $atu_options .= " -VA=NOEVAL";
  }
  if ($ATU_STRATEGY == 2)
  {
      $atu_options .= " -VA=COMBINED";
  }

  # Postpro execution
  unlink ( "$atu_out.rod" );

  my $atu_command = $BINDIR . $PATH_SEPARATOR . $atu_postpro_name . " \"$atu_out\" \"$atu_out\" $atu_options" ." ".$ATU_REPORT_OPTIONS;
  my $postpro_status = SystemP($atu_command);

  if ( ! -f "$atu_out.rod" )
    {
      print "No result generated.\n";
      CleanTemporaryTuFiles($INTDIR);    
      return(1);
    }
  # TPM initialization
  $ENV{'ATTOL_TPM_USER'}=$USERNAME;
  $ENV{'ATTOL_TPM_ROOT'}=$ATTOL_TPM_ROOT;

  # Graphic Generation
  my $rtx_opt = "";  
  if ($MATLAB || $ENV{'ATURTX'})
  {
     $rtx_opt = " -g ";  
  }  

  # Translator execution
  unlink ( "$atu_xrd" );

  my $atu_command = $BINDIR . $PATH_SEPARATOR .  "rod2xrd" . $rtx_opt . " \"-l$LOGFILENAME\"" . " \"-o$atu_xrd\" \"$atu_out.rod\"" ;
  my $trans_status = SystemP ($atu_command);
  if ($trans_status != 0 ) { print "Xrd Translation Failed.\n"; exit(1); }

  # Matlab Translation
  if ($MATLAB)
  {
     # MAT Generation
     # use '-v' to set MATLAB Result Viewer
     my $atu_command = $BINDIR . $PATH_SEPARATOR .  "rod2mat -v " . " \"$atu_out.rod\"" ;
     my $trans_status = SystemP ($atu_command);
  }

  # add temporary files into log 
  CleanTemporaryTuFiles($INTDIR);    
  return 0;
}

################################################################################
# Here is the beginning
################################################################################

my $status = 0;
my $ada_mode = 0;

  if (($TDP_LANGUAGE eq "ADA") || ($TDP_LANGUAGE eq "ADA83") || ($TDP_LANGUAGE eq "ADA83_V34"))
  {
     $ada_mode = 1;
  }  
  # Out Files
  my $atu_out = "" ;
  if ($INTDIR)
  {
     $atu_out .= $INTDIR.$PATH_SEPARATOR;
  }
  $atu_out .= $ATU_INTERMEDIATE_FILE;
 
#----------------------------------------------------------------------------
## Linkage
#----------------------------------------------------------------------------
if ($STAGE =~ /l/)
{
  if ($ada_mode == 0)
    {
      #----------------------------------------------------------------------------
      ## do not forget object file of target package.
      if (&MakeTargetPackageObject) {
	exit 1;
      }
      push( @OBJECTS, &GetTargetPackageObject() );
      
      print "Linking $TESTPROGRAM...\n";
      $status = atl_link ($TESTPROGRAM, \@OBJECTS, $LDFLAGS,
                          \@LIBRARY_DIR_LIST, $LIBRARIES);
      CleanTemporaryTuFiles($INTDIR);    
      if ($status != 0 ) { print "Failed.\n"; exit(1); }
    }
  else
    { 
      my $cwdir = getcwd();
      $cwdir=~s/\//$PATH_SEPARATOR/g;
      my ($exedir,$exename,$exeext) = SplitFileName( $TESTPROGRAM );
      my $targetexe = $exename.".".$exeext;
      my $save_ctx = $ATL_MAIN_PROCEDURE;


      $ATL_MAIN_PROCEDURE = lc($ATU_INTERMEDIATE_FILE);
      $ATL_MAIN_PROCEDURE = clean_name_from_exotic_char($ATL_MAIN_PROCEDURE);
      $status = atl_link ($targetexe, \@OBJECTS, $LDFLAGS,\@LIBRARY_DIR_LIST, $LIBRARIES);

      $ATL_MAIN_PROCEDURE = $save_ctx;      
      CleanTemporaryTuFiles($INTDIR);    
      if ($status != 0 ) { print "Failed.\n"; exit(1); }      
    }
}

#----------------------------------------------------------------------------
## Execution
#----------------------------------------------------------------------------
if ($STAGE =~ /e/ )
{
  $atu_rio = "$atu_out.rio";
  print "Executing $TESTPROGRAM $ARGUMENTS...\n";
  $status = AtlExec ($TESTPROGRAM, $atu_rio, $ARGUMENTS);
  if ($status != 0 )
  {
    print "Executable terminated with error code $status.\n";
  }

  if ( ! -f $atu_rio )
  {
    print "No trace $atu_rio generated.\n";
    exit(1);
  }

  $RIOFILE = $atu_rio . ".tmp";
  unlink($RIOFILE);
  SplitTraces ($atu_rio);
  if ( -f $RIOFILE)
  {
    rename ( $RIOFILE, $atu_rio );
  }

  if ( ! $used[$rio] )
  {
    print "No RIO instruction found.\n";
    exit(1);
  }
}

#----------------------------------------------------------------------------
## Postprocessing
#----------------------------------------------------------------------------
if ($STAGE =~ /r/ )
{
  my $atu_cio = "$INTDIR".$PATH_SEPARATOR."atu.cio";
  print "Postprocessing...\n";

  if ($ATCMODE) {
    if ( ! $used[$tio] )
    {
      print "No TIO instruction found.\n";
    }
    else
    {
      $status = atc_postpro ($ATC_TIO_NAME, $atu_cio);
      if ($status != 0 ) { print "Failed.\n"; exit(1); }
    }
  }

  $status = atu_postpro ($atu_out, $XRDFILE, $atu_cio);
  if ($status != 0 ) { print "Failed.\n"; exit(1); }
}

## Intermediate files remove
# none

print "Done.\n";

0;
#
################################################################################
