#!/usr/bin/perl 
#
#   by Gerard Menicucci   BDGP 7/13/2000
# 
#     fxsql scriptname | hsel

use strict;

#my $infilename = "junkin.lst";
my $infilename = $ARGV[0];
my $useSTDIN = "N";
if (!defined $infilename || $infilename eq "") {
   $useSTDIN = "Y";
   }

if ($useSTDIN eq "N") { 
  open (INFILE, $infilename) or die "Can't find input file $infilename";
  }

my $outfilename = "junk" . getppid . ".lst";
open (OUTFILE, ">" . $outfilename) or die "Can't find output file $outfilename";

my $row;
my $col;
my $line;
my @fields;
my @headings = ();
my @maxfieldlengths = ();
my @fieldtype = ();
my $parserowcount = 0;
for ($row=0;;) {
    @fields = ();
    for ($col=0;;) {
        if ($useSTDIN eq "Y") {
           $line = <STDIN>;
           }
        else {
           $line = <INFILE>;
           }
        last if (!defined($line)); #EOF 
        chomp $line;
        last if ($line eq "");
        next if ($line =~ /^Database selected.$/);
        next if ($line =~ /^Database closed.$/);
        if ($line =~ /^(\d+)\srow\(s\) retrieved.$/) {
          $parserowcount = $1;
          next;
          }
        if ($line =~ /^(\d+)\s+row\(s\) found$/) {
          $parserowcount = $1;
          next;
          }
        if ($line =~ /(\d+)\s+row\(s\) found/) {
          $parserowcount = $1;
          next;
          }
        if ($line =~ /^\s+(.*)$/) { # CASE 1: field continuation
           $fields[$col-1] .= $1;  # concat continuation line to previous field
           $col--;
           }
        else {  # CASE 2: text line starts on next line after heading  ??? NOT completely worked out at this time ???
                # only works if text line has no embedded spaces for first 40 chars  ??
          my $firstword = $line;
          $firstword =~ s/^(\S+)\s*(.*)$/$1/;
          if (length($firstword) > 40 && $fields[$col-1] eq "") {  
             # probably text field pushed to next line. 
             $fields[$col-1] = $line;
             $col--;
             }
          else { # CASE 3: NORMAL
            $fields[$col] = $line;
            $fields[$col] =~ s/^(\S+)\s+(.*)$/$2/;
            $headings[$col] = $line;
            $headings[$col] =~ s/^(\S+)\s*(.*)$/$1/;
            }
          }
        if (!defined($fields[$col])) {
           $fields[$col] = "";
           }
        if (!defined($fieldtype[$col])) {
           $fieldtype[$col] = "";
           }
        if (!defined($maxfieldlengths[$col])) {
           $maxfieldlengths[$col] = 0;
           }
        if (length($headings[$col]) > $maxfieldlengths[$col]) {
            $maxfieldlengths[$col] = length($headings[$col]);
            }
        if (length($fields[$col]) > $maxfieldlengths[$col]) {
           $maxfieldlengths[$col] = length($fields[$col]);
           }
        if ($fields[$col] eq "") {
           }
        elsif ($fields[$col] =~ /^[0123456789.-]+$/) {
           $fieldtype[$col] = "N" if ($fieldtype[$col] ne "C");
           }
        else {
           $fieldtype[$col] = "C";
           }
        $col++;
        }
    if (scalar(@fields) != 0) { # consecutive blank lines triggers 
       $row++;
       print OUTFILE join('|', @fields) . "\n";
       }
    last if (!defined($line)); #EOF 
    }


close INFILE;
close OUTFILE;

open (INFILE, $outfilename) or die "Can't find output file $outfilename";

# for ($col=0; $col < scalar(@headings); $col++) {
#    print $headings[$col], " ", $maxfieldlengths[$col], " ", $fieldtype[$col], "\n";
#    }

my $displayline;

# print headings
$displayline = "";
for ($col=0; $col < scalar(@headings); $col++) {
   my $padlength = $maxfieldlengths[$col] - length($headings[$col]);
   my $displayfield;
   $displayfield =  $headings[$col] . " " x $padlength;
   if ($fieldtype[$col] eq "N") {
       $displayfield =  " " x $padlength . $headings[$col];
       }
   $displayline .= $displayfield . " ";
   }
$displayline =~s/^(.*?)\s*$/$1/ ; # get rid of trailing spaces
print "$displayline\n";

$displayline = "";
for ($col=0; $col < scalar(@headings); $col++) {
   my $padlength = $maxfieldlengths[$col] - length($headings[$col]);
   my $displayfield;
   $displayfield =  "-" x $maxfieldlengths[$col];
   $displayline .= $displayfield . " ";
   }
$displayline =~s/^(.*?)\s*$/$1/ ; # get rid of trailing spaces
print "$displayline\n";

my $rowcount = 0;
for ($rowcount=0; ;$rowcount++) {
  $line = <INFILE>;
  last if (!defined($line)); #EOF 
  #print $line;
  chomp $line;
  @fields = split '\|', $line;
 $displayline = "";
  for ($col=0; $col < scalar(@fields); $col++) {
     my $padlength = $maxfieldlengths[$col] - length($fields[$col]);
     my $displayfield;
     $displayfield =  $fields[$col] . " " x $padlength;
     if ($fieldtype[$col] eq "N") {
         $displayfield =  " " x $padlength . $fields[$col];
         }
     $displayline .= $displayfield .  " ";
     }
  $displayline =~s/^(.*?)\s*$/$1/ ; # get rid of trailing spaces
  print "$displayline\n";
  }

print "\n$rowcount row(s) found\n";

if ($parserowcount != 0 && $parserowcount != $rowcount)  {
  print "\n!!! Possible error in row count ($parserowcount vs. $rowcount) !!!\n";
  }

close INFILE;
unlink($outfilename);
