#!/opt/perl/bin/perl
use strict;
use warnings;
use Cwd;
use Getopt::Long;
use File::Basename;
use GPS::StrUtil;
my $help;
my $here = cwd();
my $sitelog="$ENV{GPS_HOME_DIR}/site-logs/blank.log";
my @SITELOGS;
my $site;
my $dir = "$ENV{GPS_HOME_DIR}";
my $agency = "unavco";
my ($keyword,$keyword1,$keyword2);
my $kfield='';
my $ksection='';
my $printsection = 1;
my $printfield =1;
my $print_ant_graph =0;
my (@header,@sections,@subsections,@fields); 
my $progname = basename ($0);
my $out;
my $nargs=@ARGV;
my $and=1;
my (@KEYS,@RESULT) ;
my $logdir = "$dir/site-logs/$agency-logs/";

#define usage
my $usage= qq {
Usage: 
	$progname [-i site.log| -s site -a agency] -section section 
	          [-k keyword1,keyword2,..] [-f field1,field2,...]
		  [-help]
};
#Get command line options
GetOptions( "i=s"    	=> \$sitelog ,
            "s=s"    	=> \$site ,
            "a=s"	    => \$agency,
            "k:s"    	=> \$keyword,
            "field:s"  	=> \$kfield,
	        "section=s"	=> \$ksection,
	        "graph"     => \$print_ant_graph,
            "help"     	=> \$help
           ) || die "error in options";    
if ( ! $nargs || $help ) { print "$usage\n"; exit; };

die "$usage\n" if ! $ksection ;
@KEYS = split(/,/,join(',',lc($ksection)));
$kfield = lc($kfield) if $kfield;
#print "$keyword1 $keyword2 : $#KEYS @KEYS\n";
$logdir = "$dir/site-logs/$agency-logs/";
$site = lc($site);
if ( $site =~ /\w{4}/ ) {
	opendir (DIR, $logdir);
	@SITELOGS = reverse sort grep  /$site/, readdir(DIR);
	$sitelog = $SITELOGS[0];
};

open ( LOG,"< $logdir/$sitelog") || die "can\'t open $site sitelog \n";

