#!/usr/bin/env perl
use warnings;
use utf8;
BEGIN { $| = 1; }

$ARG0 =$ARGV[0];
if ($ARG0 && $ARG0 =~ /^--bfn=0/) {$bfn =0;} else {$bfn =1;}

while (<STDIN>) {

 s/^\$¶.*//;
  if (/<norole>/) {s/ §[A-Z_=\/\-]+//g;} # transparent nouns like 'a type of'
  elsif (/<norole-ext>/) {s/ §[A-Z_=\/\-]+/ §EXT/g;} # transparent nouns like 'a type of'
  s/(§2[A-Z_\-]+)=/$1 §§2/g; # split ordinary and BFN-fine-grained roles
  s/(§[A-Z_\-]+)=/$1 §§/g; # split ordinary and BFN-fine-grained roles

  s/ &(R|P<|<ACC) / /g;
  if (s/ &headstop / /g) {s/ ([A-Z]+) / <headstop> /g;}
  s/\t *\[.*\t/\t/; # keep only one (the last) syn reading
  s/\[par-l\]/[(]/; # new?!
  s/\[par-r\]/[)]/;
  s/\@PAR-[LR]/\@PU/g; # did not have L?!
  
  s/ <[A-PR-Z][A-XZ]+:[^\+>]*?>+ / /g; # cave single-letter and Q, SYN
  s/ <[A-PR-Z][A-XZ]+:[^\+>]*?>+ / /g; # cave single-letter and Q, SYN
#  s/<TMP+:.*?> //g; # implied in the previous line
  s/ <r:.*?>//g; # remove role potentiality markers on verbs, for corpus work
  if (! $bfn) {
    s/ §§[A-Z_\-]+//; # remove BFN roles
    s/ <fnb:.*?>//; # remove BFN frames
  }
  s/ \'.*?\' / /; # remove Eo translations
  s/ <\+.*?>//g;
  s/ <[^>Q]*?\+>//g; # cave greedy .* until next + tag
#  print "--$_";
  s/ <vt?a\+.*?>//g;
  s/ <som\^.*?>//g;
  s/ <([a-zæøå=]+\^)?[vx][iertqd]?[tpaqkd]?(-[a-zæøå=]+)?>//g;
  s/ <vdqk>//g;
  s/ <((advl|np|icl)-(close|fs|icl|long)|afterpar|art-long|behave|cjtsem|exist|firstcjtsubj|lex|ma|ment|mod[123]|nd-suf|relat|sentstart|subj-icl|verb|uc|vreci|D[a-z].*?)>//g; # <art-long> is implied by <cross-pp> ART
  s/ <(-comp|comppos:.*?)>//g;
  s/ <vH&?>//g;
  while (s/ <(ex-transparent|cjtsem)>//) {
    $move_secondary = $1;
    s/( [A-Z]+ )/ <%%%$move_secondary>$1/;
  }
  s/<%%%/</g;
  #    s/ <[a-zA-Z]+:.*?>//g;
#    s/ <D[a-z]+>//g;
    s/ <[^>:Q]*?\+[^>]*?>//g; # cave <v:af^vtp> og <Q+/->
    s/ <[^>:]*?\^[^>]*?>//g;
  s/( %[^ ]+)( .*§[^ ]+)( £)/$2$1$3/; # reordering % tags from left of role block (R:sd-... §...) to left of onto block (£)
  #  s/§/§/;
  if (s/(\t.*?)\t.*? (R:sd.*?) \#[0-9]->[0-9].*/$1/) {
    $line =$_;
    @secondline_tags = split / /, $2;
    foreach (@secondline_tags) {
      if (/^((R:sd|§).*)$/) {
	$tag =$1; $tagstart =$2;
	if ($line !~ / $tag /) {
	  $line =~ s/(( $tagstart).*? )/$1$tag /;
	}
      }
    }
    $_ =$line;
  }
 while (s/( <[^ >]+>)(.*)\1/$1$2/) {}

  print;
}
