/* ***********************************************************************
 * *                                                                     *
 * *  Class to convert a Universal ASCII Lighting Format file to         *
 * *  an IRIS WARN product                                               *
 * *                                                                     *
 * ***********************************************************************
 * File: utils/ualf/UalfToIris.C
 *
 *                      COPYRIGHT (c) 2006  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

/* This program is a pipe which takes an input file defining 
 * Universal ASCII Lighting Format (UALF) data and converts
 * it into an IRIS WARN product containing that data.
 *
 * Note if you are modifying this program, and wish to insert a diagnostic
 * printf, be sure to write it to the error output.
 */

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "setup.h"
#include "headers.h"
#include "product.h"
#include "UalfConvert.h"

#define MAX_LINELENGTH (120)

/* ===============================================================
 * Main program starts here.
 */
int main(int iArgc, char *sArgv[])
{
  int         total_bytes;  
  struct      warn_product warnProduct;
  UalfConvert ualfConvert;

  setlocale( LC_ALL, "" ) ;

  memset( &warnProduct, 0, sizeof(struct warn_product));

  /* Process the command line arguments */
  ualfConvert.processCommandLine( iArgc, sArgv );

  /* Process the setup file,if any.  We're using:
  **  /usr/sigmet/config/UalfToIris.conf */
  ualfConvert.loadSetup();
    
  /* Read in the header */
  ualfConvert.initializeProdHeader(&warnProduct);

  /* now loop through the file reading and parsing lines from the standard input */
  UINT1  lfirstpass;
  int    iLine = 1;
  char     sLog[200], sLine[MAX_LINELENGTH], *sptr;

  /* now loop through the file reading and parsing lines from the standard input */
  iLine = 1;
  lfirstpass = TRUE;
  sptr = fgets( sLine, MAX_LINELENGTH, stdin );
  while (NULL != sptr)
  {
    UINT1 lerror;
    if (ualfConvert.isVerbose())
    {
      sprintf( sLog, "Line %d: %s", iLine, sptr );
      ualfConvert.Lprintf( sLog);
    }
    lerror = ualfConvert.lProcessAsciiLine( sptr, &warnProduct, lfirstpass);
    if (lerror)
    {
      sprintf( sLog, "Syntax error on ASCII file line %d\n", iLine );
      ualfConvert.Lprintf( sLog );
    }
    iLine +=1;
    sptr = fgets( sLine, MAX_LINELENGTH, stdin );
    lfirstpass = FALSE;
  }

  total_bytes = PRODUCT_HDR_SIZE + sizeof(struct protect_setup) +
    warnProduct.Header.end.iresults_count*sizeof(struct warning_results);

  warnProduct.Header.hdr.ibytes = total_bytes; 

  /* Write out the results to the standard output */
  fwrite( &warnProduct, sizeof(struct warn_product), 1, stdout );

  if (ualfConvert.isVerbose())
    {
      sprintf( sLog, "Wrote %d bytes, %d warnings\n", total_bytes,
	       warnProduct.Header.end.iresults_count );
      ualfConvert.Lprintf( sLog);
    }
  exit( EXIT_SUCCESS );
}
