#!/usr/bin/perl
#
# vdl2grep - a simple script for grepping dumpvdl2 log files.
# Prints whole VDL2 messages containing (or not containing) given text string.
#
# Copyright (c) 2017-2026 Tomasz Lemiech <szpajder@gmail.com>

use Getopt::Std;

my %opts;

use constant {
	DIR_UNKNOWN => 0,
	DIR_GND2AIR => 1,
	DIR_AIR2GND => 2,
	DIR_AIR2AIR => 3,
	DIR_GND2GND => 4
};

sub usage {
	print STDERR "$0 - a simple dumpvdl2 log file grepper\n\n";
	print STDERR "Usage: $0 [options] <search_regex> [<file1> ...]\n";
	print STDERR <<EOF

Options:
	-d		Search only downlinks
	-i		Case-insensitive search
	-u		Search only uplinks
	-v		Invert search
EOF
;
	exit 1;
}

sub print_if_matches {
	my($msg, $dir, $print, $dont_print) = @_;
	@$msg == 0 and return;
	$dir == DIR_UNKNOWN and return;		# not a dumpvdl2 log?
	$opts{u} and $dir != DIR_GND2AIR and return;
	$opts{d} and $dir != DIR_AIR2GND and return;
	if(($opts{v} && !$dont_print) || (!$opts{v} && $print)) {
		print join('', @$msg);
	}
}

sub grep_filehandle {
	my($regex, $fh) = @_;
	my($print, $dont_print) = (0,0);
	my $dir = DIR_UNKNOWN;
	my @msg = ();
	while(<$fh>) {
		if(/^\[\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(?:\.\d{3})? / && @msg > 0) {
			print_if_matches(\@msg, $dir, $print, $dont_print);
			$print = $dont_print = 0; @msg = (); $dir = DIR_UNKNOWN;
		}
		if($dir == DIR_UNKNOWN && /^[0-9A-F]{6} \((.+), .*?\).* -> [0-9A-F]{6} \((.+)\)/) {
			if($1 eq "Aircraft" && $2 eq "Ground station") {
				$dir = DIR_AIR2GND;
			} elsif($1 eq "Ground station" && $2 eq "Aircraft") {
				$dir = DIR_GND2AIR;
			}
		}
		if(!$print && !$dont_print && /$regex/) {
			if($opts{v}) {
				$dont_print = 1;
			} else {
				$print = 1;
			}
		}
		push @msg, $_;
	}
	print_if_matches(\@msg, $dir, $print, $dont_print);
}


getopts('diuv', \%opts) or usage;
$opts{d} and $opts{u} and die "Options: -d and -u are exclusive\n";

@ARGV < 1 and usage;

my $regex = $opts{i} ? qr/$ARGV[0]/i : qr/$ARGV[0]/;
shift @ARGV;
if(@ARGV) {
	my $fh;
	for my $fname(@ARGV) {
		if($fname =~ /\.(gz|bz2|xz|lzma|zst|zip)$/i) {
			require IO::Uncompress::AnyUncompress;
			unless($fh = IO::Uncompress::AnyUncompress->new($fname)) {
				print STDERR "uncompress($fname): failed to initialize descompressor\n";
				next;
			}
		} else {
			unless(open($fh, "<$fname")) {
				print STDERR "open($fname): $!\n";
				next;
			}
		}
		grep_filehandle($regex, $fh);
		close($fh);
	}
} else {
	grep_filehandle($regex, *STDIN);
}
