#!/usr/bin/perl
# -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
use warnings;
use strict;
use utf8;
BEGIN {
   $| = 1;
   binmode(STDIN, ':encoding(UTF-8)');
   binmode(STDOUT, ':encoding(UTF-8)');
}
use open qw( :encoding(UTF-8) :std );

use Getopt::Long;
Getopt::Long::Configure('no_ignore_case');
my %opts = ();
GetOptions(\%opts, ('help|?'));

sub print_help {
   print <<'XOUT';
Usage: cg-untrace

Pipe a CG stream through this to remove all deleted readings and tracing tags.

XOUT
}

if (defined $opts{'help'}) {
   print_help();
   exit(0);
}

while (<STDIN>) {
   # Discard deleted readings
   if (/^;/) {
      next;
   }
   # On readings only...
   if (/^\s+"/) {
      s/ (ID|R):/ x$1:/g;
      # ...strip trace tags
      while (s/ [-A-Z]+:[^"\s\n]+//g) {}
      while (s/ (ADD|REM|SET)RELATIONS?\(\S+\):[^"\s\n]+//g) {}
      s/ x(ID|R):/ $1:/g;
   }
   print;
}
