#!/usr/bin/perl
#
#------------------------------------------------------
#
# This is attach_ffs.pl
# It attaches an Option 2  bulletin flag field seperator 
# line and WMO header (if given) then copies it to the 
# same filename with ".with_ffs" appended.
#
# Input file <header>
# where:
#    file is the full path and name of the file to send.
#    header is an optional WMO header (in quotes) 
#      (i.e. "JTNC01 KWBC")
#
#   Author: Paula Freeman
#
#   Modified by: J. Maloney, can attach a WMO header
#                            as well...
#
#------------------------------------------------------


$NArgs = @ARGV;
if ($NArgs < 1)   {
   usage ();
   exit;
}

#
#  Get input
#


($Filename, $Header, $Awipid) = @ARGV;
print "Filename is $Filename\n";
print "Header is $Header\n" if $Header;
print "AWIPS ID is $Awipid\n" if $Awipid;

#
#  Create the file for TOC
#

   make_file ();
   
   
sub usage () {
   print "Usage: $0  <input path> [<WMO Header> (in quotes)] [<AWIPS ID> (in quotes)]\n";
}

sub make_file  {

  
#  Count the number of bytes in the file to put into
#  the Bulletin Flag Field Seperator. 
 
   $ByteCount = `wc -c $Filename | cut -c1-8`;
   $Header = "$Header\n" if $Header;
   $ByteCount += length($Header);
   $Awipid = "$Awipid\n" if $Awipid;
   $ByteCount += length($Awipid);
  
   $BulletinFlagFieldSep = sprintf( "****%10.10d****", $ByteCount);
   $Output_Filename = $Filename . ".with_ffs";
   
   open(OUTFILE, ">$Output_Filename") or die "Cannot open $Output_Filename for output.";
   print OUTFILE "$BulletinFlagFieldSep\n$Header$Awipid";

   open (INFILE, $Filename) or die "Cannot open $Filename";

   while ($rec=<INFILE>) {
      print OUTFILE $rec;
   }

   close INFILE;
   close OUTFILE;
      
   print "$Filename -> $Output_Filename\n";
}


