#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sigtypes.h"
#include "user_lib.h"
#include "UalfTokenize.h"


int main( int argc_a, char** argv_a )
{
  int   icount = 0;
  int   lerror = false;
  int   itime_inc = 15;
  UINT2 lfirsttime = true;
  UINT2 lwrite_header = true;
  UINT2 lfound_lastline = false; 
  char  *sptr;
  char  sline[300], sline_copy[300];
  char  sualf_filename_out[50];
  char  *sualf_filename_in = NULL;
  FILE  *stream_in, *stream_out;
  struct ymds_time cur_ymds_time;
  struct ymds_time next_ymds_time;
  struct ymds_time last_ymds_time;
  UalfTokenize ualf_tok;


  //Parse args looking for filename and a time increment
  for (int ii = 1; ii < argc_a; ii++)
  {
    if (argv_a[ii][0] == '-')
    {
      if (0 == strcmp("time_inc", &argv_a[ii][1]))
      {
        ii++;
        if (ii >= argc_a)
          lerror = true;
        else 
        {
          itime_inc = atoi(argv_a[ii]);
          if ((itime_inc < 5) || (itime_inc > 60))
            lerror = true;
        }
      }
      else
        lerror = true;
    }
    else
    {
      if( sualf_filename_in )
        lerror = true ;
      sualf_filename_in = argv_a[ii] ;
    }
  }

  if (sualf_filename_in == NULL)
    lerror = true;

  if (lerror)
  {
     printf( "Usage: SimulateUalf [-time_inc 5 - 60 seconds] ualf_file\n" ) ;
     exit(EXIT_FAILURE);
  }

  //Simulate UALF data input by breaking up a UALF data file into
  //seperate files.  The itime_inc value will specifiy how much 
  //ualf data the seperate files will conntain.
  while(1)
  {
    lwrite_header = true;
    stream_in = fopen(sualf_filename_in, "r" );
    if (stream_in == NULL)
    {
      printf("Can't open ualf file %s for reading\n", sualf_filename_in);
      exit(EXIT_FAILURE);
    }

    sprintf(sualf_filename_out, "/usr/iris_data/ualf/out%d.ualf", icount++);
    stream_out = fopen(sualf_filename_out, "w" );
    if (stream_out == NULL) 
    {
      printf("Can't open ualf file %s for writting\n", sualf_filename_out);
      exit(EXIT_FAILURE);
    }

    while(1)
    {
      UINT2 ltokenized_passed = false;
      sptr = fgets(sline, sizeof(sline), stream_in);
      if (sptr == NULL)
      {
        //End of file
//        printf("\n\n");
        fclose(stream_in);
        fclose(stream_out);
        if (!lfound_lastline)
          last_ymds_time = ualf_tok.getYmdsTime();
        lfound_lastline = true;
        break;
      }
      strcpy(sline_copy, sline);

      if (ualf_tok.tokenize(sline))
      {
        if (ualf_tok.getTokCnt() == ualf_tok.getMaxParams())
          ltokenized_passed = true;
        else
        {
          printf("Bad number of tokenized params. Tokenized %d should have been %d\n",
            ualf_tok.getTokCnt(), ualf_tok.getMaxParams());
        }
      }
      else
        printf("Tokenize problem on line <%s>\n", sline_copy);


      if (ltokenized_passed)
      {
        if (lwrite_header)
        {
          if (lfirsttime)
          {
            //Need to round seconds to the nearest minute
            cur_ymds_time = ualf_tok.getYmdsTime();
            cur_ymds_time.isec = iPackTime(ualf_tok.getHour(),ualf_tok.getMinute(), 0);
            //Set span of ualf data we are looking for
            next_ymds_time = cur_ymds_time; 
            last_ymds_time = cur_ymds_time; 
            //For now set last line ahead 1 hour until we
            //can make one pass thru the file and get the
            //last line
            ymds_add_seconds(3600, &last_ymds_time); 
          }
          else
            ymds_add_seconds(itime_inc, &cur_ymds_time); 

 
          UINT2 iday, ihour, imin, isec;
          ymds_add_seconds(itime_inc, &next_ymds_time); 
          unpacktime(cur_ymds_time.isec, &iday, &ihour, &imin, &isec);
          fprintf(stream_out,"9	%d	%d	%d	%d	%d	%d\n",
            cur_ymds_time.iyear, cur_ymds_time.imon, cur_ymds_time.iday, ihour, imin, isec);
//          printf("9	%d	%d	%d	%d	%d	%d\n",
//            cur_ymds_time.iyear, cur_ymds_time.imon, cur_ymds_time.iday, ihour, imin, isec);
        }

        struct ymds_time ualf_file_ymds_time;
        ualf_file_ymds_time = ualf_tok.getYmdsTime();
        if (last_ymds_time.isec > cur_ymds_time.isec)
        {
          if ((ualf_file_ymds_time.isec >= cur_ymds_time.isec) && 
              (ualf_file_ymds_time.isec < next_ymds_time.isec))
          {
             fprintf(stream_out,"%s", sline_copy);
//             printf("%s", sline_copy);
          }
        }
        else 
        {
          printf("Done\n");
          exit(EXIT_SUCCESS);
        }
        lfirsttime = false;
        lwrite_header = false;
      }
    }
  }
}