my @lines=<LOG>;
my $sn=-1;
my $ssn=0;
my $sssn=0;
my ($section,$subsection,$field,$value);
my %SECTIONS;
my $flag=0;
my @l=@lines;
my @LINES;
foreach my $line (@l ){
		chomp($line);
		$line =~ /^\s*$/;
		$line =~ /^\n/;
		last if is_section($line);
		push(@LINES,sprintf ("%s\n",$line));
		push(@header,$line) ;
		shift @lines;
				
};
my $multiline=0;
my $antgraphs=0;
my $has_colon=0;
my @s=();
foreach my $line ( @lines ) {
		next if $line =~ /^\s*$/;  #skip blank lines
		next if $line =~ /^\n/;   #skip new lines
		chomp($line);
		if ( $antgraphs ) {
			my $l = sprintf ("%s\n",$line);
			push(@LINES,$l);
			push(@{$SECTIONS{antgraph}},$l);
		}elsif ( is_section(fix_line($line)) ) {
			my @tmp = split(' ',$line);
			$sn = $tmp[0]; 
			$sn =~ s/\.//;
			shift @tmp;
			$section = join " ",@tmp;
			$section =~ s/\s+/ /g;
			push(@sections,$section);
			push(@{$SECTIONS{"$sn."}},($section," "));
			push(@LINES,sprintf ( "%d.  %s\n",$sn,$section));
		}elsif ( is_sub_section(fix_line($line)) ) {
			my @tmp = split(' ',$line);
			@s = split(/\./,shift @tmp);
			if ( $#s > 1 ) { ($sn,$ssn,$sssn) = @s } else { ($sn,$ssn) = @s };
			#$ssn =~ s/\./_/;
			$line = join " ",@tmp;
			my $value;
			($subsection,$value) = split (/:/,$line);
			#@tmp = split(/:/,$line);
			#$subsection = shift @tmp if @tmp;
			#$subsection =~ s/\s+/ /g if @tmp;
			#$line = join " ",@tmp if @tmp;
			push(@subsections,$subsection);
			if ( $#s > 1 ) {
				push(@LINES , sprintf ( "%d.%s.%s %s :%s\n",$sn,$ssn,$sssn,$subsection,$value) ); 
				push(@{$SECTIONS{"$sn.$ssn.$sssn"}},($subsection,$value));
			} else {
				push(@LINES,sprintf ( "%d.%s %s :%s\n",$sn,$ssn,$subsection,$value) );
				push(@{$SECTIONS{"$sn.$ssn"}},($subsection,$value));
			}; 
			#print "$ssn\t$subsection\n" 
		} elsif ( is_multiline(fix_line($line)) ) {
			$field = join ( " " ,split(/:/,$line) );
			$field = trim($field);
			push(@LINES,sprintf ("%32s %s\n",":",$field));
		} else {
			#next if $antgraphs ;
			$value='';
			my @tmp = split(/\s+:\s+/,$line);
			$field = trim(shift @tmp);
			#$field =~ s/^\s+/ /;
			$antgraphs = ( $field =~ /(Antenna Graphics with Dimensions|insert text graphic from file antenna\.gra)/ );
			if ( ! $antgraphs ) {
			   $has_colon = has_colon ( fix_line($line) ); 		
			   $value = trim(join " ",@tmp);
			
			  if ( $has_colon ) {
				push( @LINES ,sprintf ("      %-24s : %s\n",$field,$value) );
			  } else {
				push( @LINES ,sprintf ("      %-24s : \n",$field) );
			  };
			  if ( $#s > 1 ) {
				push(@{$SECTIONS{"$sn.$ssn.$sssn"}},($field,$value))
			  } elsif ( $#s == 1 )  {
				push(@{$SECTIONS{"$sn.$ssn"}},($field,$value))
			  } else {
				push(@{$SECTIONS{"$sn."}},($field,$value))
			  };
			
			  push(@fields,$field) if $field;
			};
		};
#		print "$sn $section\n" if is_section($line) ;

};


my %SOURCE ;
my $criteria= sprintf("(%s)",join('|',@KEYS));
my @keyset = keys %SECTIONS;
my (@tmp,@result);

#print "criteria $criteria\n";
#do the search
foreach my $key ( @keyset ) {
	if ( lc( ${$SECTIONS{$key}}[0] ) =~ /$criteria/ )  {
		#push ( @{$SOURCE{$key}}, @{$SECTIONS{$key}}) if ! defined @{$SOURCE{$key}};
		$SOURCE{$key}= $key if ! defined $SOURCE{$key};
	};
};


@keyset= sort grep ! /\.x/, keys %SOURCE; 

	
@KEYS = split(/,/,join(',',lc($kfield))) ;
if ( $kfield ) { $printsection = 0; $printfield = 1 };
#print (map { "$_\n" } @keyset );
#exit;

foreach my $key ( @keyset ) {
	if ( $key eq "antgraph" ) {
	  my @s =  @{$SECTIONS{$key}};
	  chomp(@s);
          push @result,@s if $print_ant_graph;	
	} else {
	  #my @s = map { lc( $_ ) } @{$SECTIONS{$key}};
	  my @s =  @{$SECTIONS{$key}};
	  my $i = 0;
	  push( @result, "$key : ${$SECTIONS{$key}}[$i] : ${$SECTIONS{$key}}[$i+1]") ;
	  $i+=2;
	  while ( $i < $#s ) {
		if ( $printsection ) {
			push( @result, "$key : ${$SECTIONS{$key}}[$i] : ${$SECTIONS{$key}}[$i+1]") ;
		} elsif ( $printfield ) {
		   if ( founded( $s[$i] , @KEYS ) ) { 
			push( @result, "$key : ${$SECTIONS{$key}}[$i] : ${$SECTIONS{$key}}[$i+1]") ;
		   };
		};
		$i+=2;
	  };
        }
}; 
	
print (map { "$_\n" } @result );

#my $outdir = "$here/logdefs";
#mkdir ($outdir) if ! -e $outdir;
#print "@sections\n";
#open ( HEADER , "> $outdir/blank.header " );
#open ( SEC , "> $outdir/blank.sections " );
#open ( SSEC , "> $outdir/blank.subsections " );
#open ( FIELDS , "> $outdir/blank.fields " );

#print HEADER (map { "$_\n" } @header );
#print SEC (map { "$_\n" } @sections );
#print SSEC (map { "$_\n" } @subsections );
#print FIELDS (map { "$_\n" } @fields );

#===========================================================================#
sub founded {
	my $line = lc(shift);
	my @keys = @_;
	my $words = join(' ',split (/ /,trim($line) ));
	my $count = 0;
        my $criteria= sprintf("(%s)",join('|',@KEYS));
	foreach my $k ( @keys ) {
		$count +=  ($words =~ /$criteria/) ;	
	};
	#print $count,scalar @keys if scalar @keys == 2;
	if (  $count == scalar @keys  ) { return ( 1 ) } else {return (0)};
}
sub is_section {
	my $line = shift;
	$line =~ s/\*/\\\*/g;  		#fix asterisks
	return ( $line =~ /^(\d+\.\s+)/ )
}
sub is_sub_section {
	my $line = shift;
#match 			begins with	8.1	  or     8.1.1  or   8.x  or  8.1.x
	return ( $line =~ /^(\d+\.\d+|\d+(\.\d+)+|\d+\.x|\d+\.\d+\.x)/ ) 
}
sub is_multiline {
	my $line = shift;
	my @mlfields = ( 'Distance\/activity',
			 'Additional Information',
			 'Notes',
		  	 'Agency',
			 'Mailing Address',
			 'Marker Description'
		  );
        #$line = fix_line($line) ;
	my $founded=0;
	foreach my $f ( @mlfields ) {
	   $founded = grep /$f/,$line;
	}
	#my $founded = grep /$line/, @mlfields ;
	my @tmp = split(/:/,$line);
	my $field = shift @tmp;
	my $value = join " ",@tmp;
	my $parsed = ! $field && $value ;
	return ($founded || $parsed )
}

sub has_colon { # for long fields
	my $line = shift; #fix_line(shift);
	my @nocolon = ( 'Approximate Position (ITRF)',
			'Differential Components from GNSS Marker to the tied monument (ITRS)',
			'Primary Contact',
			'Secondary Contact',
			'Hardcopy on File',
			'Antenna Graphics with Dimensions'
                      );
	
	my $colon = grep { $_ =~ /$line/ } @nocolon;

	return( ! $colon)

}
sub fix_line2 {
	my $line = shift;
	$line = trim($line);
        #escape non word characters
	
	#$line =~ s/(\(|\)|
	#            \?|\!|
#		    \*|\.|
#		    \^|\\|
#		    \+|\/|
#		    \[|\]|
#		    \{|\}
#		    )
        $line=~ s/([^\w: ])/\\$1/g;
	return  $line 
}
sub fix_line {
	my $line = shift;
  #      return escape($line);
	$line =~ s/^\s+// ; 		#no  initial blanks
	$line =~ s/\s+$// ; 		#no trail blanks
	$line =~ s/\(/\\\(/g ;     #fix parenthesis
	$line =~ s/\)/\\\)/g ; 			
	$line =~ s/\?/\\\?/g;  		#fix ? 
	$line =~ s/\*/\\\*/g;  		#fix asterisks
	$line =~ s/\+/\\\+/g;  		#fix plus 
	$line =~ s/\//\\\//g;  		#fix slash
	$line =~ s/\[/\\\[/g ;     #fix parenthesis
	$line =~ s/\]/\\\]/g ; 			
	return ( $line )
}

sub trim_line {
	my $line = shift;
	$line =~ s/^\s+// ; 		#no  initial blanks
	$line =~ s/\s+$// ; 		#no trail blanks
	return ( $line )
}

sub trim_ {
	my $line = shift;
	$line =~ s/^\s+// ; 		#no  initial blanks
	$line =~ s/\s+$// ; 		#no trail blanks
	return  $line 
}
