/* **********************************************************************
 * *                                                                    *
 * *  Example program to convert versions of IRIS product files         *
 * *                                                                    *
 * **********************************************************************
 * File: utils/examples/version.C
 *
 *               COPYRIGHT (c) 1997, 1999, 2001, 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.
 */

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

#include "sigtypes.h"
#include "dsp.h"
#include "setup.h"
#include "headers.h"
#include "iris_task.h"
#include "ingest.h"
#include "product.h"
#include "user_lib.h"
#include "misc_lib.h"

int main( int argc_a, char **argv_a )
{
  MESSAGE istatus;
  union generic_product *buf_ptr;
  char *iptr;
  int answer, count, operation;
  FILE *in_file, *out_file;

/*
  if (argc_a != 2)
  {
    printf("usage: convert <input_file>\n\n");
    exit(0);
  }
*/

  in_file = fopen( argv_a[1], "r+b");
  if (in_file == NULL)
  {
    printf("unable to open input file\n\n");
    exit(EXIT_FAILURE);
  }

  buf_ptr = (generic_product *)malloc(10000000);
  if (buf_ptr == NULL)
  {
    printf("null ptr\n");
    exit(EXIT_FAILURE);
  }

  iptr = (char*)buf_ptr;
  count = 0;
  while(1)
  {
    answer = fread(iptr, 1, sizeof(unsigned char), in_file);
    if (answer <= 0)  break;
    count++;
    iptr++;
  }

  fclose(in_file);

  operation = VCV_FROM_V2 | VCV_TO_V1 | VCV_DIRECT;
  istatus = vcv_product(buf_ptr, NULL, count, operation );
  if (istatus == SS_NORMAL)
    {
      out_file = fopen("iris.out", "w+b");
      fwrite(buf_ptr, count, sizeof(unsigned char), out_file);
      fclose(out_file);
    }
  else
    {
      sig_signal( istatus );
    }

  exit(EXIT_SUCCESS) ;
}

