#!/Perl
# AIM: To reline HTML, just per tags
use strict;
use warnings;
use File::Basename;  # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] )
use Cwd;
my $os = $^O;
my $perl_dir = '/home/geoff/bin';
my $PATH_SEP = '/';
my $temp_dir = '/tmp';
if ($os =~ /win/i) {
    $perl_dir = 'C:\GTools\perl';
    $temp_dir = $perl_dir;
    $PATH_SEP = "\\";
}
unshift(@INC, $perl_dir);
require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl' Check paths in \@INC...\n";
# log file stuff
our ($LF);
my $pgmname = $0;
if ($pgmname =~ /(\\|\/)/) {
    my @tmpsp = split(/(\\|\/)/,$pgmname);
    $pgmname = $tmpsp[-1];
}
my $outfile = $temp_dir.$PATH_SEP."temp.$pgmname.txt";
open_log($outfile);

# user variables
my $VERS = "0.0.2 2012-06-28";

my ($IF);
##my $in_file = 'C:\Projects\Tidy\tests\tempmem.htm';
##my $in_file = 'C:\GTools\perl\fg-servers.htm';
my $in_file = 'C:\GTools\perl\fgms-servers.html';
#my $in_file = 'search[1].txt';
my $out_html = $temp_dir.$PATH_SEP.'temphtm2.htm';

my $chk_for_for = 1;
my $load_log = 2;

my $nlbrace = 0;
my $in_script = 0;
my $snlbrace = 0;
my $in_table = 0;
my $line;

my (@filearr,@newarr);

sub done_for($) {
    my $txt = shift;
    return 1 if ($txt =~ /^for\s*\((.+)\s+in\s+(.+)\)/);
    my $len = length($txt);
    my ($i,$ch);
    my @br = ();
    my $scnt = 0;
    my $incomm = 0;
    my $cc = '';
    for ($i = 0; $i < $len; $i++) {
        $ch = substr($txt,$i,1);
        if ($incomm) {
            $incomm = 0 if ($ch eq $cc);
        } elsif (($ch eq '"')||($ch eq "'")) {
            $incomm = 1;
            $cc = $ch;
        } else {
            if ($ch eq ';') {
                if (@br) {
                    $scnt++;
                } else {
                    prt("Got semicolon outside bracket [$txt]\n");
                    exit(1);
                }
            } elsif ($ch eq '(') {
                push(@br,$ch);
            } elsif ($ch eq ')') {
                if (@br) {
                    pop @br;
                    if (!@br && ($scnt == 2)) {
                        prt("Done for! [$txt]\n");
                        return 1;   # got a complete for statement
                    }
                }
            }
        }
    }
    prt("NOT Done for! [$txt]\n");
    return 0; # not done for
}

