#-*-perl-*-
#
# $Id: parse_config,v 25.1 2004/01/14 19:10:46 biersma Exp $
#
# (c) 1999-2004 Morgan Stanley Dean Witter and Co.
# See ..../src/LICENSE for terms of distribution.
#
# Parse our config file to get the parameters we need
#

foreach my $relpath ( qw( . .. ../.. ../../.. ) ) {
    next unless -f "$relpath/CONFIG";
    $config = "$relpath/CONFIG";
    last;
}

die "Unable to locate CONFIG file\n" unless -f $config;

use File::Basename;
#
# In the test scripts, we need to disable the non-lazy loading of
# symbols, since the IRIX client has lots of undefined symbols that
# can't be resolved.  Normally, these are never called, so not a
# problem.  However, since the non-lazy loading is an important test,
# we only disable it on IRIX.
#
use Config;
if ( $Config{osname} =~ /irix/ ) {
    $ENV{PERL_DL_NONLAZY} = '0';
}

open(CONFIG,"$config") or
  die "Unable to open CONFIG file: $ERRNO\n";

while ( <CONFIG> ) {
    next if /^\#/;
    next unless ($key,$value) = /^(\w+)\s*=\s*(.*)\s*$/;
    if ( $ENV{$key} ) {
	print "Environment variable '$key' overrides CONFIG definition\n";
	$myconfig{$key} = $ENV{$key};
    } else {
	$myconfig{$key} = $value;
    }
}

close(CONFIG) or
  die "Unable to close CONFIG file: $ERRNO\n";

#
# Validate the structure of the MQMTOP directory.  It has to exist,
# and have lib and inc (or include) subdirectories.
#

if ( defined $myconfig{MQMTOP} ) {
    $mqmtop = $myconfig{MQMTOP};
} else {

    #
    # Canonical default.  True for most of the UNIX platforms.
    #
    $mqmtop = "/opt/mqm";
    $systemdir = q{/var/mqm/qmgrs/@SYSTEM};

    if ( $Config{osname} =~ /aix/ ) {
	$mqmtop = "/usr/lpp/mqm";
    } elsif ( $Config{osname} eq 'os390' ) {
	$mqmtop = dirname($config);
    } elsif ( $Config{osname} =~ /win32/i ) {

      require "Win32/TieRegistry.pm";
      import Win32::TieRegistry;

      $Registry->Delimiter('/');

      my $CurrentVersion = "LMachine/SOFTWARE/IBM/MQSeries/CurrentVersion/";

      my ($mqmdir) = (
		      $Registry->{"$CurrentVersion/FilePath"} ||
		      $Registry->{"$CurrentVersion/WorkPath"} ||
		      "C:/Mqm"
		     );

      $mqmtop = "$mqmdir/Tools";

      $systemdir = $mqmdir . q{/qmgrs/@SYSTEM};

    }
}

#
# On OS/390 we create directories and catenate the PDS members into
# HFS files if necessary.
#
if ( $Config{"osname"} eq 'os390' ) {

    #
    # The high level qualifier for the .SCSQC370 header file pds
    # members and the .SCSQLOAD load module data set.
    #
    if ( defined $myconfig{HLQ} ) {
        $hlq = $myconfig{HLQ};
    } else {
        $hlq = 'MQS120';
    }

    mkdir ( "$mqmtop/lib", 0755 ) unless ( -d "$mqmtop/lib" );
    mkdir ( "$mqmtop/inc", 0755 ) unless ( -d "$mqmtop/inc" );

    foreach my $member (
			qw(CMQC
			   CMQCFC
			   CMQXC
			   CSQ4CBI
			   CSQ4CB0
			   CSQ4CMSG
			   CSQ4TCH0
			   CSQ4TC0)
		       ) {
        cat2hfs ( $member, $hlq, 'SCSQC370', "$mqmtop/inc" );
    }

}

unless ( -d $mqmtop ) {
    die "No such directory '$mqmtop'\n";
}

unless ( -d "$mqmtop/lib" ) {
    die "Missing lib directory in $mqmtop\n";
}

if ( -d "$mqmtop/inc" ) {
    $include = "$mqmtop/inc";
} elsif ( -d "$mqmtop/include" ) {
    $include = "$mqmtop/include";
} elsif ( -d "$mqmtop/C/include" ) {
    $include = "$mqmtop/C/include";
} else {
    die "Missing inc or include directory in $mqmtop\n";
}

#
# Ugh.  Need to figure out which version we are.  This is kinda ugly...
#
$mqversion = 2;

open(CMQC,"$include/cmqc.h")
  or die "Unable to open $include/cmqc.h: $ERRNO\n";

$mqsubversion = 0;

while ( <CMQC> ) {
    $mqversion = 5 if /MQCNO_NONE/;
    $mqsubversion = 1 if /MQGMO_VERSION_3/;
}

close(CMQC);

#
# This hack is probably going to go away in the future...
#
if ( defined $myconfig{MQMLOCALE} ) {
    $mqmlocale = $myconfig{MQMLOCALE};
} else {
    $mqmlocale = "";
}

#
# perhaps useful only on OS/390
#
sub cat2hfs {
    my $member = shift;
    my $hlq = shift;
    my $pdsdir = shift;
    my $incdir = shift;

    my $header = $member;
    $header =~ tr/[A-Z]/[a-z]/;
    $header = "$header.h";
    my $rc = 0;
    if ( ! -e "$incdir/$header" ) {
        $rc = `cat \"//'$hlq.$pdsdir($member)'\" > $incdir/$header`;
        die "$rc, Unable to catenate \"//'$hlq.$pdsdir($member)'\": $ERRNO\n"
          if ( $rc || $? );
    }
}

1;
