=head1 NAME

  CFX5::Parallel::Reqs::pvmhosts - PVMHosts generator for CFX-5 parallel

=cut

package CFX5::Parallel::Reqs::pvmhosts;

use CFX5::Parallel::Reqs::Base;
push @ISA, qw(CFX5::Parallel::Reqs::Base);

use CFX5::Config qw(%Config);
use CFX5::Output qw(:all);

use CFX5::PkgLoader qw(cfx5_require);

use CFX5::FileOps qw(copy_file_for_read);

use File::Temp qw(tmpnam);

use strict;

sub setup {
  my $self=shift;
  my $workdir=shift;

  unless (defined $workdir) {
    internal_error("Cannot make a PVM hosts file without a working directory object");
  }

  # we always remove and re-write
  $workdir->remove_file('PVMhosts') if $workdir->have_file('PVMhosts');
  my $file=$workdir->creatable_file('PVMhosts');

  cfx5_require('CFX5::Parallel::PvmHosts');

  unless (CFX5::Parallel::PvmHosts->write($file, $self->ppe()->get_ccldb_obj(),
                                          $self->ppe()->all_valid_hosts())) {
    _error("Error writing PVM hosts file: $!");
    return undef;
  }

  # Link to a temporary file with no spaces
  my $tempfile=tmpnam();
  copy_file_for_read($file, $tempfile);

  $Config{PVM_HOSTS_FILE} = $tempfile;

  return $file;
}

1;