sub process_array2 {
	my ($i,$lin,$bit,$ch,$line,$cnt,$maxcnt);
	$bit = '';
	$cnt = 0;
    $maxcnt = scalar @filearr;
    prt( "Processing $maxcnt lines...\n" );
    my ($i2,$nc,$pc,$i3,$nnc);
    for ($lin = 0; $lin < $maxcnt; $lin++) {
        $line = $filearr[$lin];
		chomp $line;
		$line =~ s/\r//;
		while( $line =~ /\t/ ) {
			$line =~ s/\t/ /g;
		}
		my $ll = length($line);
		$cnt++;
        $ch = '';
		for ($i = 0; $i < $ll; $i++) {
            $pc = $ch;
			$ch = substr($line, $i, 1);
            $i2 = $i + 1;
            $i3 = $i + 2;
            $nc = ($i2 < $ll) ? substr($line,$i2,1) : '';
            $nnc = ($i3 < $ll) ? substr($line,$i3,1) : '';
			if ($ch eq '<') {
				if (length($bit)) {
					prt( "$cnt: [$bit]\n" );
					$bit .= "\n";
					###print $HF $bit;
					push(@newarr, $bit);
				}
				$bit = $ch;
			} elsif ($nlbrace && ($ch eq '{')) {
				$bit .= $ch;
                if ($nc eq '}') {
                    $bit .= $nc;
                    $i++;
                    $ch = $nc;
                    if ($nnc eq ';') {
                        $bit .= $nnc;
                        $i++;
                        $ch = $nnc;
                    }
                }
				prt( "$cnt: [$bit]\n" );
				$bit .= "\n";
				##print $HF $bit;
				push(@newarr, $bit);
				$bit = '';
			} elsif ($ch eq '}') {
				if ($nlbrace) {
					if (length($bit)) {
						prt( "$cnt: [$bit]\n" );
						$bit .= "\n";
						###print $HF $bit;
						push(@newarr, $bit);
					}
					prt( "$cnt: [$ch]\n" );
					$bit = $ch;
                    if ($nc eq ';') {
                        $bit .= $nc;
                        $i++;
                        $ch = $nc;
                    }
					$bit .= "\n";
					###print $HF $bit;
					push(@newarr, $bit);
				} else {
					$bit .= $ch;
                    if ($nc eq ';') {
                        $bit .= $nc;
                        $i++;
                        $ch = $nc;
                    }
					prt( "$cnt: [$bit]\n" );
					$bit .= "\n";
					###print $HF $bit;
					push(@newarr, $bit);
				}
				$bit = '';
			} elsif ($ch eq '>') {
				# closing item
				$bit .= $ch;
				if ($bit =~ /<script/i) {
					$in_script = 1;
					prt( "Entering SCRIPT\n" );
					$snlbrace = $nlbrace;
					$nlbrace = 1;
				} elsif ($bit =~ /<\/script/i) {
					$in_script = 0;
					prt( "Exit SCRIPT\n" );
					$nlbrace = $snlbrace;
				} elsif ($bit =~ /<table/i) {
					$in_table++;
					prt( "Entering TABLE $in_table\n" );
				} elsif ($bit =~ /<\/table/i) {
					prt( "Exit TABLE $in_table\n" );
					$in_table-- if $in_table;
				}
			} elsif ($in_script && ($ch eq ';')) {
				$bit .= $ch;
                if ($chk_for_for && ($bit =~ /^for\b/)) {
                    # stay and close the for(...;...;...)
                    my $done_for = done_for($bit);
                    while (!$done_for) {
                        $i++;   # go to next char
                		for (; $i < $ll; $i++) {
                            $pc = $ch;
                			$ch = substr($line, $i, 1);
                            $i2 = $i + 1;
                            $nc = ($i2 < $ll) ? substr($line,$i2,1) : '';
                            $bit .= $ch;    # add char
                            if ($ch eq ')') {
                                $done_for = done_for($bit);
                                if ($done_for) {
                                    if ($nc eq '{') {
                                        $i++;
                                        $bit .= $nc;
                                        $ch = $nc;
                                    }
                                    last;
                                }
                            }
                        }
                        if (!$done_for && (($lin + 1) < $maxcnt)) {
                            $lin++;
                            $line = $filearr[$lin];
                    		chomp $line;
		                    $line =~ s/\r//;
		                    while( $line =~ /\t/ ) {
			                    $line =~ s/\t/ /g;
		                    }
		                    $ll = length($line);
		                    $cnt++;
                            $i = -1;
                        }
                    }
                    prt( "$cnt: [$bit]\n" );
                    $bit .= "\n";
                    push(@newarr, $bit);
                    ###print $HF $bit;
                    $bit = '';
                } else {
                    prt( "$cnt: [$bit]\n" );
                    $bit .= "\n";
                    push(@newarr, $bit);
                    ###print $HF $bit;
                    $bit = '';
                }
			} else {
				$bit .= $ch;
			}
		}
		if (length($bit)) {
			prt( "$cnt: [$bit]\n" );
			$bit .= "\n";
			###print $HF $bit;
			push(@newarr, $bit);
		}
		$bit = '';
	}
}



####prt( "$0 ...Hello, World...\n" );
open $IF, "<$in_file" or mydie( "ERROR: Can not open $in_file ...\n" );

@filearr = <$IF>; # slurp whole file into an array
my $lc = scalar @filearr;
prt( "$in_file contain $lc line ...\n" );
process_array2();
$lc = scalar @newarr;
prt( "Got $lc line in new array ...\n" );
write2file(join("",@newarr)."\n",$out_html);
###system($out_html);
system("ep $out_html");
close_log($outfile,$load_log);
exit(0);


### eof - relinehtm.pl
