=head1 NAME

  CFX5::Simulation::Configuration::Remeshing::Remesh::MeshUpdate - pre session file
=head1 SYNOPSIS

=head1 DESCRIPTION

$Id$

=cut

package CFX5::Simulation::Configuration::Remeshing::Remesh::MeshUpdate ;
@ISA=qw(CFX5::Output::File);

use strict;

use CFX5::Output::File ; 
use CFX5::Output qw(:all);
use CFX5::PathManip qw(basename split_mult_files join_file);
use CFX5::Simulation::Configuration::Remeshing::Remesh ;
use CFX5::Commands qw(do_cmd parse_rv);
use CFX5::Simulation::Util qw(GetDataSet);
use CFX5::Config qw(%Config) ;

#
################################################################################
# Call to CFX Pre to execute the update
################################################################################
#
sub DoUpdate() {
  my $self = shift ;
  my $outputFile = shift; 

  unless (defined $self->{filename}) {
    _error("DoUpdate: Mesh update definition is incomplete - can't do update");
    return 0; 
  }
  unless (-e $self->{filename}) {
    _error("DoUpdate: Update not possibe - missing file: $self->{filename}");
    return 0; 
  }
  
  my $cmdLine = "\"$Config{CFX_CMD_DIR}/cfx5pre\" -batch \"$self->{filename}\"" ; 
  
  my @cmdArgs = CFX5::Commands::split_cmd($cmdLine); 
  my $cmd = shift(@cmdArgs); 
  
  my $rv = CFX5::Simulation::Configuration::Remeshing::run_and_redirect_Remeshing(
             $outputFile,
             $cmd,@cmdArgs);
  unless ($rv->is_success()) {
    _error($rv->errstr("DoUpdate: Failed during call to CFX Pre"));
    return 0; 
  }
   
  1 ; 
}
#
################################################################################
# Session File Creation
################################################################################
#
#===============================================================================
# Write header information to session file
#===============================================================================
#
sub WriteHeader() {
  my $self = shift ;
  my $inpFile = shift ;  

  if (my $fh=$self->open()) {
  
    print $fh "COMMAND FILE:\n"; 
    print $fh "  CFX Pre Version = ".$Config{CFXBUILDID_MAJOR_MINOR}."\n"; 
    print $fh "END\n"; 
  
    print $fh ">load filename=$inpFile, mode=def\n";
    print $fh ">update\n";
    $self->close();
  } else {
    _error("WriteHeader: Could not write header to $self->{filename}");
    return 0; 
  }
  1;  
}  
  
#
#===============================================================================
# Write body information for given remesh object
#===============================================================================
#
sub WriteRemeshContrib() {
  my $self      = shift ;
  my $remeshObj = shift ;  
  
  # Get 
  # --> the location (i.e. assemblies, etc...) that are being remeshed
  my $location = $remeshObj->Get("Location") ; 
  my $meshFile = $remeshObj->Get("MeshFile") ;
  my $option   = $remeshObj->Get("Option") ;  
  my $meshReloadOptions = $remeshObj->Get("MeshReloadOptions") ;  

  # write entry to the mesh update file
  if (my $fh=$self->open()) {

    my $reloadCmd = ">reload option=location, location=$location, replacement=$meshFile, preservenames=true" ;
    $reloadCmd   .= ", replacetype=Generic" if $option eq "ICEM CFD Replay" ;
    $reloadCmd   .= ", $meshReloadOptions" if defined $meshReloadOptions ;
    print $fh "$reloadCmd\n"; 
    print $fh ">update\n";
    $self->close();
    
  # throw an error if things didn't work out 
  } else {
    _error("WriteRemeshContrib: Could not write mesh update body to $self->{filename}");
    return 0 ; 
  }
  1;  
}  
  
#
#===============================================================================
# Write footer information to session file
#===============================================================================
#
sub WriteFooter() {
  my $self = shift ;
  my $outFile = shift ;  

  if (my $fh=$self->open()) {
    print $fh ">writeCaseFile filename=$outFile, operation=write solver file\n" ;
    print $fh ">update\n";
    $self->close();
  } else {
    _error("WriteFooter: Could not write footer to $self->{filename}");
  }
  1;  
}  

#
################################################################################
## Support Routines
################################################################################
#
#===============================================================================
# GetFileDataCCLAPI
#===============================================================================
#
sub GetFileDataCCLAPI() {
  my $fName = shift; 

#
# Ensure file exists
#
  unless (-e $fName) {
    _error("ReplaceMeshes: Specified file does not exist: $fName") ; 
    return undef ;
  }
  
#
# Extract the CCL dataset
#
  my @CCL = GetDataSet($fName,"G/FILEDATA","EVERY","0"); 
#  my @CCL = GetDataSet($fName,"STATE","CFX-Pre","0"); 
  if (!defined $CCL[0]) {
    return undef ;
  }

#
# Create new CCLAPI and assign CCL to it
#
  my $cclapi = CFX5::CCLI::new_api_object();
  my $rv=$cclapi->parseCCLData(\@CCL, '');
  if (defined $rv) {
    _error("ReplaceMeshes: Error reading Command Language data from $fName"); 
    return undef ;
  }
  
  return $cclapi ;
}

1 ;
