#!/usr/bin/perl

use IO::Socket;

# this script retrieves the header file for CLAPACK and modifies
# it so that it fits into the LTILib scheme.

my $server='www.netlib.org';
my $remotefile="/clapack/clapack.h";
my $localfile=$ENV{'HOME'}."/ltilib/misc/lamath/clapack.h";

if ( -f $localfile ) {
  print STDERR "$localfile already exists.\n";
  exit;
}

my $sock=IO::Socket::INET->new(PeerAddr => $server,
                               PeerPort => 'http(80)',
                               Proto    => 'tcp');

if ($sock->opened()) {
  
  print $sock "GET $remotefile HTTP/1.0\r\n\r\n";
  $sock->flush();

  my @lines=<$sock>;

  close($sock);

  #print join('',@lines);

  open(OUT, ">$localfile") || die "Cannot open $localfile: $!";
  # state: reading HTTP header
  my $hmode=1;
  # state: reading #define statements
  my $dmode=0;
  print OUT qq?
/**
 * \@file clapack.h
 * This file has been generated by executing \$ltilib/tools/perl/getclapackh
 * It is based on the original clapack.h from http://www.netlib.org/clapack/
 * As such, this file is not part of the LTIlib, but included here for
 * convenience. It is copyrighted by the CLAPACK developers.
 * A documentation of the routines is contained in the book:
 * E. Anderson, Z. Bai, C. Bischof, S. Blackford, J. Demmel, J. Dongarra,
 * J. Du Croz, A. Greenbaum, S. Hammarling, A. McKenney, and D. Sorensen: 
 * LAPACK Users' Guide. 3rd edition. Society for Industrial and Applied
 * Mathematics, Philadelphia, 1999. ISBN 0-89871-447-8
 * 
 * The getclapackh tool included with the LTIlib automatically makes
 * some modifications to the original clapack.h. These are as follows:
 * - this comment is prepended to the file
 * - the file includes f2c.h that comes with the f2clib.
 * - the file includes ltiConfig.h for determining if LAPACK is installed
 * - instead of the fixed function names, the prototypes are based on
 *   the F77_FUNC macro (which is generated by configure) for determining 
 *   the mangled C-name of a FORTRAN function name. 
 * 
 */

?;#'

  foreach my $l (@lines) {
    # skip HTTP reply header
    if ($hmode) {
      next if ($l !~ /^\#/);
      $hmode=0;
      $dmode=1;
    }
    if ($dmode) {
      print OUT $l;
      if ($l !~ /^\#/) {
        # done with original define's
        print OUT "#include \"ltiConfig.h\"\n";
        print OUT "#ifdef HAVE_LAPACK\n";
        print OUT "#include <f2c.h>\n\n";
        print OUT "extern \"C\" {\n";
        $dmode=0;
      }
    } else {
      if ($l =~ /^\#endif/) {
        # have found end of file
        print OUT "}\n";
        print OUT "#endif /* HAVE_LAPACK */\n";
      } 
      if ($l =~ /Subroutine \*\/ int (\w+)_\(/) {
        my $name=$1;
        my $lname=lc($name);
        my $uname=uc($name);
        print OUT "#define LA_$uname F77_FUNC($lname,$uname)\n";
        $l =~ s/ int \w+_\(/ int LA_$uname\(/;
        print OUT $l;    
      } else {
        print OUT $l;
      }
    }
  }
} else {
  print STDERR "Could not open socket: $!\n";
}

