#!/usr/bin/gawk -f

function printtab()  {
	    currow[tnst]++;

	    printf "+";
	    col = 1;
	    while( col <= maxcol ) {
	      printf "%s+" , substr( hrule , 0, colwid[tnst,col++] );
	    }
	    print "";

	    row = 1;
	    while( row < currow[tnst] ) {
	      col = 1;
	      printf "|";
	      while( col <= maxcol ) {

		ttext[tnst,row,col] = ttext[tnst,row,col] spaces;

		if( tcs[tnst,row,col] == 1 ) {
		  printf "%s|", substr( ttext[tnst,row,col] ,0,colwid[tnst,col]) ;
		  col++;
		}
		else if( tcs[tnst,row,col] == 0 ) {
		  if( col == 1 ) {
#BLANK ROW
		    col = 1;
		    while( col <= maxcol ) {
		      printf "%s+" , substr( hrule , 0, colwid[tnst,col++] );
		    }
		    col = 2;
		  }
		  else {
#tail of colspan
		    col++;
#		       printf "*";
		  }
		}
#colspan > 1
		else {
		  len = 1;
		  cnt = 0;
#awk sometimes gets confused when comparing 9 with 10 (num v.s. alpha)	
		  while( cnt + 1 < 1 + tcs[tnst,row,col] ) {
		    len += colwid[tnst,col+cnt]+1;
#printf "[%d %d]\n" , cnt , len;
		    cnt++;
		  }
		  printf "%s|", substr( ttext[tnst,row,col] ,0,len-2);
		  col ++;
		}
		
	      }

	      row++;
	      print "";
	    }

	    printf "+";
	    col = 1;
	    twid = 1;
	    while( col <= maxcol ) {
	      len = colwid[tnst,col++];
	      twid += len+1;
	      printf "%s+" , substr( hrule , 0, len );
	    }
	    thght = row+1;
	    print "";
	    print "TABLE " tnst "." curtbl[tnst-1] ", " twid "x" thght;
	    print "";
system("");
}


BEGIN { 
  RS = "\<" ; 
  FS = "\>" ; 

  tnst = 0;
  colsp = 0;
  rowsp = 0;
  dflg = 0;
  loff = 0;
  maxcol = 0;
  lofmx = 1;
  tralready = 0;
hrule = "-----------------------------------------------------------------";
spaces ="                                                                 ";
#  colwid = 12;
}

{
  if( substr(toupper($1),0,5) == "TABLE" ) {
    curtbl[tnst]++;
    if( tnst ) {
      ttext[tnst,currow[tnst],curcol[tnst]] = " [ Nested Table " tnst+1 "." curtbl[tnst] " ]";
      tcs[tnst,currow[tnst],curcol[tnst]] = 1;
      col = length( ttext[tnst,currow[tnst],curcol[tnst]]);
      if( col > colwid[tnst,curcol[tnst]] && colsp == 1 )
	colwid[tnst,curcol[tnst]] = col;
    }
    tnst++;
    curtbl[tnst] = 0;
    currow[tnst] = 0;
    curcol[tnst] = 0;
    maxcol = 0;
    col = 1;
      tralready = 0;
    while( col < 64 ) {
      colwid[tnst,col] = 0;
      rowspan[col++] = 1;
    }
    
  }
  
  if( tnst ) {	
    if( substr(toupper($1),0,2) == "TR" ) {
      if( !tralready ) {
      curcol[tnst] = 0;
      currow[tnst]++;
      }
      tralready = 0;
    }
    
    if( substr(toupper($1),0,2) == "TH" \
	|| substr(toupper($1),0,2) == "TD" ) {
      curcol[tnst]++;
      colsp = 1;
      rowsp = 1;
      ttext[tnst,currow[tnst],curcol[tnst]] = "";
      if( match(toupper($0), "COLSPAN=") )
	colsp = substr($0,RSTART+8,5); 
      if( match(toupper($0), "ROWSPAN=") )
	rowsp = substr($0,RSTART+8,5); 
      rowspan[curcol[tnst]] = rowsp;
      tcs[tnst,currow[tnst],curcol[tnst]] = colsp;
    }
    
    if( substr(toupper($1),0,2) == "BR" )
      ttext[tnst,currow[tnst],curcol[tnst]] = ttext[tnst,currow[tnst],curcol[tnst]] "/";
    
    gsub("\n","",$0);
    gsub("\046amp;","+",$0);
    gsub("\046#169;","(C)",$0);
    
    sub("\r$","",$2);
    sub("\t$","",$2);
    gsub("\t"," ",$2);
    gsub(" $","",$2);
    
    if( NF > 1 && length($2) ) {
      ttext[tnst,currow[tnst],curcol[tnst]] = (ttext[tnst,currow[tnst],curcol[tnst]] $2);
    }
    
    if( substr(toupper($1),0,3) == "/TD" \
	|| substr(toupper($1),0,3) == "/TH" ) {
      
      col = length( ttext[tnst,currow[tnst],curcol[tnst]]);
      if( col > colwid[tnst,curcol[tnst]] && colsp == 1 )
	colwid[tnst,curcol[tnst]] = col;
      
      while( colsp > 1 ) {
	curcol[tnst]++;
	tcs[tnst,currow[tnst],curcol[tnst]] = 0;
	ttext[tnst,currow[tnst],curcol[tnst]] = "";
       	colsp--;
      }
      if( curcol[tnst] > maxcol )
	maxcol = curcol[tnst];
      tdflag--;
    }
    
    if( substr(toupper($1),0,3) == "/TR" ) {
      curcol[tnst] = 0;
      currow[tnst]++;
      tralready = 1;
    }


  }

  if( substr(toupper($1),0,6) == "/TABLE" ) {
    if( tralready ) {
      currow[tnst]--;
      tralready = 0;
    }
    if( currow[tnst] )
      printtab();
    inrow = 0;
    tdflag = 0;
    currow[tnst] = 0;
    tnst--;
  }
  
}

END { 
print ""; 
}
