#!/usr/bin/perl -w
# NAME: xmlreline.pl
# AIM: Simple read and re-line an XML file input, write to output, if one given
# 20/12/2011 - Review only - no major changes
use strict;
use warnings;
use File::Basename;  # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] )
use Cwd;
my $perl_dir = 'C:\GTools\perl';
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 = $perl_dir."\\temp.$pgmname.txt";
open_log($outfile);

# user variables
my $VERS = "0.0.2 2011-12-20";
my $keep_spaces = 0; # preserve spaces in text, or NOT
my $load_log = 0;
my $in_file = '';
my $out_xml = '';
my $verbosity = 0;
my $debug_on = 0;
# my $def_file = 'C:\FG\29\Atlas-0.4.9\projects\msvc\temp2\fgxcaps.xml';
my $def_file = 'C:\DTEMP\osm-1153716.gpx';   # ;C:\DTEMP>np osm-1153716.gpx
my $def_out = $perl_dir."\\tempout.xml";

### program variables
my @warnings = ();
my $cwd = cwd();
my $os = $^O;

sub VERB1() { return $verbosity >= 1; }
sub VERB2() { return $verbosity >= 2; }
sub VERB5() { return $verbosity >= 5; }
sub VERB9() { return $verbosity >= 9; }

sub show_warnings($) {
    my ($val) = @_;
    if (@warnings) {
        prt( "\nGot ".scalar @warnings." WARNINGS...\n" );
        foreach my $itm (@warnings) {
           prt("$itm\n");
        }
        prt("\n");
    } else {
        ### prt( "\nNo warnings issued.\n\n" );
    }
}

sub pgm_exit($$) {
    my ($val,$msg) = @_;
    if (length($msg)) {
        $msg .= "\n" if (!($msg =~ /\n$/));
        prt($msg);
    }
    show_warnings($val);
    close_log($outfile,$load_log);
    exit($val);
}


sub prtw($) {
   my ($tx) = shift;
   $tx =~ s/\n$//;
   prt("$tx\n");
   push(@warnings,$tx);
}

sub process_in_file($) {
    my ($inf) = @_;
    if (! open INF, "<$inf") {
        pgm_exit(1,"ERROR: Unable to open file [$inf]\n"); 
    }
    my @lines = <INF>;
    close INF;
    my $lncnt = scalar @lines;
    prt("Processing $lncnt lines, from [$inf]...\n");
    my ($line,$inc,$lnn);
    my ($len,$ch,$pc,$nc,$i,$i2);
    my ($tag,$txt,$intag,$inquot,$qc,$ntag,$ltag,@arr,$indent,$scnt);
    my $nline = 0;
    my @tagstack = ();
    $lnn = 0;
    $ch = '';
    $intag = 0;
    $inquot = 0;
    $tag = '';
    $txt = '';
    $ntag = '';
    $scnt = 0;
    $indent = '';
    my $xml_msg = '';
    my $on_new_line = 1;
    foreach $line (@lines) {
        chomp $line;
        $lnn++;
        next if ($line =~ /^\s*$/);
        $len = length($line);
        for ($i = 0; $i < $len; $i++) {
            $pc = $ch;
            $ch = substr($line,$i,1);
            $i2 = $i + 1;
            $nc = ($i2 < $len) ? substr($line,$i2,1) : "";
            if ($intag) {
                $tag .= $ch;
                if ($inquot) {
                    $inquot = 0 if ($ch eq $qc);
                } else {
                    if ($ch eq '>') {
                        $intag = 0;
                        $txt = trim_tailing($txt) if (!$keep_spaces);
                        if (length($txt)) {
                            if ($on_new_line) {
                                $indent = ' ' x $scnt;
                                prt($indent.$txt) if (VERB1());
                                $xml_msg .= $indent.$txt;
                            } else {
                                prt($txt) if (VERB1());
                                $xml_msg .= $txt;
                            }
                            $on_new_line = 0;   # now NOT on a new line
                        }
                        if ($tag =~ /^<\//) {
                            $nline = 1;
                            $ntag =~ s/^\///;
                            # closing a tag
                            if (@tagstack) {
                                $ltag = pop @tagstack;
                                if ($ltag ne $ntag) {
                                    prtw("WARNING: $lnn: Closing tag [$ntag], not last [$ntag]! [$line]\n");
                                }
                                $scnt = scalar @tagstack;
                            } else {
                                prtw("WARNING: $lnn: Closing tag [$ntag], but no stack! [$line]\n");
                            }
                        } elsif ($tag =~ /\/>$/) {
                            $nline = 1;
                        } elsif ($tag =~ /^<\?/) {
                            $nline = 1;
                            $tag = trim_all($tag);
                        } elsif ($tag =~ /^<!/) {
                            $nline = 1;
                            $tag = trim_all($tag);
                        } else {
                            $nline = 0;
                            if ($ntag =~ /\s/) {
                                # contains a space, thus maybe has attributes
                                @arr = split(/\s/,$ntag);
                                $ntag = $arr[0];
                            }
                            push(@tagstack,$ntag);
                            #$scnt = scalar @tagstack;
                        }
                        $indent = '';
                        if (length($txt) == 0) {
                            $indent = ' ' x $scnt;
                        }
                        prt($indent."$tag") if (VERB1());
                        $xml_msg .= $indent."$tag";
                        $on_new_line = 0;
                        if ($nline) {
                            prt("\n") if (VERB1());
                            $xml_msg .= "\n";
                            $on_new_line = 1;
                        }
                        $scnt = scalar @tagstack;
                        $tag = '';
                        $txt = '';
                    } else {
                        # continuing an IN TAG
                        $ntag .= $ch;
                        if (($ch eq '"')||($ch eq "'")) {
                            $qc = $ch;
                            $inquot = 1;
                        } elsif ($ch eq '[') {
                            $qc = ']';
                            $inquot = 1;
                        }
                    }
                }
            } else {
                # awaiting start of tag
                if ($ch eq '<') {
                    $tag = $ch;
                    $ntag = '';
                    $intag = 1;
                    if ((length($txt) == 0)&&($nline == 0)) {
                        prt("\n") if (VERB1());
                        $xml_msg .= "\n";
                        $on_new_line = 1;
                    }
                    # prt("$lnn: Begin tag\n");
                } else {
                    if ($ch =~ /\s/) {
                        if ($keep_spaces) {
                            $txt .= $ch if (length($txt));
                        } elsif (length($txt)) {
                            $txt .= $ch if ($txt =~ /\S$/);
                        }
                    } else {
                        $txt .= $ch;
                    }
                }
            }
        } # for length of each line
    }
    if ((length($out_xml))&&(length($xml_msg))) {
        write2file($xml_msg,$out_xml);
        prt("Written XML to [$out_xml]\n");
    } elsif (length($xml_msg)) {
        prt("No output file given, so xml not written...\n");
    }
}

