#! perl -lw

# on 2025-12-15 2PM EST, AFDBOX changed from "Synopsis" to "What has changed" followed by "Key messages"
sub readchunk {
    # note @lines is a global variable
    while(<>){
        chomp;
        if(/(.*?)\s*\&\&\s*$/){
            #sometimes && is at the end of a line with text
            if($1 ne ""){
                push@lines,$_;
            }
            last;
        }
        push@lines,$_;
    }
}

sub skipblanklines {
    while(<>){chomp;last unless /^\s*$/;}
}

while(<>){
    chomp;
    if(/^AFD/){
        push@header,$_;
        last;
    }}
die unless @header;
&skipblanklines;

# traditionally there was a blank line after AFDBOX, but we make things more compact
# push@header,'';

chomp;
push@header,$_;
die unless /^Area Forecast Discussion/;
# Area Forecast Discussion...DELAYED
while(<>){
    chomp;
    last if/^\s*$/;
    push@header,$_;
}

for(;;){
    undef @lines;
    &readchunk;
    die unless@lines;
    $good=0;
    for(@lines){
        if(/^\.(SYNOPSIS|KEY MESSAGES)\.*/i){
            $good=1;
        }
    }
    last if $good;
}

while($lines[$#lines] eq ''){
    pop@lines;
}
for(@header){
    print;
}
for(@lines){
    print;
}
