/* ************************************************************
 * *                                                          *
 * *  Input pipe to change the data type of an IRIS  product  *
 * *                                                          *
 * ************************************************************
 * File: utils/in-pipes/ChDataType.C
 *
 *                       COPYRIGHT (c) 2003  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 IRIS product in the standard input
 * and writes a modified product out the standard output.  The modification
 * consists of changing the data type.
 *
 * 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 "headers.h"
#include "product.h"


/* Local function prototypes
 */
static void ChangeProductHeader
(struct product_hdr *phdr_a );

static void ProcessCommandLine
(int iArgc_a, 
 char *sArgv_a[],
 char sFilename_a[FILENAME_SIZE] );

static void Usage( void );

static void Lprintf
( const char *sString_a );

/* ===============================================================
 * Main program starts here.
 */
int main(int iArgc, char *sArgv[])
{
  char sFileName[FILENAME_SIZE];
  SINT4 iSize;
  struct product_hdr PHeader;
#define BUFFER_SIZE (5120)
  UINT1 iBuffer[BUFFER_SIZE];
  char log_str[200];

  setlocale( LC_ALL, "" ) ;

  /* Process the command line arguments */
  ProcessCommandLine( iArgc, sArgv, sFileName );

  /* Process the product header */
  iSize = fread( &PHeader, sizeof(char), sizeof(struct product_hdr), stdin); 
  if (iSize == sizeof(struct product_hdr)) 
    {
      ChangeProductHeader( &PHeader );
      fwrite( &PHeader, 1, iSize, stdout);  
    }
  else
    {
      sprintf(log_str, "File size %d smaller than product header.\n", iSize );
      Lprintf(log_str);
      fwrite( &PHeader, 1, iSize, stdout);  
      exit( EXIT_FAILURE );
    }

  iSize = fread(iBuffer, sizeof(char), BUFFER_SIZE, stdin); 
  while( iSize > 0 )
    {
      fwrite(iBuffer, 1, iSize, stdout);  
      iSize = fread(iBuffer, sizeof(char), BUFFER_SIZE, stdin); 
    }

  exit( EXIT_SUCCESS );
}
/* ======================================
 * Change the data type and product name in the product header.
 */
static void ChangeProductHeader
(struct product_hdr *phdr_a )
{
  phdr_a->pcf.idata_out=DB_USER;
  memcpy( phdr_a->pcf.spname, "NEW_NAME    ", 12 );
}

/* ======================================================================
 * Function to process the command line arguments.
 */
static void ProcessCommandLine
(int iArgc_a, 
 char *sArgv_a[],
 char sFilename_a[FILENAME_SIZE] )
{

  int iArg;

  /* Parse command line options
   */
  for( iArg=1 ; iArg<iArgc_a ; iArg++ )
    {
      const char *sArg = sArgv_a[iArg];
      /* fprintf( stderr, "Processing %d: '%s'\n", iArg, sArg ); */

      if (sArg[0] == '-')
	{
	  if (0 == strncmp( sArg, "-h", 2 ))
	    {
	      Usage();
	    }
	}
      else
	strncpy( sFilename_a, sArg, FILENAME_SIZE-1 );
      
    }
  return;
}
/* ==================================================
 */
static void Usage( void )
{
  fprintf( stderr, "Options: -help\n" );
  fprintf( stderr, "         filename\n" );
  fprintf( stderr, "         (all other arguments ignored)\n" );
  exit(EXIT_FAILURE) ;
}

/* ===========================================
 * Print string to log and terminal, if desired.
 */
static void Lprintf
( const char *sString_a )
{
  fputs( sString_a, stderr );
}
