/* **************************************
 * *                                    *
 * *    Rainbow format viewer program   *
 * *                                    *
 * **************************************
 * File: utils/rainbow/rainbowview.C
 *
 *                         COPYRIGHT (c) 2002  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 take the filename of a rainbow file and prints out diagnostic
 * information.
 */

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

#include "sigtypes.h"

#include "user_lib.h"
#include "rainbow_h.h"

/* Local function prototypes
 */

static void ProcessCommandLine
(int iArgc, 
 char *sArgv[],
 struct view_options *pOptions_a );

static void Usage( void );

struct view_options
{
  char sPath[PATHNAME_SIZE];
  SINT4 iRayCount;     /* Only print this many rays */
  SINT4 iLineLength;   /* Line length to use for data listing */
};


/* ===============================================================
 * Main program starts here.
 */
int main(int iArgc, char *sArgv[])
{
  SINT4 iChannel, iSize;
  struct view_options Options;
  struct rainbow_info8 Checkup;
  struct convert_config CSetup;
  char *sData;
  MESSAGE iStatus;
  UINT1 lError;

  setlocale( LC_ALL, "" ) ;

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

  /* Open the file */
  iStatus = imapopen( Options.sPath, FALSE, (void**)&sData, &iSize, &iChannel );
  if (iStatus != SS_NORMAL)
    {
      sig_signal( iStatus );
      exit(EXIT_FAILURE);
    }

  /* Process the setup file, if any */
  LoadSetup( &CSetup );
  /* Override logging file details */
  CSetup.lLogToFile   = FALSE;
  CSetup.lLogToTerm   = TRUE;
  CSetup.lVerbose     = TRUE;
  CSetup.lRainbowView = TRUE;

  RainbowParse( sData, iSize, &Checkup, &CSetup );

  lError = RainbowWarnings( &Checkup, &CSetup );

  iStatus = imapclose( sData, iSize, iChannel );
  if (iStatus!=SS_NORMAL) sig_signal( iStatus );

  if (lError)
    exit( EXIT_FAILURE );
  else
    exit( EXIT_SUCCESS );
}

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

  int iArg;
  memset( pOptions_a, 0, sizeof(*pOptions_a) );
  pOptions_a->iLineLength = 80;

  /* 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, "-count:", 7 ))
	    {
	      char *sEnd;
	      pOptions_a->iRayCount = strtol( &sArg[7], &sEnd, 10 );
	    }
	  else if (0 == strncmp( sArg, "-h", 2 ))
	    {
	      Usage();
	      exit(EXIT_FAILURE) ;
	    }
	  else if (0 == strncmp( sArg, "-length:", 8 ))
	    {
	      char *sEnd;
	      pOptions_a->iLineLength = strtol( &sArg[8], &sEnd, 10 );
	      if (pOptions_a->iLineLength<30) pOptions_a->iLineLength=30;
	    }
	}
      else
	{
	  strncpy( pOptions_a->sPath, sArg, PATHNAME_SIZE-1);
	}
    }
  if (!pOptions_a->sPath[0])
    {
      printf( "rainbowview requires a pathname arg.\n" );
      exit(EXIT_FAILURE);
    }
  return;
}
/* ==================================================
 */
static void Usage( void )
{
  fprintf( stderr, "Options:\n" );
  fprintf( stderr, "         -help     (print this list and exit)\n" );
  //  fprintf( stderr, "         -length:N (max line length to use)\n" );
  fprintf( stderr, "         pathname\n" );
  fprintf( stderr, "         (all other arguments ignored)\n" );
}

