#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


/* globale Arbeitsvariablen */

#include "cube.h"


void main(argc,argv)
int argc;
char **argv;
{
  double		tx,t,T;
  long			j,ch;
  long long		nsamp,i,k;
  long long		*nsample;
  int			**data,amp;
  long			tmks,gps_t;
  long			sec,mksec,x1,x2;
  long long		tsl,ts0l,txl;


  char  		buf[1000];
  int			code;
  int			ampx[100];
extern int optind, opterr, optopt;
   int c,errflg; 
   time_t start_time;
   struct tm *tss;

   errflg=0;
   putenv ("TZ=GMT");
   tzset; 


  if (!(nsample = malloc (MXSMP * sizeof(long long))))   { fprintf(stderr, "%s ERROR: malloc for nsample failed, not enough core memory\n",argv[0]); exit(0); }

  gps_t=0;
  opterr=0; while ((c = getopt (argc, argv, "o:t")) != -1)
  switch (c)
      {
         case 't':        /* gps_t timing info explicite */
           gps_t=1;               
           break;
         case '?':
           fprintf(stderr,"%s ERROR: Unrecognized option: -%c\n",argv[0], optopt);
           errflg++;
      }


/* read config */
   if (fgets(buf,1000,stdin)) 
   {
	code=sscanf(buf,STR_FORMAT,(char *) SC_STR); 
   }

   fprintf(stdout,STR_FORMAT"\n",PR_STR);


/* allocating arrays for the CH_NUM time series */
  if (!(data = malloc (CH_NUM * sizeof(int)))) { fprintf(stderr, "%s ERROR: malloc failed, not enough core memory\n",argv[0]); exit(0); }
  for (ch = 0; ch < CH_NUM; ch++) if (!(data[ch] = malloc (MXSMP * sizeof(int)))) { fprintf(stderr, "%s ERROR: malloc failed, not enough core memory\n",argv[0]); exit(0); }


/* read til first time stamp */
  j=0;
  x1=-1;
  while (x1 < 0)
  {
       if (fgets(buf,1000,stdin))
       {
	switch (CH_NUM)
	{
		case 1: code=sscanf(buf,"%lld%d%ld%ld",&nsamp,&ampx[0],&x1,&x2); break;
		case 2: code=sscanf(buf,"%lld%d%d%ld%ld",&nsamp,&ampx[0],&ampx[1],&x1,&x2); break;
		case 3: code=sscanf(buf,"%lld%d%d%d%ld%ld",&nsamp,&ampx[0],&ampx[1],&ampx[2],&x1,&x2); break;
	}
/* no data treatment */
	if (nsamp==-1)
	{ 
		fprintf(stdout,"%ld ",-1L); 
  		for (ch=0; ch<CH_NUM; ch++) fprintf(stdout,"%d ",0); 
  		fprintf(stdout,"%ld %ld\n",0L,0L); 
		fprintf(stderr,"%s ERROR: No useful data in add_time!\n", argv[0]); 
		exit(77);
	}
       }
       j++;
  }


/* ini for segment */
  i=0;
  nsample[i]=nsamp;
  for (ch=0; ch<CH_NUM; ch++) data[ch][i]=ampx[ch]; 		
  ts0l=x1*1000000+x2;

/*  fprintf(stderr,"start with %d %d %ld %ld\n",nsamp,amp,x1,x2); */



/* main loop */     
  while (!feof(stdin))
  {
       if (fgets(buf,1000,stdin))
       {
	switch (CH_NUM)
	{
		case 1: code=sscanf(buf,"%lld%d%ld%ld",&nsamp,&ampx[0],&x1,&x2); break;
		case 2: code=sscanf(buf,"%lld%d%d%ld%ld",&nsamp,&ampx[0],&ampx[1],&x1,&x2); break;
		case 3: code=sscanf(buf,"%lld%d%d%d%ld%ld",&nsamp,&ampx[0],&ampx[1],&ampx[2],&x1,&x2); break;
	}
       }
/*	fprintf(stderr,"next %d %d %lf\n", nsamp,amp,x1); */
	if ( x1 < 0.0 ) 
	{		
		i++;
		if (i>=MXSMP) { fprintf(stderr,"%s ERROR: max number of sample w/o GPS exceeds %ld, increase MXSMP in cube.h and recompile\n",argv[0],MXSMP); exit(200); }
		nsample[i]=nsamp;
		for (ch=0; ch<CH_NUM; ch++) data[ch][i]=ampx[ch]; 
		tsl=x1*1000000+x2;
	}
	else
	{
/* add time + output */
		i++;
		nsample[i]=nsamp;
		for (ch=0; ch<CH_NUM; ch++) data[ch][i]=ampx[ch]; 
		tsl=x1*1000000+x2;
/* fprintf(stderr," add time from %ld to %ld with %ld samples \n",ts0,ts,i); */
		for (k=0; k<i; k++) 
		{
			txl = (long long) (1.0*(tsl-ts0l)/i*k+ts0l);
			sec=(long) floor(txl/1000000);
			mksec = (long) (txl-floor(txl/1000000)*1000000);
			fprintf(stdout,"%lld ",nsample[k]); 
/* TEST with sin 
			t=txl*0.000001-1400054271.0;
			T=10.0;
			for (ch=0; ch<CH_NUM; ch++) data[ch][k]=(int) (1000000.0*sin(M_PI*t/T)); 
*/

			for (ch=0; ch<CH_NUM; ch++) fprintf(stdout,"%d ",data[ch][k]); 
			if (gps_t==0) fprintf(stdout,"%ld %ld \n",sec,mksec); 
			if (gps_t==1) 
			{
				start_time=sec;
  				tss = localtime(&start_time); 
				fprintf(stdout,"%4d-%02d-%02dT%02d:%02d:%02d %6ld\n",(int) (tss->tm_year)+1900,(int) (tss->tm_mon)+1,(int) (tss->tm_mday),(int) (tss->tm_hour),(int) (tss->tm_min),(int) (tss->tm_sec),mksec);
 
			}
		}
/* ini for next segment */
		i=0;
		nsample[i]=nsamp;
		for (ch=0; ch<CH_NUM; ch++) data[ch][i]=ampx[ch]; 
		ts0l=x1*1000000+x2;
	}
  }
/* write last tagged sample */

  fprintf(stdout,"%lld ",nsample[0]); 
  for (ch=0; ch<CH_NUM; ch++) fprintf(stdout,"%d ",data[ch][0]); 
  fprintf(stdout,"%ld %ld\n",(long) floor(ts0l/1000000),(long) (ts0l-floor(ts0l/1000000)*1000000)); 

  j=j-1;
  
/*  fprintf(stderr,"file read with %d samples\n",j); */


 }

