#!/bin/perl -w

# Split up GTS SYNOP (AAXX, BBXX) and BUOY messages
#
# Input: string of messages, as received over GTS
# Output: array of messages, each with own header
#
# Note: short messages (<=8 chars) aren't included in the output; this
# means that the terminating "NNNN" is also not returned.
# Note: junk characters are now allowed
#
# See-also: decode_ships.pl, for example, which uses this code
#
# Fixes:
#    1998/12/15: add DATE="" in BBXX processing to prevent adding the date
#                when we shouldn't. Add a few comments. [wmc]
#
#   Copyright W. M. Connolley 1998, 2003. wmc@bas.ac.uk
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details, available from:
#
#   http://www.gnu.org/licenses/gpl.txt.

sub split_gts_synop {
  my($text)=@_;
  my($line,@line,$M,@Messages,$MESSAGES,$TYPE,$Etc,$DATE);

# Split up the input on "NNNN" and add an initial <CR> just in case the very first
# text is AAXX or such: this will only ever occur in tests.
  @text=split(/NNNN/,"\n".$text);

  if ($DEBUG) { print "Got ",$#text+1," messages\n" };

  while ($line=shift @text) {
    if ($DEBUG>2) { print "Line: $line\n---\n" };

# Only interested if the line is one of our types
    if ($line=~/\n(AAXX|BBXX|ZZYY)/) {

# Save the TYPE, and the rest of the message in Etc
      $TYPE=$1; $Etc=$';
      if ($DEBUG>2) { print "Type: $TYPE\n" };

# AAXX's often come in groups, with the date shared betweeen them. Need to split it off the first
      if ($TYPE eq "AAXX") {

# Split up Etc so we can extract the DATE
        @line=split(" ",$Etc);
        $DATE=shift @line;

# Check that the date looks vaguely OK
        if (length($DATE) != 5) { 
# otherwise junk the line
          $line=" "
        } else { 
# If it is OK, then reform line with DATE removed
          $line=join(" ",@line) 
        };   
        $DATE.=" "; $TYPE.=" ";

# BBXX'S, OTOH, come in groups but don't share the date
      } elsif ($TYPE eq "BBXX") {

        $TYPE.=" ";
        $line=$Etc;
        $DATE="";

      } else {

        $line="$TYPE $Etc";
        $DATE=""; $TYPE="";

      };

# Now, split up into the difference Messages
      @Messages=split(/=/,$line);

# Loop over them, adding TYPE and DATE as needed in order to make them self-sufficient
      foreach $M (@Messages) {
        $M=~s/^\s*//;
        if ( (length($M) > 8) && $M !~ /[^\s\w\/\.]/ ) {
          push(@MESSAGES,$TYPE.$DATE.$M)
        }
      };

    };
  };

  if ($DEBUG>2) { print "Returning messages:\n\n",join("\n---\n",@MESSAGES),"\n---\n" };

  return @MESSAGES;

};

1;
