# -*- cperl -*-
#
# Small frontend for ld that ought to catch common problems
# in the linking stage
#

open(OLDSTDERR, ">&STDERR") || die "Failed to backup STDERR: $!";
open(FILE, ">myld.stderr") || die "Failed to create myld.stderr: $!";
open(STDERR, ">&FILE") || die "Failed to redirect STDERR: $!";
$retval = system @ARGV;
open(STDERR, ">&OLDSTDERR");
close(FILE) || die "Failed to close myld.stderr: $!";
if (-f "myld.stderr"  &&  !-z _) {
  open(FILE, "<myld.stderr") || die "Failed to reopen myld.stderr: $!";
  local $/ = undef;
  my $contents = <FILE>;
  die "Failed to read myld.stderr: $!" unless defined($contents);
  close(FILE) || die "Failed to close myld.stderr: $!";

  if ($contents =~ /cannot find -l(g?z)/i) {
    my $missing = $1;
    print <<"MSG";
$contents

An error occurred while linking the DBD::mysql driver. The error
message seems to indicate that you don't have a libz.a, libgz.a,
libz.so or libgz.so. This is typically resolved by:

1.) You may try to remove the -lz or -lgz flag from the libs list
    by using the --libs switch for "perl Makefile.PL".
2.) On Red Hat Linux install libz-devel
3.) On other systems, please contact the mailing list

     Msql-Mysql-modules\@lists.mysql.com

For further hints, see INSTALL.html, section Linker flags.
MSG
    exit 1;
  }
}

if ($retval) {
  print STDERR $contents;
  exit 1;
}

END { unlink "myld.stderr"; }
