=head1 NAME

CFX5::PDB - Patran Database Access setup for CFX-5

=head1 SYNOPSIS

  use CFX5::PDB;
  # ...
  CFX5::PDB::setup_environment();

=head1 DESCRIPTION

This module provides routines for use by modules which use external
programs which link in the Patran "dbaccess.a" database access
libraries, such as CFX-Build, the GE mesher and the refiner.  It does
I<not> provide database access routines for Perl programs!

Basically, this module serves as a central place to store the list of
environment variables to export, the directories to add to the
C<LD_LIBRARY_PATH>, and other miscellaneous setup.

=cut

package CFX5::PDB;

use strict;
use CFX5::Config qw(%Config export_conf 
		    append_to_execpath prepend_to_libpath append_to_libpath);
use CFX5::PathManip qw(join_file prepend_to_path);

use vars qw($CVSVersion);
$CVSVersion='@(#) $Id$\n';

=head2 setup_environment

Not exportable.

  CFX5::PDB::setup_environment();

This function takes no arguments.  It will do the following:

=over

=item 1.

Append the CFX-Build 5 C<bin> directory to the C<PATH> variable.

=item 2.

Prepend the CFX-Build 5 C<lib> and C<interbase/lib> directories to the
C<LD_LIBRARY_PATH> variable, or whatever the equivalent is for this
machine.  On IRIX, this modifies both C<LD_LIBRARYN32_PATH> and
C<LD_LIBRARY_PATH> as appropriate.

=item 3.

Export various environment variables used by the PDB libraries.  This
includes things like P3_ENABLE_NFS_DB_ACCESS (which I believe is no
longer used), and the PDB_* variables; it does I<not> include
variables such as C<P3_HOME> used by Patran itself.

Actually, that is a small lie; it does export C<P3_HOME> and
C<P3_INITIAL_PATH>, because they are needed to find the F<dbport>
utility.  But the gist of the comment still applies!

=back

=cut

sub setup_environment {
  #
  # make sure the required things are on the library and program paths
  #
  append_to_execpath(join_file($Config{P3_HOME}, "bin"));
  prepend_to_libpath(join_file($Config{P3_HOME}, "lib"),
		     join_file($Config{P3_HOME}, "interbase", "lib"));
  if ($Config{windows_nt}) {
    append_to_libpath(join_file($Config{P3_HOME}, "bin", 'exe'));
  }

  if ($Config{CFX5OS} eq 'irix' 
      and $Config{ld_path_var} ne 'LD_LIBRARY_PATH') {
    # on IRIX, and either n32 or 64 ABIs, so set up LD_LIBRARY_PATH
    # for Interbase access.
    $Config{LD_LIBRARY_PATH}='' unless defined $Config{LD_LIBRARY_PATH};
    prepend_to_path(\$Config{LD_LIBRARY_PATH},
		    join_file($Config{P3_HOME}, "lib_o32"),
		    join_file($Config{P3_HOME}, "interbase", "lib"));
    CFX5::Config::export_conf('LD_LIBRARY_PATH');
  }    

  #
  # this is for the IBM, where we need to override the system
  # libraries with whatever libraries CFX-5 was compiled with.
  # CFX5LIBDIR/runtime is the place where they are stuck when
  # compiled, but is not distributed.  P3_HOME/librl is the place
  # where they are distributed to customers; these are the ones we get
  # from MSC.
  #
  unless (defined $Config{CFX5_P3_RUNLIBS}) {
    for my $dir (join_file($Config{CFX5LIBDIR},'runtime'),
		 join_file($Config{P3_HOME},'librl')) {
      if (-d $dir) {
	$Config{CFX5_P3_RUNLIBS}=$dir;
	export_conf('CFX5_P3_RUNLIBS');
	last;
      }
    }
  }
  prepend_to_libpath($Config{CFX5_P3_RUNLIBS})
    if defined $Config{CFX5_P3_RUNLIBS};

  #
  # now the exciting bit: make sure we have exported the variables we
  # need 
  #
  CFX5::Config::export_conf
    (qw(P3_ENABLE_NFS_DB_ACCESS

	P3_HOME P3_INITIAL_PATH

	P_SCHEMA

	PDF_MIN_DISK_BLOCK_SIZE PDF_PAGE_COMPRESSION_MIN
	PDF_LOCKING_DISABLED PDF_MAX_UNDOS PDB_C_CACHE_SIZE
	PDB_C_MAX_FILES PDB_C_SWAP_LARGE_PAGE_MIN PDB_C_SWAP_DIR
	PDB_C_KEEP_SWAP PDB_B_BTREE_PAGE_SIZE PDB_A_ARRAYTREE_PAGE_SIZE
	PDB_A_DATA_PAGE_SIZE

        P3UPGRADE2001R1_TEMPLATE
       ));

  (my $p3vr=$Config{P3_VERSION}) =~ s/[^\d.].*$//;
  if ($Config{windows_nt} and $p3vr < 9.0) {
    # this forces all programs to use the MSVC 5 heap at runtime.
    # This seems to be required at least by CFX-Build and the mesh
    # inflation code, maybe by more.  If possible, the codes should be
    # fixed and this workaround removed, or at least restricted to the
    # executables which need it by setting it to (for example) 
    #   'E:\CFX-5.4-BETA\BUILD\5.4BETA\WINNT\CFXBUILD5.EXE,2'
    #
    # This seems to be undocumented, but see the HEAPINIT.C file in
    # the Microsoft CRT source.
    $Config{__MSVCRT_HEAP_SELECT}='__GLOBAL_HEAP_SELECTED,2';
    export_conf('__MSVCRT_HEAP_SELECT');
  }

  1;
}

1;
