=head1 NAME

  CFX5::Simulation::AAS - aaS executable control

=head1 SYNOPSIS

This object is used by L<CFX5::Simulation>, to manage the process
of starting the aaS (as a Server) executable

=head1 DESCRIPTION

$Id: AAS.pm 135502 2016-01-11 11:15:21Z cjstaple $

=cut

package CFX5::Simulation::AAS;

use strict;
use Carp;

use CFX5::Config qw(%Config setconf export_conf);
use CFX5::PathManip qw(join_file);
use CFX5::FileTests;
use CFX5::Commands;


sub start {
  # Export PID of cfx5solve script, so that aaS executable can monitor it
  my $envvar = 'ANSYS_CFX5SOLVE_PID';
  setconf($envvar, $$);
  export_conf($envvar);

  # Check aaS executable exists
  my $aas_exe = join_file($Config{CFX5EXECDIR}, 'CFXExceptionHolderServer.exe');
  return 0 unless (CFX5::FileTests::execfile($aas_exe,"CFX aaS executable: $aas_exe"));

  # Assemble command line and start aaS in the background
  my @aas_cmd = ();
  if ($Config{windows_nt}) {
    push @aas_cmd, "cmd.exe"; 
    push @aas_cmd, "/c";
  }

  push @aas_cmd, "$aas_exe";
  my $aas_pid = CFX5::Commands::bg_cmd(@aas_cmd);

  # Check aas executable was started
  unless( defined $aas_pid) {
    _error("Failed to start aaS executable: $!");
    return 0;
  }
  
  1;
}

1;
