#! perl -w
#==============================================================================
# @(#) $Id$
#------------------------------------------------------------------------------
##
## Name:
##
## cfxdsdbreader
##
## Purpose:
##
## Wrapper for gtmDSDBReader.exe
##
## Synopsis:
#S
#S cfxdsdbreader [-help|gtmDSDBReader args]
#S
## []  denotes an optional argument.
## |   separates mutually exclusive arguments. 
## ... following an argument indicates that it may be repeated.
## <>  denotes that substitution of a suitable value is required. All other
##     arguments are keywords, some of which have a short form.
##
## COPYRIGHT ANSYS, ALL RIGHTS RESERVED
##
#------------------------------------------------------------------------------

# get the standard CFX-5 library directory
BEGIN {
 if ($0 =~ m!^([a-z]:)?[\\/]!i) {
    # it's an absolute path, use it as-is
    (my $nd=$0) =~ s!(perllib[\\/])?[^\\/]*$!!i;
    unshift @INC, "$nd/perllib";
  } else {
    # wrap this in an eval, and if it fails, pray ... for example,
    # this can fail on Windows if we're running off a share.
    eval {
      require FindBin;
      FindBin->import();
      unshift @INC, "$FindBin::Bin/perllib";
      my $dummy=$FindBin::Bin; # kill used only once warning
    };
    warn $@ if $@;
  }
  if (defined $ENV{CFX_LOCAL_PERLLIB}) {
     unshift @INC, $ENV{CFX_LOCAL_PERLLIB};
  }

}

$ENV{CFX_SUPPRESS_README}=1;

use CFX5::Init;
CFX5::Init::init();

use vars qw($CVSVersion);
sub BEGIN {
  $CVSVersion='@(#) $Id$\n';
}

use strict;

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

use CFX5::PathManip qw(join_file dirname prepend_to_path);

use CFX5::Commands qw(do_cmd parse_rv);

#
# read the configuration
#
CFX5::Config::read_config();

my %archdir=(
	"winnt" => "winnt",
	"winnt-amd64" => "winnt-amd64",
	"linux" => "linux",
	"linux-amd64" => "linux-amd64");

my $DSDBdir=$archdir{$Config{CFX5OS}};
_error("$0 not supported on the architecture") unless defined $DSDBdir;
exit_if_error();

_error("Workbench installation not detected") unless defined $Config{WORKBENCHINSTALLROOT};

my @cmd;
unless( $Config{windows_nt} ){
  my $wbcmd=join_file($Config{WORKBENCHINSTALLROOT}, ".workbench");
  _error("Workbench not found") unless -x $wbcmd;
  push @cmd, $wbcmd, '-cmd'; 

  # Workbench messes up passing arguments with spaces
  # If -names has spaces replace with commas
  my $i=0;
  while($i<scalar(@ARGV)){
    if($ARGV[$i] eq "-names"){
      $i++;
      $ARGV[$i]=~s! !,!g;
      last;
    }
    $i++;
  }
 
}

my $DSDBcmd=join_file($Config{CFX5BINDIR},$DSDBdir, "gtmDSDBReader.exe");
_error("$DSDBcmd not found") unless -x $DSDBcmd;
exit_if_error();
push @cmd, $DSDBcmd;

my $ldvar=$Config{ld_path_var};
prepend_to_path(\$Config{$ldvar}, join_file(dirname($Config{CFX5LIBDIR}), $DSDBdir));
export_conf($ldvar);

my $rv=parse_rv(do_cmd(@cmd, @ARGV));

exit($rv->{rv});