#########################################
### MAIN ###
parse_args(@ARGV);
### prt( "$pgmname: in [$cwd]: Hello, World...\n" );
process_in_file($in_file);
pgm_exit(0,"");
########################################
sub give_help {
    prt("$pgmname: version $VERS\n");
    prt("Usage: $pgmname [options] in-file\n");
    prt("Options:\n");
    prt(" --help  (-h or -?) = This help, and exit 0.\n");
    prt(" --verb[n]     (-v) = Bump [or set] verbosity. def=$verbosity\n");
    prt(" --load        (-l) = Load log at end. ($outfile)\n");
    prt(" --out <file>  (-o) = Write output to this file.\n");
}
sub need_arg {
    my ($arg,@av) = @_;
    pgm_exit(1,"ERROR: [$arg] must have a following argument!\n") if (!@av);
}

sub parse_args {
    my (@av) = @_;
    my ($arg,$sarg);
    while (@av) {
        $arg = $av[0];
        if ($arg =~ /^-/) {
            $sarg = substr($arg,1);
            $sarg = substr($sarg,1) while ($sarg =~ /^-/);
            if (($sarg =~ /^h/i)||($sarg eq '?')) {
                give_help();
                pgm_exit(0,"Help exit(0)");
            } elsif ($sarg =~ /^v/) {
                if ($sarg =~ /^v.*(\d+)$/) {
                    $verbosity = $1;
                } else {
                    while ($sarg =~ /^v/) {
                        $verbosity++;
                        $sarg = substr($sarg,1);
                    }
                }
                prt("Verbosity = $verbosity\n") if (VERB1());
            } elsif ($sarg =~ /^l/) {
                $load_log = 1;
            } elsif ($sarg =~ /^o/) {
                need_arg(@av);
                shift @av;
                $sarg = $av[0];
                $out_xml = $sarg;
                prt("Set output to [$out_xml]\n") if (VERB1());
            } else {
                pgm_exit(1,"ERROR: Invalid argument [$arg]! Try -?\n");
            }
        } else {
            $in_file = $arg;
            prt("Set input to [$in_file]\n") if (VERB1());
        }
        shift @av;
    }

    if ((length($in_file) ==  0) && $debug_on) {
        $in_file = $def_file;
        $out_xml = $def_out;
    }
    if (length($in_file) ==  0) {
        pgm_exit(1,"ERROR: No input files found in command!\n");
    }
    if (! -f $in_file) {
        pgm_exit(1,"ERROR: Unable to find in file [$in_file]! Check name, location...\n");
    }
}

# eof - xmlreline.pl
