#!/ms/dist/perl5/bin/perl
#
# NOTE: The RCS Id is down the here-doc that gets embedded in the
# resulting files.  My RCS tools like there to be one of htese, so it
# gets "re-used".  
#
# (c) 1999-2007 Morgan Stanley Dean Witter and Co.
# See ..../src/LICENSE for terms of distribution.
#
# This bit of hackery parses the IBM HTML docs for reason code, and
# extracts a macro to text string mapping.  This is just a pipeline
# tool, too, so you have to redirect the output whereever appropriate.

use File::Basename;

#
# Ah, what a hack...
#
# A couple of the reason codes are tricky to parse, so cheat, and seed
# things...
#
%text =
  (
   MQRCCF_CLUSTER_NAME_CONFLICT => "ClusterName and ClusterNamelist attributes conflict.",
   MQRCCF_REPOS_NAME_CONFLICT   => "RepositoryName and RepositoryNamelist attributes conflict.",
  );

while ( <> ) {

    chomp;
    s/\s+$//;

    # <P><DT>MQRC_ADAPTER_DEFS_LOAD_ERROR
    if ( /<P><DT>(MQRC(?:CF)?_\S+)/ ) {
	$reason = $1;
	$text{$reason} = undef unless defined $text{$reason};
	$maxlength = length($reason) if length($reason) > $maxlength;
    }
    # <DD>(2132, X'854') Unable to load adapter subsystem definition module.
    elsif ( /^(?:<\/DT>)?<DD>\(\d+,\s+X\'[^\']+\'\)\s+(.*)/ ) {
        my $desc = $1;
        $desc =~ s!</?\w[^>]+>!!g;
	$text{$reason} = $desc unless defined $text{$reason};
    }
    elsif ($reason && /^(?:<\/DT>)?<DD>(.*)/ ) {
        my $desc = $1;
        $desc =~ s!</?\w[^>]+>!!g;
	$text{$reason} = $desc unless defined $text{$reason};
    }
}
#$maxlength--;

#
# Hack, hack... read the files generated from a previous release and add
# those entries that are no longer generated or parseable.
#
open (OLD, "/ms/dev/perl5/MQSeries/1.17/src/pre.in/MQSeries/Constants/ReasonText.in") || die "Cannot open: $!";
while (<OLD>) {
    #&MQRCCF_CFST_STRING_LENGTH_ERR  => "String length not valid.",
    next unless (/&(MQR\S+)\s+=>\s+\"(.*?)\"/);
    my ($reason, $desc) = ($1, $2);
    $text{$reason} = $desc;
}
close OLD;
             

foreach my $type ( qw( ReasonText ReasonMacro ) ) {
    
    my $file = "pre.in/MQSeries/Constants/$type.in";

    die "Unable to write to $file.\n" unless -w $file;

    my $tmpfile = "$file.$$";

    warn "Updating $file\n";

    open(OUT,">$tmpfile") || die "Unable to open $tmpfile: $!\n";

    print OUT <<"EndOfHeader";
# -*-perl-*-
#
# WARNING: This file is automatically generated.
# Any changes made here will be mercilessly lost.
#
# You have been warned, infidel.
#
# This file is auto-generated by parsing the IBM MQSeries HTML
# documentation page for Reason Codes.  To see how this is done in all
# its glory (or lack thereof), see:
#
#    ..../src/util/extract_reason_codes
#
# \$Id: extract_reason_codes,v 27.1 2007/01/11 20:20:45 molinam Exp $
#
# (c) 1999-2007 Morgan Stanley Dean Witter and Co.
# See ..../src/LICENSE for terms of distribution.
#

package MQSeries::Constants;

%$type =
  (
EndOfHeader

    foreach $reason ( sort keys %text ) {

	unless ( defined($text{$reason} ) ) {
	    warn "Missing reason text for $reason\n";
	    next;
	}

	print OUT "   &$reason" . " " x ($maxlength - length($reason)) . " => ";

	if ( $type eq 'ReasonText' ) {
	    print OUT qq{"$text{$reason}",\n};
	}
	else {
	    print OUT qq{"$reason",\n};
	}
	
    }

   print OUT <<"EndOfFooter";
  );

1;
EndOfFooter

    close(OUT) || die "Unable to close $tmpfile: $!\n";

    rename($tmpfile,$file) || die "Unable to rename $tmpfile to $file: $!\n";

}

